Skip to content

Commit

Permalink
fix(PollSet): wait on premature epoll_wait return; reinforce tests fo…
Browse files Browse the repository at this point in the history
…r windows
  • Loading branch information
aleks-f committed Nov 25, 2023
1 parent 2807c79 commit e8f8299
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
26 changes: 18 additions & 8 deletions Net/src/PollSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ class PollSetImpl
Poco::Timestamp start;
rc = epoll_wait(_epollfd, &_events[0],
static_cast<int>(_events.size()), static_cast<int>(remainingTime.totalMilliseconds()));
if (rc == 0) return result;
if (rc == 0)
{
if (keepWaiting(start, remainingTime)) continue;
return result;
}

// if we are hitting the events limit, resize it; even without resizing, the subseqent
// calls would round-robin through the remaining ready sockets, but it's better to give
Expand All @@ -187,13 +191,7 @@ class PollSetImpl
// if interrupted and there's still time left, keep waiting
if (SocketImpl::lastError() == POCO_EINTR)
{
Poco::Timestamp end;
Poco::Timespan waited = end - start;
if (waited < remainingTime)
{
remainingTime -= waited;
continue;
}
if (keepWaiting(start, remainingTime)) continue;
}
else SocketImpl::error();
}
Expand Down Expand Up @@ -304,6 +302,18 @@ class PollSetImpl
return epoll_ctl(_epollfd, op, fd, &ev);
}

static bool keepWaiting(const Poco::Timestamp& start, Poco::Timespan& remainingTime)
{
Poco::Timestamp end;
Poco::Timespan waited = end - start;
if (waited < remainingTime)
{
remainingTime -= waited;
return true;
}
return false;
}

#ifndef WEPOLL_H_
using EPollHandle = std::atomic<int>;
#else // WEPOLL_H_
Expand Down
3 changes: 2 additions & 1 deletion Net/testsuite/src/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ void EchoServer::run()
{
std::cerr << "EchoServer: " << exc.displayText() << std::endl;
}
ss.close();
}
}
_socket.close();
_done = true;
}

Expand All @@ -99,4 +101,3 @@ bool EchoServer::done()
{
return _done;
}

19 changes: 16 additions & 3 deletions Net/testsuite/src/PollSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,19 @@ void PollSetTest::testPollClosedServer()
"waiting on server after %ds", secs), __LINE__);
}
}

char buffer[5];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue(n == 0);
auto smm = ps.poll(Timespan(1000000));
assertEqual(1, smm.size());
assertTrue(ss1 == smm.begin()->first);
ps.remove(ss1);
assertTrue(!ps.empty());
assertTrue(!ps.has(ss1));
assertTrue(ps.has(ss2));
echoServer2.stop();
assertTrue (len == ss2.sendBytes(str.data(), len));
sw.restart();
while (!echoServer2.done())
{
Thread::sleep(10);
Expand All @@ -417,8 +427,11 @@ void PollSetTest::testPollClosedServer()
"waiting on server after %ds", secs), __LINE__);
}
}

assertEqual(2, ps.poll(Timespan(1000000)).size());
n = ss2.receiveBytes(buffer, sizeof(buffer));
assertTrue(n == 0);
smm = ps.poll(Timespan(1000000));
assertEqual(1, smm.size());
assertTrue(ss2 == smm.begin()->first);

// socket closed or error
assertTrue(0 >= ss1.receiveBytes(0, 0));
Expand Down
12 changes: 8 additions & 4 deletions Net/testsuite/src/SocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,10 @@ void SocketTest::testEchoUnixLocal()
if (socketFile.exists()) socketFile.remove();
echoServer.stop();
#else // POCO_HAS_UNIX_SOCKET
#pragma message("[UNIX LOCAL SOCKET DISABLED]")
std::cout << "[UNIX LOCAL SOCKET DISABLED]" << std::endl;
#if POCO_OS == POCO_OS_WINDOWS_NT
#pragma message("[UNIX LOCAL SOCKET DISABLED]")
#endif
std::cout << "[UNIX LOCAL SOCKET DISABLED]";
#endif
}

Expand All @@ -588,8 +590,10 @@ void SocketTest::testUnixLocalAbstract()
ss.close();
echoServer.stop();
#else // POCO_HAS_UNIX_SOCKET
#pragma message("[ABSTRACT UNIX LOCAL SOCKET DISABLED]")
std::cout << "[ABSTRACT UNIX LOCAL SOCKET DISABLED]" << std::endl;
#if POCO_OS == POCO_OS_WINDOWS_NT
#pragma message("[ABSTRACT UNIX LOCAL SOCKET DISABLED]")
#endif
std::cout << "[ABSTRACT UNIX LOCAL SOCKET DISABLED]";
#endif
}

Expand Down
8 changes: 4 additions & 4 deletions Net/testsuite/src/SocketTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class SocketTest: public CppUnit::TestCase
void onReadable(bool& b);
void onWritable(bool& b);

int _readableToNot;
int _notToReadable;
int _writableToNot;
int _notToWritable;
int _readableToNot = 0;
int _notToReadable = 0;
int _writableToNot = 0;
int _notToWritable = 0;
};


Expand Down

0 comments on commit e8f8299

Please sign in to comment.