Commit b5578687 authored by Davis King's avatar Davis King

cleaned up the sockets test

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402497
parent aa34d39f
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
#include "tester.h" #include "tester.h"
namespace namespace {
{
using namespace test; using namespace test;
using namespace dlib; using namespace dlib;
...@@ -23,8 +22,6 @@ namespace ...@@ -23,8 +22,6 @@ namespace
dlib::signaler s(m); dlib::signaler s(m);
const char magic_num = 42; const char magic_num = 42;
const int min_bytes_sent = 10000; const int min_bytes_sent = 10000;
int thread_count;
int thread_count2;
int assigned_port; int assigned_port;
logger dlog("test.sockets"); logger dlog("test.sockets");
...@@ -36,7 +33,8 @@ namespace ...@@ -36,7 +33,8 @@ namespace
public: public:
serv ( serv (
) : ) :
error_occurred (false) error_occurred (false),
got_connections(false)
{} {}
void on_listening_port_assigned ( void on_listening_port_assigned (
...@@ -73,31 +71,54 @@ namespace ...@@ -73,31 +71,54 @@ namespace
tag = 5.0; tag = 5.0;
error_occurred = true; error_occurred = true;
} }
got_connections = true;
dlog << LINFO << "in serv::on_connect(): on_connect ending"; dlog << LINFO << "in serv::on_connect(): on_connect ending";
} }
bool error_occurred; bool error_occurred;
bool got_connections;
double tag; double tag;
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
class thread_container : public multithreaded_object
{
public:
serv& srv;
thread_container (
serv& srv_
) : srv(srv_)
{
for (int i = 0; i < 10; ++i)
register_thread(*this, &thread_container::thread_proc);
// start up the threads
start();
}
~thread_container ()
{
// wait for all threads to terminate
wait();
}
void thread_proc ( void thread_proc (
void* param
) )
{ {
serv& srv = *reinterpret_cast<serv*>(param);
try try
{ {
dlog << LTRACE << "enter thread"; dlog << LTRACE << "enter thread";
{ {
auto_mutex M(m); auto_mutex M(::m);
while (assigned_port == 0) while (assigned_port == 0)
s.wait(); ::s.wait();
} }
int status; int status;
connection* con; scoped_ptr<connection> con;
string hostname; string hostname;
string ip; string ip;
status = get_local_hostname(hostname); status = get_local_hostname(hostname);
...@@ -106,7 +127,8 @@ namespace ...@@ -106,7 +127,8 @@ namespace
srv.tag = 1.0; srv.tag = 1.0;
srv.error_occurred = true; srv.error_occurred = true;
srv.clear(); srv.clear();
dlog << LWARN << "leaving thread, line: " << __LINE__; dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "get_local_hostname() failed";
return; return;
} }
...@@ -116,7 +138,8 @@ namespace ...@@ -116,7 +138,8 @@ namespace
srv.tag = 2.0; srv.tag = 2.0;
srv.error_occurred = true; srv.error_occurred = true;
srv.clear(); srv.clear();
dlog << LWARN << "leaving thread, line: " << __LINE__; dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "hostname_to_ip() failed";
return; return;
} }
...@@ -127,7 +150,8 @@ namespace ...@@ -127,7 +150,8 @@ namespace
srv.tag = 3.0; srv.tag = 3.0;
srv.error_occurred = true; srv.error_occurred = true;
srv.clear(); srv.clear();
dlog << LWARN << "leaving thread, line: " << __LINE__; dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "create_connection() failed";
return; return;
} }
...@@ -141,17 +165,6 @@ namespace ...@@ -141,17 +165,6 @@ namespace
dlog << LTRACE << "shutting down connection to server"; dlog << LTRACE << "shutting down connection to server";
close_gracefully(con); close_gracefully(con);
dlog << LTRACE << "finished calling close_gracefully() on the connection"; dlog << LTRACE << "finished calling close_gracefully() on the connection";
auto_mutex M(m);
--thread_count;
s.broadcast();
while (thread_count > 0)
s.wait();
srv.clear();
--thread_count2;
s.broadcast();
} }
catch (exception& e) catch (exception& e)
{ {
...@@ -161,6 +174,14 @@ namespace ...@@ -161,6 +174,14 @@ namespace
} }
dlog << LTRACE << "exit thread"; dlog << LTRACE << "exit thread";
} }
};
void run_server(serv* srv)
{
dlog << LTRACE << "calling srv.start()";
srv->start();
dlog << LTRACE << "srv.start() just ended.";
}
void sockets_test ( void sockets_test (
) )
...@@ -176,36 +197,29 @@ namespace ...@@ -176,36 +197,29 @@ namespace
dlog << LTRACE << "starting test"; dlog << LTRACE << "starting test";
serv srv; serv srv;
thread_count = 10;
assigned_port = 0; assigned_port = 0;
thread_count2 = thread_count;
dlog << LTRACE << "spawning threads"; dlog << LTRACE << "spawning threads";
int num = thread_count; thread_container stuff(srv);
for (int i = 0; i < num; ++i)
{
create_new_thread(thread_proc,&srv);
}
dlog << LTRACE << "calling srv.start()"; thread_function thread2(run_server, &srv);
srv.start();
dlog << LTRACE << "srv.start() just ended."; // wait until all the sending threads have ended
stuff.wait();
{
auto_mutex M(m);
while (thread_count2 > 0)
s.wait();
}
if (srv.error_occurred) if (srv.error_occurred)
{ {
dlog << LDEBUG << "tag: " << srv.tag; dlog << LDEBUG << "tag: " << srv.tag;
} }
srv.clear();
dlog << LTRACE << "ending successful test"; dlog << LTRACE << "ending successful test";
DLIB_CASSERT( !srv.error_occurred,""); DLIB_CASSERT( !srv.error_occurred,"");
DLIB_CASSERT( srv.got_connections,"");
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
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