fix race condition in sockets unit test

This commit is contained in:
Davis King 2022-02-21 08:31:40 -05:00
parent c7ed14c956
commit 39852f092c

View File

@ -25,6 +25,7 @@ namespace {
const char magic_num = 42; const char magic_num = 42;
const int min_bytes_sent = 10000; const int min_bytes_sent = 10000;
int assigned_port; int assigned_port;
const int num_test_connections = 10;
logger dlog("test.sockets"); logger dlog("test.sockets");
@ -36,7 +37,8 @@ namespace {
serv ( serv (
) : ) :
error_occurred (false), error_occurred (false),
got_connections(false) got_connections(false),
s(m)
{} {}
void on_listening_port_assigned ( void on_listening_port_assigned (
@ -75,11 +77,33 @@ namespace {
} }
got_connections = true; got_connections = true;
dlog << LINFO << "in serv::on_connect(): on_connect ending"; dlog << LINFO << "in serv::on_connect(): on_connect ending";
auto_mutex M(m);
++num_connections;
s.broadcast();
} }
bool error_occurred; bool error_occurred;
bool got_connections; bool got_connections;
double tag; double tag;
void wait_for_all_connections_to_close()
{
auto_mutex M(m);
while(num_connections != num_test_connections)
{
// something has gone wrong if it takes more than 10 seconds. So just end and let
// the test fail in that case.
s.wait_or_timeout(10000);
}
}
private:
dlib::mutex m;
dlib::signaler s;
int num_connections = 0;
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -94,7 +118,7 @@ namespace {
serv& srv_ serv& srv_
) : srv(srv_) ) : srv(srv_)
{ {
for (int i = 0; i < 10; ++i) for (int i = 0; i < num_test_connections; ++i)
register_thread(*this, &thread_container::thread_proc); register_thread(*this, &thread_container::thread_proc);
// start up the threads // start up the threads
@ -211,7 +235,7 @@ namespace {
// wait until all the sending threads have ended // wait until all the sending threads have ended
stuff.wait(); stuff.wait();
srv.wait_for_all_connections_to_close();
srv.clear(); srv.clear();
if (srv.error_occurred) if (srv.error_occurred)