"git@developer.sourcefind.cn:OpenDAS/dgl.git" did not exist on "24dc71fc9559786c151db3456303b0d38e020031"
Commit 39852f09 authored by Davis King's avatar Davis King
Browse files

fix race condition in sockets unit test

parent c7ed14c9
...@@ -25,6 +25,7 @@ namespace { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment