X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcallback.cpp;h=d3bdfa8e652350867ed4b98a79f0fdf0ea9f72e3;hp=beb0c777db059f3dd9a054d8342e9eac1f04072e;hb=3d9c0861e86edcbfad10b84da332093fd6438f32;hpb=6cda58a6dad87ff6efe2277db2155be60edb8d48 diff --git a/test/callback.cpp b/test/callback.cpp index beb0c77..d3bdfa8 100644 --- a/test/callback.cpp +++ b/test/callback.cpp @@ -1,9 +1,24 @@ -/*************************************************************************** - * Copyright (C) 2004 by Intra2net AG * - * info@intra2net.com * - * * - ***************************************************************************/ +/* +Copyright (C) 2004 by Intra2net AG +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ #include #include #include @@ -33,15 +48,19 @@ class test_callback : public TestFixture { CPPUNIT_TEST_SUITE(test_callback); - CPPUNIT_TEST(NewConnCallback); - CPPUNIT_TEST(ConnClosedCallback); - CPPUNIT_TEST(ConnDeletedCallback); - CPPUNIT_TEST(CallbackOrder); + CPPUNIT_TEST(ServerNewConnCallback); + CPPUNIT_TEST(ServerConnClosedCallback); + CPPUNIT_TEST(ServerConnDeletedCallback); + CPPUNIT_TEST(ServerCallbackOrder); + CPPUNIT_TEST(ClientConnClosedCallback); + CPPUNIT_TEST(ClientConnDeletedCallback); CPPUNIT_TEST_SUITE_END(); std::vector callback_done; + pid_t child_pid; + public: void setUp() @@ -52,6 +71,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) @@ -63,11 +86,9 @@ class test_callback : public TestFixture callback_done[ev]=true; } - void NewConnCallback() + void ServerNewConnCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -77,20 +98,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"); - // close the connection + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); + + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -124,11 +151,9 @@ class test_callback : public TestFixture } } - void ConnClosedCallback() + void ServerConnClosedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -138,20 +163,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"); - // close the connection + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); + + // close the connection + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -185,11 +216,9 @@ class test_callback : public TestFixture } } - void ConnDeletedCallback() + void ServerConnDeletedCallback() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -199,20 +228,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 @@ -249,11 +284,9 @@ class test_callback : public TestFixture } } - void CallbackOrder() + void ServerCallbackOrder() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -263,22 +296,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 @@ -295,7 +334,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); @@ -312,8 +351,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++) { @@ -326,12 +365,131 @@ class test_callback : public TestFixture } ss.cleanup(); + 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() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss("./socket"); + + // 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); + } + + default: + // parent + { + string data; + // wait till server is up + sleep(1); + + socket_client_connection sc("./socket"); + + sc.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, 0)); + + sc.write("ABC"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); + + CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[new_connection])); CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_deleted])); + CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_deleted])); } } } + void ClientConnDeletedCallback() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss("./socket"); + + // 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); + } + + default: + // parent + { + string data; + // wait till server is up + sleep(1); + + { + socket_client_connection sc("./socket"); + + sc.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, 0)); + + sc.write("ABC"); + + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); + } + + CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[new_connection])); + CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_closed])); + CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_deleted])); + } + } + } }; CPPUNIT_TEST_SUITE_REGISTRATION(test_callback);