| 1 | // |
| 2 | // PollSetTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2016, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "PollSetTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "EchoServer.h" |
| 15 | #include "Poco/Net/StreamSocket.h" |
| 16 | #include "Poco/Net/ServerSocket.h" |
| 17 | #include "Poco/Net/SocketAddress.h" |
| 18 | #include "Poco/Net/NetException.h" |
| 19 | #include "Poco/Net/PollSet.h" |
| 20 | #include "Poco/Stopwatch.h" |
| 21 | |
| 22 | |
| 23 | using Poco::Net::Socket; |
| 24 | using Poco::Net::StreamSocket; |
| 25 | using Poco::Net::ServerSocket; |
| 26 | using Poco::Net::SocketAddress; |
| 27 | using Poco::Net::ConnectionRefusedException; |
| 28 | using Poco::Net::PollSet; |
| 29 | using Poco::Timespan; |
| 30 | using Poco::Stopwatch; |
| 31 | |
| 32 | |
| 33 | PollSetTest::PollSetTest(const std::string& name): CppUnit::TestCase(name) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | |
| 38 | PollSetTest::~PollSetTest() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void PollSetTest::testPoll() |
| 44 | { |
| 45 | EchoServer echoServer1; |
| 46 | EchoServer echoServer2; |
| 47 | StreamSocket ss1; |
| 48 | StreamSocket ss2; |
| 49 | |
| 50 | ss1.connect(SocketAddress("127.0.0.1" , echoServer1.port())); |
| 51 | ss2.connect(SocketAddress("127.0.0.1" , echoServer2.port())); |
| 52 | |
| 53 | PollSet ps; |
| 54 | assertTrue(ps.empty()); |
| 55 | ps.add(ss1, PollSet::POLL_READ); |
| 56 | assertTrue(!ps.empty()); |
| 57 | assertTrue(ps.has(ss1)); |
| 58 | assertTrue(!ps.has(ss2)); |
| 59 | |
| 60 | // nothing readable |
| 61 | Stopwatch sw; |
| 62 | sw.start(); |
| 63 | Timespan timeout(1000000); |
| 64 | assertTrue (ps.poll(timeout).empty()); |
| 65 | assertTrue (sw.elapsed() >= 900000); |
| 66 | sw.restart(); |
| 67 | |
| 68 | ps.add(ss2, PollSet::POLL_READ); |
| 69 | assertTrue(!ps.empty()); |
| 70 | assertTrue(ps.has(ss1)); |
| 71 | assertTrue(ps.has(ss2)); |
| 72 | |
| 73 | // ss1 must be writable, if polled for |
| 74 | ps.update(ss1, PollSet::POLL_READ | PollSet::POLL_WRITE); |
| 75 | PollSet::SocketModeMap sm = ps.poll(timeout); |
| 76 | assertTrue (sm.find(ss1) != sm.end()); |
| 77 | assertTrue (sm.find(ss2) == sm.end()); |
| 78 | assertTrue (sm.find(ss1)->second == PollSet::POLL_WRITE); |
| 79 | assertTrue (sw.elapsed() < 100000); |
| 80 | |
| 81 | ps.update(ss1, PollSet::POLL_READ); |
| 82 | |
| 83 | ss1.sendBytes("hello" , 5); |
| 84 | char buffer[256]; |
| 85 | sw.restart(); |
| 86 | sm = ps.poll(timeout); |
| 87 | assertTrue (sm.find(ss1) != sm.end()); |
| 88 | assertTrue (sm.find(ss2) == sm.end()); |
| 89 | assertTrue (sm.find(ss1)->second == PollSet::POLL_READ); |
| 90 | assertTrue (sw.elapsed() < 100000); |
| 91 | |
| 92 | int n = ss1.receiveBytes(buffer, sizeof(buffer)); |
| 93 | assertTrue (n == 5); |
| 94 | assertTrue (std::string(buffer, n) == "hello" ); |
| 95 | |
| 96 | |
| 97 | ss2.sendBytes("HELLO" , 5); |
| 98 | sw.restart(); |
| 99 | sm = ps.poll(timeout); |
| 100 | assertTrue (sm.find(ss1) == sm.end()); |
| 101 | assertTrue (sm.find(ss2) != sm.end()); |
| 102 | assertTrue (sm.find(ss2)->second == PollSet::POLL_READ); |
| 103 | assertTrue (sw.elapsed() < 100000); |
| 104 | |
| 105 | n = ss2.receiveBytes(buffer, sizeof(buffer)); |
| 106 | assertTrue (n == 5); |
| 107 | assertTrue (std::string(buffer, n) == "HELLO" ); |
| 108 | |
| 109 | ps.remove(ss2); |
| 110 | assertTrue(!ps.empty()); |
| 111 | assertTrue(ps.has(ss1)); |
| 112 | assertTrue(!ps.has(ss2)); |
| 113 | |
| 114 | ss2.sendBytes("HELLO" , 5); |
| 115 | sw.restart(); |
| 116 | sm = ps.poll(timeout); |
| 117 | assertTrue (sm.empty()); |
| 118 | |
| 119 | n = ss2.receiveBytes(buffer, sizeof(buffer)); |
| 120 | assertTrue (n == 5); |
| 121 | assertTrue (std::string(buffer, n) == "HELLO" ); |
| 122 | |
| 123 | ss1.close(); |
| 124 | ss2.close(); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void PollSetTest::setUp() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void PollSetTest::tearDown() |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | |
| 138 | CppUnit::Test* PollSetTest::suite() |
| 139 | { |
| 140 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PollSetTest" ); |
| 141 | |
| 142 | CppUnit_addTest(pSuite, PollSetTest, testPoll); |
| 143 | |
| 144 | return pSuite; |
| 145 | } |
| 146 | |