X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcallback.cpp;h=eb600cb28008621e29b800b3472e6be998c8b227;hp=d3bdfa8e652350867ed4b98a79f0fdf0ea9f72e3;hb=307b5e74c506b609d5c407be0943f45255ab5122;hpb=3d9c0861e86edcbfad10b84da332093fd6438f32 diff --git a/test/callback.cpp b/test/callback.cpp index d3bdfa8..eb600cb 100644 --- a/test/callback.cpp +++ b/test/callback.cpp @@ -33,463 +33,448 @@ on this file might be covered by the GNU General Public License. #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #include +#include "test_fixtures.hxx" + using namespace std; using namespace libt2n; -using namespace CppUnit; -class test_callback : public TestFixture +class test_callbackFixture : public KillChildOnShutdownFixture { - CPPUNIT_TEST_SUITE(test_callback); - - CPPUNIT_TEST(ServerNewConnCallback); - CPPUNIT_TEST(ServerConnClosedCallback); - CPPUNIT_TEST(ServerConnDeletedCallback); - CPPUNIT_TEST(ServerCallbackOrder); - CPPUNIT_TEST(ClientConnClosedCallback); - CPPUNIT_TEST(ClientConnDeletedCallback); - - CPPUNIT_TEST_SUITE_END(); - +protected: std::vector callback_done; - pid_t child_pid; + void callback_func(callback_event_type ev, int conn_id) + { + // we don't care for the conn_id, we just mark the callback as done + if (callback_done[ev]) + throw runtime_error("callback already done for this event"); - public: + callback_done[ev]=true; + } - void setUp() +public: + test_callbackFixture() { callback_done.resize(__events_end); } - void tearDown() + ~test_callbackFixture() { 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) - { - // we don't care for the conn_id, we just mark the callback as done - if (callback_done[ev]) - throw runtime_error("callback already done for this event"); - - callback_done[ev]=true; - } +BOOST_FIXTURE_TEST_SUITE(test_callback, test_callbackFixture) - void ServerNewConnCallback() +BOOST_AUTO_TEST_CASE(ServerNewConnCallback) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - try - { - string data; - // wait till server is up - sleep(1); + string data; + // wait till server is up + sleep(1); - { - socket_client_connection sc("./socket"); + { + socket_client_connection sc("./socket"); - sc.write("ABC"); + sc.write("ABC"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection - } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; + // close the connection } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - socket_server ss("./socket"); + // don't call atexit and stuff + _exit(0); + } - ss.add_callback(new_connection,bind(&test_callback::callback_func, boost::ref(*this), new_connection, _1)); + default: + // parent + { + socket_server ss("./socket"); - // max 3 sec - for (int i=0; i < 3; i++) - { - ss.fill_buffer(1000000); + ss.add_callback(new_connection,bind(&test_callback::ServerNewConnCallback::callback_func, boost::ref(*this), new_connection, _1)); - string data; - unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); - con->write("XYZ"); - } + // 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)) + { + server_connection* con=ss.get_connection(cid); + con->write("XYZ"); } - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[new_connection])); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_deleted])); } + BOOST_CHECK_EQUAL(true,static_cast(callback_done[new_connection])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_closed])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_deleted])); } } +} - void ServerConnClosedCallback() +BOOST_AUTO_TEST_CASE(ServerConnClosedCallback) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - try - { - string data; - // wait till server is up - sleep(1); + string data; + // wait till server is up + sleep(1); - { - socket_client_connection sc("./socket"); + { + socket_client_connection sc("./socket"); - sc.write("ABC"); + sc.write("ABC"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection - } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; + // close the connection } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - socket_server ss("./socket"); + // don't call atexit and stuff + _exit(0); + } - ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1)); + default: + // parent + { + socket_server ss("./socket"); - // max 3 sec - for (int i=0; i < 3; i++) - { - ss.fill_buffer(1000000); + ss.add_callback(connection_closed,bind(&test_callback::ServerConnClosedCallback::callback_func, boost::ref(*this), connection_closed, _1)); - string data; - unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); - con->write("XYZ"); - } + // 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)) + { + server_connection* con=ss.get_connection(cid); + con->write("XYZ"); } - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[new_connection])); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_deleted])); } + BOOST_CHECK_EQUAL(false,static_cast(callback_done[new_connection])); + BOOST_CHECK_EQUAL(true,static_cast(callback_done[connection_closed])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_deleted])); } } +} - void ServerConnDeletedCallback() +BOOST_AUTO_TEST_CASE(ServerConnDeletedCallback) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - try - { - string data; - // wait till server is up - sleep(1); + string data; + // wait till server is up + sleep(1); - { - socket_client_connection sc("./socket"); + { + socket_client_connection sc("./socket"); - sc.write("ABC"); + sc.write("ABC"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - // close the connection - } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; + // close the connection } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - socket_server ss("./socket"); + // don't call atexit and stuff + _exit(0); + } - ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1)); + default: + // parent + { + socket_server ss("./socket"); - // max 3 sec - for (int i=0; i < 3; i++) - { - ss.fill_buffer(1000000); + ss.add_callback(connection_deleted,bind(&test_callback::ServerConnDeletedCallback::callback_func, boost::ref(*this), connection_deleted, _1)); - string data; - unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); - con->write("XYZ"); - } - ss.cleanup(); + // 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)) + { + server_connection* con=ss.get_connection(cid); + con->write("XYZ"); } ss.cleanup(); - - 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])); } + ss.cleanup(); + + BOOST_CHECK_EQUAL(false,static_cast(callback_done[new_connection])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_closed])); + BOOST_CHECK_EQUAL(true,static_cast(callback_done[connection_deleted])); } } +} - void ServerCallbackOrder() +BOOST_AUTO_TEST_CASE(ServerCallbackOrder) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - try - { - string data; - // wait till server is up - sleep(1); + string data; + // wait till server is up + sleep(1); - { - socket_client_connection sc("./socket"); + { + socket_client_connection sc("./socket"); - sc.write("1"); + sc.write("1"); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); - sc.write("2"); + sc.write("2"); - // close the connection - } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; + // close the connection } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - socket_server ss("./socket"); + // don't call atexit and stuff + _exit(0); + } - ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1)); - ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1)); + default: + // parent + { + socket_server ss("./socket"); - bool got_1=false; + ss.add_callback(connection_closed,bind(&test_callback::ServerCallbackOrder::callback_func, boost::ref(*this), connection_closed, _1)); + ss.add_callback(connection_deleted,bind(&test_callback::ServerCallbackOrder::callback_func, boost::ref(*this), connection_deleted, _1)); - for (int i=0; i < 5; i++) - { - ss.fill_buffer(500000); + bool got_1=false; - string data; - unsigned int cid; - if(!got_1 && ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); - con->write("XYZ"); - got_1=true; - // don't call get_packet anymore - } - ss.cleanup(); + for (int i=0; i < 5; i++) + { + ss.fill_buffer(500000); + + string data; + unsigned int cid; + if(!got_1 && ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + con->write("XYZ"); + got_1=true; + // don't call get_packet anymore } ss.cleanup(); + } + ss.cleanup(); - 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])); + BOOST_CHECK_MESSAGE(static_cast(callback_done[connection_closed]) == true, "closed done"); + BOOST_CHECK_MESSAGE(static_cast(callback_done[connection_deleted]) == false, "not deleted yet"); - for (int i=0; i < 4; i++) - { - ss.fill_buffer(500000); + for (int i=0; i < 4; i++) + { + ss.fill_buffer(500000); - string data; - unsigned int cid; - ss.get_packet(data,cid); - ss.cleanup(); - } + string data; + unsigned int cid; + ss.get_packet(data,cid); 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])); } + ss.cleanup(); + + BOOST_CHECK_MESSAGE(static_cast(callback_done[connection_closed]) == true, "closed done (2)"); + BOOST_CHECK_MESSAGE(static_cast(callback_done[connection_deleted]) == true, "not deleted yet(2)"); } } +} - void ClientConnClosedCallback() +BOOST_AUTO_TEST_CASE(ClientConnClosedCallback) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - 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(...) + socket_server ss("./socket"); + + // max 3 sec + for (int i=0; i < 3; i++) { - std::cerr << "exception in child. ignoring\n"; - } + ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + string data; + unsigned int cid; + if(ss.get_packet(data,cid)) + break; + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; - // wait till server is up - sleep(1); + // don't call atexit and stuff + _exit(0); + } - socket_client_connection sc("./socket"); + default: + // parent + { + string data; + // wait till server is up + sleep(1); - sc.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, 0)); + socket_client_connection sc("./socket"); - sc.write("ABC"); + sc.add_callback(connection_closed,bind(&test_callback::ClientConnClosedCallback::callback_func, boost::ref(*this), connection_closed, 0)); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); + sc.write("ABC"); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[new_connection])); - CPPUNIT_ASSERT_EQUAL(true,static_cast(callback_done[connection_closed])); - CPPUNIT_ASSERT_EQUAL(false,static_cast(callback_done[connection_deleted])); - } + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); + + BOOST_CHECK_EQUAL(false,static_cast(callback_done[new_connection])); + BOOST_CHECK_EQUAL(true,static_cast(callback_done[connection_closed])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_deleted])); } } +} - void ClientConnDeletedCallback() +BOOST_AUTO_TEST_CASE(ClientConnDeletedCallback) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - 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(...) + socket_server ss("./socket"); + + // max 3 sec + for (int i=0; i < 3; i++) { - std::cerr << "exception in child. ignoring\n"; - } + ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + string data; + unsigned int cid; + if(ss.get_packet(data,cid)) + break; + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; - // wait till server is up - sleep(1); + // don't call atexit and stuff + _exit(0); + } - { - socket_client_connection sc("./socket"); + default: + // parent + { + string data; + // wait till server is up + sleep(1); - sc.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, 0)); + { + socket_client_connection sc("./socket"); - sc.write("ABC"); + sc.add_callback(connection_deleted,bind(&test_callback::ClientConnDeletedCallback::callback_func, boost::ref(*this), connection_deleted, 0)); - // wait half a sec - sc.fill_buffer(500000); - sc.get_packet(data); - } + sc.write("ABC"); - 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])); + // wait half a sec + sc.fill_buffer(500000); + sc.get_packet(data); } + + BOOST_CHECK_EQUAL(false,static_cast(callback_done[new_connection])); + BOOST_CHECK_EQUAL(false,static_cast(callback_done[connection_closed])); + BOOST_CHECK_EQUAL(true,static_cast(callback_done[connection_deleted])); } } -}; +} -CPPUNIT_TEST_SUITE_REGISTRATION(test_callback); +BOOST_AUTO_TEST_SUITE_END()