X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcallback.cpp;h=60c9058a63367ca290b45e41a676fafdde8b64d9;hp=9d4c7d1f791d2f5f000eb0900591501051fed6e7;hb=0995cebb9189b872e8d7e58d8b8122fa00cc9061;hpb=91730468439e21dcf8d275d0f70d803c20ccaa7c diff --git a/test/callback.cpp b/test/callback.cpp index 9d4c7d1..60c9058 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: { @@ -79,20 +83,26 @@ class test_callback : public TestFixture case 0: // child { - string data; - // wait till server is up - sleep(1); - + try { - socket_client_connection sc("./socket"); + string data; + // wait till server is up + sleep(1); - sc.write("ABC"); + { + socket_client_connection sc("./socket"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + sc.write("ABC"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -128,9 +138,7 @@ class test_callback : public TestFixture void ServerConnClosedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -140,20 +148,26 @@ class test_callback : public TestFixture case 0: // child { - string data; - // wait till server is up - sleep(1); - + try { - socket_client_connection sc("./socket"); + string data; + // wait till server is up + sleep(1); - sc.write("ABC"); + { + socket_client_connection sc("./socket"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + sc.write("ABC"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -189,9 +203,7 @@ class test_callback : public TestFixture void ServerConnDeletedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -201,20 +213,26 @@ class test_callback : public TestFixture case 0: // child { - string data; - // wait till server is up - sleep(1); - + try { - socket_client_connection sc("./socket"); + string data; + // wait till server is up + sleep(1); - sc.write("ABC"); + { + socket_client_connection sc("./socket"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + sc.write("ABC"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -253,9 +271,7 @@ class test_callback : public TestFixture void ServerCallbackOrder() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -265,22 +281,28 @@ class test_callback : public TestFixture case 0: // child { - string data; - // wait till server is up - sleep(1); - + try { - socket_client_connection sc("./socket"); + string data; + // wait till server is up + sleep(1); - sc.write("1"); + { + socket_client_connection sc("./socket"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + sc.write("1"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - sc.write("2"); + sc.write("2"); - // close the connection + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -297,7 +319,7 @@ class test_callback : public TestFixture bool got_1=false; - for (int i=0; i < 4; i++) + for (int i=0; i < 5; i++) { ss.fill_buffer(500000); @@ -314,8 +336,8 @@ class test_callback : public TestFixture } ss.cleanup(); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_deleted])); + CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done",true,static_cast(callback_done[connection_closed])); + CPPUNIT_ASSERT_EQUAL_MESSAGE("not deleted yet",false,static_cast(callback_done[connection_deleted])); for (int i=0; i < 4; i++) { @@ -328,17 +350,15 @@ class test_callback : public TestFixture } ss.cleanup(); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_deleted])); + CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done (2)",true,static_cast(callback_done[connection_closed])); + CPPUNIT_ASSERT_EQUAL_MESSAGE("deleted done",true,static_cast(callback_done[connection_deleted])); } } } void ClientConnClosedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -348,18 +368,25 @@ class test_callback : public TestFixture case 0: // child { - socket_server ss("./socket"); - - // max 3 sec - for (int i=0; i < 3; i++) + try { - ss.fill_buffer(1000000); + socket_server ss("./socket"); - string data; - unsigned int cid; - if(ss.get_packet(data,cid)) - break; + // max 3 sec + for (int i=0; i < 3; i++) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + if(ss.get_packet(data,cid)) + break; + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + // don't call atexit and stuff _exit(0); } @@ -390,9 +417,7 @@ class test_callback : public TestFixture void ClientConnDeletedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -402,18 +427,25 @@ class test_callback : public TestFixture case 0: // child { - socket_server ss("./socket"); - - // max 3 sec - for (int i=0; i < 3; i++) + try { - ss.fill_buffer(1000000); + socket_server ss("./socket"); - string data; - unsigned int cid; - if(ss.get_packet(data,cid)) - break; + // max 3 sec + for (int i=0; i < 3; i++) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + if(ss.get_packet(data,cid)) + break; + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + // don't call atexit and stuff _exit(0); }