Skip to content

Commit

Permalink
fix(PollSetTest): avoid looping
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Oct 24, 2023
1 parent cb07358 commit c04dfdb
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions Net/testsuite/src/PollSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,43 +326,43 @@ void PollSetTest::testPoll()

void PollSetTest::testPollNoServer()
{
StreamSocket ss1;
StreamSocket ss2;
ss1.connectNB(SocketAddress("127.0.0.1", 0xFEFE));
ss2.connectNB(SocketAddress("127.0.0.1", 0xFEFF));
StreamSocket ss1(SocketAddress::IPv4);
StreamSocket ss2(SocketAddress::IPv4);

PollSet ps;
assertTrue(ps.empty());
ps.add(ss1, PollSet::POLL_READ | PollSet::POLL_WRITE | PollSet::POLL_ERROR);
ps.add(ss2, PollSet::POLL_READ | PollSet::POLL_WRITE | PollSet::POLL_ERROR);
assertTrue(!ps.empty());
assertTrue(ps.has(ss1));
assertTrue(ps.has(ss2));
PollSet::SocketModeMap sm;
int signalled = 0;
Stopwatch sw; sw.start();
do

try
{
sm = ps.poll(Timespan(1000000));
for (auto s : sm)
assertTrue(0 != (s.second & PollSet::POLL_ERROR));

signalled += static_cast<int>(sm.size());
if (sw.elapsedSeconds() > 10) fail();
} while (signalled < 2);
assertTrue(signalled == 2);
ss1.connect(SocketAddress("127.0.0.1", 0xFEFE));
fail("Socket should not connect");
}
catch (Poco::Net::ConnectionRefusedException&) {}
catch (Poco::IOException&) {}

try
{
ss2.connect(SocketAddress("127.0.0.1", 0xFEFE));
fail("Socket should not connect");
}
catch (Poco::Net::ConnectionRefusedException&) {}
catch (Poco::IOException&) {}

assertTrue (2 == ps.poll(Timespan(1000000)).size());
}


void PollSetTest::testPollClosedServer()
{
EchoServer echoServer1;
EchoServer echoServer2;
StreamSocket ss1;
StreamSocket ss2;

ss1.connect(SocketAddress("127.0.0.1", echoServer1.port()));
ss2.connect(SocketAddress("127.0.0.1", echoServer2.port()));
StreamSocket ss1(SocketAddress::IPv4);
StreamSocket ss2(SocketAddress::IPv4);

PollSet ps;
assertTrue(ps.empty());
Expand All @@ -372,6 +372,9 @@ void PollSetTest::testPollClosedServer()
assertTrue(ps.has(ss1));
assertTrue(ps.has(ss2));

ss1.connect(SocketAddress("127.0.0.1", echoServer1.port()));
ss2.connect(SocketAddress("127.0.0.1", echoServer2.port()));

std::string str = "HELLO";
int len = static_cast<int>(str.length());

Expand All @@ -385,20 +388,12 @@ void PollSetTest::testPollClosedServer()
assertTrue (len == ss2.sendBytes(str.data(), len));
while (!echoServer2.done()) Thread::sleep(10);

int signalled = 0;
Stopwatch sw; sw.start();
do
{
signalled += static_cast<int>(ps.poll(Timespan(1000000)).size());
int secs = sw.elapsedSeconds();
if (secs > 10)
fail(Poco::format("timed out after %ds, sockets signalled=%z (expected 2)", secs, signalled), __LINE__);
} while (signalled < 2);

assertTrue(signalled == 2);
// socket closed or error
assertTrue(0 >= ss1.receiveBytes(0, 0));
assertTrue(0 >= ss2.receiveBytes(0, 0));
char c;
assertTrue(0 >= ss1.receiveBytes(&c, 1));
assertTrue(0 >= ss2.receiveBytes(&c, 1));

assertTrue(2 == ps.poll(Timespan(1000000)).size());
}


Expand Down

0 comments on commit c04dfdb

Please sign in to comment.