Skip to content

Commit c04dfdb

Browse files
committed
fix(PollSetTest): avoid looping
1 parent cb07358 commit c04dfdb

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

Net/testsuite/src/PollSetTest.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -326,43 +326,43 @@ void PollSetTest::testPoll()
326326

327327
void PollSetTest::testPollNoServer()
328328
{
329-
StreamSocket ss1;
330-
StreamSocket ss2;
331-
ss1.connectNB(SocketAddress("127.0.0.1", 0xFEFE));
332-
ss2.connectNB(SocketAddress("127.0.0.1", 0xFEFF));
329+
StreamSocket ss1(SocketAddress::IPv4);
330+
StreamSocket ss2(SocketAddress::IPv4);
331+
333332
PollSet ps;
334333
assertTrue(ps.empty());
335334
ps.add(ss1, PollSet::POLL_READ | PollSet::POLL_WRITE | PollSet::POLL_ERROR);
336335
ps.add(ss2, PollSet::POLL_READ | PollSet::POLL_WRITE | PollSet::POLL_ERROR);
337336
assertTrue(!ps.empty());
338337
assertTrue(ps.has(ss1));
339338
assertTrue(ps.has(ss2));
340-
PollSet::SocketModeMap sm;
341-
int signalled = 0;
342-
Stopwatch sw; sw.start();
343-
do
339+
340+
try
344341
{
345-
sm = ps.poll(Timespan(1000000));
346-
for (auto s : sm)
347-
assertTrue(0 != (s.second & PollSet::POLL_ERROR));
348-
349-
signalled += static_cast<int>(sm.size());
350-
if (sw.elapsedSeconds() > 10) fail();
351-
} while (signalled < 2);
352-
assertTrue(signalled == 2);
342+
ss1.connect(SocketAddress("127.0.0.1", 0xFEFE));
343+
fail("Socket should not connect");
344+
}
345+
catch (Poco::Net::ConnectionRefusedException&) {}
346+
catch (Poco::IOException&) {}
353347

348+
try
349+
{
350+
ss2.connect(SocketAddress("127.0.0.1", 0xFEFE));
351+
fail("Socket should not connect");
352+
}
353+
catch (Poco::Net::ConnectionRefusedException&) {}
354+
catch (Poco::IOException&) {}
355+
356+
assertTrue (2 == ps.poll(Timespan(1000000)).size());
354357
}
355358

356359

357360
void PollSetTest::testPollClosedServer()
358361
{
359362
EchoServer echoServer1;
360363
EchoServer echoServer2;
361-
StreamSocket ss1;
362-
StreamSocket ss2;
363-
364-
ss1.connect(SocketAddress("127.0.0.1", echoServer1.port()));
365-
ss2.connect(SocketAddress("127.0.0.1", echoServer2.port()));
364+
StreamSocket ss1(SocketAddress::IPv4);
365+
StreamSocket ss2(SocketAddress::IPv4);
366366

367367
PollSet ps;
368368
assertTrue(ps.empty());
@@ -372,6 +372,9 @@ void PollSetTest::testPollClosedServer()
372372
assertTrue(ps.has(ss1));
373373
assertTrue(ps.has(ss2));
374374

375+
ss1.connect(SocketAddress("127.0.0.1", echoServer1.port()));
376+
ss2.connect(SocketAddress("127.0.0.1", echoServer2.port()));
377+
375378
std::string str = "HELLO";
376379
int len = static_cast<int>(str.length());
377380

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

388-
int signalled = 0;
389-
Stopwatch sw; sw.start();
390-
do
391-
{
392-
signalled += static_cast<int>(ps.poll(Timespan(1000000)).size());
393-
int secs = sw.elapsedSeconds();
394-
if (secs > 10)
395-
fail(Poco::format("timed out after %ds, sockets signalled=%z (expected 2)", secs, signalled), __LINE__);
396-
} while (signalled < 2);
397-
398-
assertTrue(signalled == 2);
399391
// socket closed or error
400-
assertTrue(0 >= ss1.receiveBytes(0, 0));
401-
assertTrue(0 >= ss2.receiveBytes(0, 0));
392+
char c;
393+
assertTrue(0 >= ss1.receiveBytes(&c, 1));
394+
assertTrue(0 >= ss2.receiveBytes(&c, 1));
395+
396+
assertTrue(2 == ps.poll(Timespan(1000000)).size());
402397
}
403398

404399

0 commit comments

Comments
 (0)