From: Gerd v. Egidy Date: Wed, 27 Aug 2008 08:58:48 +0000 (+0000) Subject: libt2n: (gerd) make sure no exception is thrown in command_client constructor, fix... X-Git-Tag: v0.4~9 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=b592218402bfa779c024da2f32c4c782d52eaf88 libt2n: (gerd) make sure no exception is thrown in command_client constructor, fix killing of child processes in unit tests --- diff --git a/src/command_client.cpp b/src/command_client.cpp index 8ccbf10..63ff539 100644 --- a/src/command_client.cpp +++ b/src/command_client.cpp @@ -49,8 +49,25 @@ command_client::command_client(client_connection* _c, long long _command_timeout c->add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this))); // don't expect hello from an always closed connection (like dummy_client_connection) - if (!c->is_closed()) - read_hello(); + if (!is_connection_closed()) + { + try + { + read_hello(); + } + catch (t2n_communication_error &e) + { + c->close(); + + // store a copy of the exception that you can find out details about the error later + std::auto_ptr tmp(e.clone()); + constructorException=tmp; + } + catch (...) + { + throw; + } + } } /** @brief replace the connection currently in use with a new one @@ -197,6 +214,9 @@ void command_client::send_command(command* cmd, result_container &res) command_container cc(cmd); boost::archive::binary_oarchive oa(ofs); + if (is_connection_closed()) + throw t2n_transfer_error("connection to server is closed"); + try { oa << cc; diff --git a/src/command_client.hxx b/src/command_client.hxx index 841609c..59d4755 100644 --- a/src/command_client.hxx +++ b/src/command_client.hxx @@ -19,6 +19,9 @@ #ifndef __LIBT2N_COMMAND_CLIENT #define __LIBT2N_COMMAND_CLIENT +#include +#include + #include "client.hxx" #include "container.hxx" @@ -42,6 +45,8 @@ class command_client std::string read_packet(const long long &usec_timeout); bool check_hello(const std::string& hellostr); + std::auto_ptr constructorException; + public: command_client(client_connection* _c, long long _command_timeout_usec=command_timeout_usec_default, @@ -60,6 +65,10 @@ class command_client { return command_timeout_usec; } long long get_hello_timeout_usec(void) { return hello_timeout_usec; } + bool is_connection_closed(void) + { return c->is_closed(); } + t2n_exception* get_constuctor_exception(void) + { return constructorException.get(); } }; } diff --git a/test/callback.cpp b/test/callback.cpp index bc951b9..257da83 100644 --- a/test/callback.cpp +++ b/test/callback.cpp @@ -44,6 +44,8 @@ class test_callback : public TestFixture std::vector callback_done; + pid_t child_pid; + public: void setUp() @@ -54,6 +56,10 @@ class test_callback : public TestFixture void tearDown() { callback_done.clear(); + + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); } void callback_func(callback_event_type ev, int conn_id) @@ -67,9 +73,7 @@ class test_callback : public TestFixture void ServerNewConnCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -128,9 +132,7 @@ class test_callback : public TestFixture void ServerConnClosedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -189,9 +191,7 @@ class test_callback : public TestFixture void ServerConnDeletedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -253,9 +253,7 @@ class test_callback : public TestFixture void ServerCallbackOrder() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -336,9 +334,7 @@ class test_callback : public TestFixture void ClientConnClosedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -390,9 +386,7 @@ class test_callback : public TestFixture void ClientConnDeletedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { diff --git a/test/cmdgroup.cpp b/test/cmdgroup.cpp index 32f8c6d..314e28d 100644 --- a/test/cmdgroup.cpp +++ b/test/cmdgroup.cpp @@ -167,19 +167,23 @@ class test_cmdgroup : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void GroupOk() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -220,9 +224,7 @@ class test_cmdgroup : public TestFixture void WrongGroup() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { diff --git a/test/comm.cpp b/test/comm.cpp index 688cbf0..7bd8b24 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -41,20 +41,25 @@ class test_comm : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void UnixCommToServer() { - pid_t pid; string data; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -94,9 +99,7 @@ class test_comm : public TestFixture void UnixCommToServerAndBack() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -166,9 +169,7 @@ class test_comm : public TestFixture void UnixCommToServerAndBackBig() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -229,10 +230,9 @@ class test_comm : public TestFixture void IPCommToServer() { - pid_t pid; string data; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -272,9 +272,7 @@ class test_comm : public TestFixture void IPCommToServerAndBack() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -344,9 +342,7 @@ class test_comm : public TestFixture void IPCommToServerAndBackBig() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -406,9 +402,6 @@ class test_comm : public TestFixture } } } // eo IPCommToServerAndBackBig() - - - }; CPPUNIT_TEST_SUITE_REGISTRATION(test_comm); diff --git a/test/hello.cpp b/test/hello.cpp index b9e1489..1036e30 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -56,13 +56,19 @@ class test_hello : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void send_hello(string hello_string, socket_server* ss, int conn_id) { @@ -81,9 +87,7 @@ class test_hello : public TestFixture void HelloOk() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -125,9 +129,7 @@ class test_hello : public TestFixture void BadTag() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -160,16 +162,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); + + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); } @@ -178,9 +177,7 @@ class test_hello : public TestFixture void BadVersion() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -217,16 +214,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg); } @@ -235,9 +229,7 @@ class test_hello : public TestFixture void SeparatorMissing() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -273,16 +265,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg); } @@ -291,9 +280,7 @@ class test_hello : public TestFixture void WrongByteOrder() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -338,16 +325,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg); } @@ -356,9 +340,7 @@ class test_hello : public TestFixture void OtherServerBig() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -392,16 +374,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); + + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); } @@ -410,9 +389,7 @@ class test_hello : public TestFixture void OtherServerSmall() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -446,16 +423,13 @@ class test_hello : public TestFixture sleep(1); socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(&sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); } diff --git a/test/reconnect.cpp b/test/reconnect.cpp index 9e64a8e..394ba25 100644 --- a/test/reconnect.cpp +++ b/test/reconnect.cpp @@ -52,13 +52,19 @@ class test_reconnect : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void send_raw_socket(string hello_string, socket_server* ss, int conn_id) { @@ -71,9 +77,7 @@ class test_reconnect : public TestFixture void simple_reconnect() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -144,9 +148,7 @@ class test_reconnect : public TestFixture void reconnect_with_close() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -222,9 +224,7 @@ class test_reconnect : public TestFixture void reconnect_buffer_complete() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -290,11 +290,9 @@ class test_reconnect : public TestFixture void reconnect_buffer_several_complete() { - pid_t pid; - const int packets=3; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -380,9 +378,7 @@ class test_reconnect : public TestFixture void reconnect_buffer_no_incomplete1() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -458,9 +454,7 @@ class test_reconnect : public TestFixture void reconnect_buffer_no_incomplete2() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { diff --git a/test/serialize.cpp b/test/serialize.cpp index 85a9b58..db31fba 100644 --- a/test/serialize.cpp +++ b/test/serialize.cpp @@ -117,19 +117,23 @@ class test_serialize : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void ClientSerializeErr() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { diff --git a/test/simplecmd.cpp b/test/simplecmd.cpp index 42721ea..36e08f1 100644 --- a/test/simplecmd.cpp +++ b/test/simplecmd.cpp @@ -121,20 +121,23 @@ class test_simplecmd : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() - { -} + { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void SimpleCmd() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -172,14 +175,11 @@ class test_simplecmd : public TestFixture CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret); } } - kill(pid,SIGKILL); } void SimpleException() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -224,17 +224,13 @@ class test_simplecmd : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret); - - kill(pid,SIGKILL); } } } void BigReturn() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -269,17 +265,13 @@ class test_simplecmd : public TestFixture string ret=dynamic_cast(rc.get_result())->get_data(); CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret); - - kill(pid,SIGKILL); } } } void BigParameter() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -314,8 +306,6 @@ class test_simplecmd : public TestFixture string ret=dynamic_cast(rc.get_result())->get_data(); CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret); - - kill(pid,SIGKILL); } } } diff --git a/test/timeout.cpp b/test/timeout.cpp index 6618ef6..bf32527 100644 --- a/test/timeout.cpp +++ b/test/timeout.cpp @@ -143,6 +143,8 @@ class test_timeout : public TestFixture CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: typedef uint32_t packet_size_indicator; @@ -151,7 +153,11 @@ class test_timeout : public TestFixture { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void send_hello(string hello_string, socket_server* ss, unsigned int conn_id) { @@ -178,9 +184,7 @@ class test_timeout : public TestFixture void ConnectTimeout() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -208,16 +212,16 @@ class test_timeout : public TestFixture socket_client_connection sc("./socket"); - CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed()); + CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed()); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong errormessage",string("no more retries left after connect error"),sc.get_last_error_msg()); } } } void HelloTimeoutNothing() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -244,30 +248,22 @@ class test_timeout : public TestFixture // wait till server is up sleep(1); socket_client_connection sc("./socket"); + command_client cc(&sc,1000000,1000000); - string errormsg; + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc,1000000,1000000); - } - catch(t2n_transfer_error &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void HelloTimeoutSlowData() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -307,30 +303,22 @@ class test_timeout : public TestFixture // wait till server is up sleep(1); socket_client_connection sc("./socket"); + command_client cc(&sc,1000000,1000000); - string errormsg; + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc,1000000,1000000); - } - catch(t2n_transfer_error &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void CommandTimeout() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -381,17 +369,13 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void CommandSlowResponse() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -457,17 +441,13 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void DisconnectOnWrite() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -511,17 +491,15 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg); - - kill(pid,SIGKILL); } } } void DisconnectOnRead() { - pid_t pid1, pid2; + pid_t pid2; - switch(pid1=fork()) + switch(child_pid=fork()) { case -1: { @@ -605,15 +583,14 @@ class test_timeout : public TestFixture } } } - kill(pid1,SIGKILL); kill(pid2,SIGKILL); } void BreakAccept() { - pid_t pid1,pid2; + pid_t pid2; - switch(pid1=fork()) + switch(child_pid=fork()) { case -1: { @@ -697,7 +674,6 @@ class test_timeout : public TestFixture } } } - kill(pid1,SIGKILL); kill(pid2,SIGKILL); } }; diff --git a/test/wrapper.cpp b/test/wrapper.cpp index c820531..8a7c16d 100644 --- a/test/wrapper.cpp +++ b/test/wrapper.cpp @@ -236,10 +236,6 @@ class test_wrapper : public TestFixture CPPUNIT_TEST(ignore_server_disconnect); CPPUNIT_TEST(ignore_handler_reconnects); -// TODO: missing tests: -// ignore: init, no server, ignore -// ignore: init, no server, ignore, server ok, connect? - CPPUNIT_TEST_SUITE_END(); public: @@ -421,8 +417,6 @@ class test_wrapper : public TestFixture CPPUNIT_ASSERT_EQUAL(2,cnt); } - - }; CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper); @@ -434,6 +428,7 @@ class test_wrapper_noserver : public TestFixture CPPUNIT_TEST(ignore_noserver); CPPUNIT_TEST(ignore_finds_lateserver); + CPPUNIT_TEST(ignore_wrongserver); CPPUNIT_TEST_SUITE_END(); @@ -442,10 +437,19 @@ class test_wrapper_noserver : public TestFixture pid_t child_pid; void setUp() - { } + { + child_pid=0; + } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + if (child_pid != 0) + { + kill(child_pid,SIGKILL); + sleep(1); + } + } void ignore_noserver() { @@ -472,7 +476,6 @@ class test_wrapper_noserver : public TestFixture t2n_exec(&cmd_group_x_client::serverfunc)(1); // launch a server - close_server=false; kill_server=false; @@ -516,10 +519,62 @@ class test_wrapper_noserver : public TestFixture int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); CPPUNIT_ASSERT_EQUAL(2,i); + } - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); + void send_hello(string hello_string, socket_server* ss, int conn_id) + { + server_connection *sc=ss->get_connection(conn_id); + sc->write(hello_string); + } + + void ignore_wrongserver() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // launch a server + + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + + // server sends garbage + + ostringstream hello; + hello << "XYZ 123"; + + ss.add_callback(new_connection,bind(&test_wrapper_noserver::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + + // max 10 sec + for (int i=0; i < 10; i++) + ss.fill_buffer(1000000); + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + } + } + + // there is no valid server + + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + // result is constructed with default constructor on error-and-ignore -> i=0 + + CPPUNIT_ASSERT_EQUAL(0,i); }