X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcallback.cpp;h=eb600cb28008621e29b800b3472e6be998c8b227;hp=bc951b958fe0d838299ee4bce6ccb5a8cd70636b;hb=a63e08b83794273da3840e0b8bf15bf0085fe3c4;hpb=af84dfb53a739a0c8c343d9172f1847fa908906d diff --git a/test/callback.cpp b/test/callback.cpp index bc951b9..eb600cb 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 @@ -18,44 +33,22 @@ #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; - public: - - void setUp() - { - callback_done.resize(__events_end); - } - - void tearDown() - { - callback_done.clear(); - } - 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 @@ -65,19 +58,33 @@ class test_callback : public TestFixture callback_done[ev]=true; } - void ServerNewConnCallback() +public: + test_callbackFixture() { - pid_t pid; + callback_done.resize(__events_end); + } - switch(pid=fork()) + ~test_callbackFixture() + { + callback_done.clear(); + } +}; + +BOOST_FIXTURE_TEST_SUITE(test_callback, test_callbackFixture) + +BOOST_AUTO_TEST_CASE(ServerNewConnCallback) +{ + 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 { string data; // wait till server is up @@ -94,51 +101,55 @@ class test_callback : public TestFixture // 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()) { - pid_t pid; - - switch(pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { string data; // wait till server is up @@ -155,51 +166,55 @@ class test_callback : public TestFixture // 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()) { - pid_t pid; - - switch(pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { string data; // wait till server is up @@ -216,54 +231,58 @@ class test_callback : public TestFixture // 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()) { - pid_t pid; - - switch(pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { string data; // wait till server is up @@ -282,71 +301,75 @@ class test_callback : public TestFixture // 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()) { - pid_t pid; - - switch(pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -360,47 +383,52 @@ class test_callback : public TestFixture if(ss.get_packet(data,cid)) break; } - // don't call atexit and stuff - _exit(0); + } 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()) { - pid_t pid; - - switch(pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -414,35 +442,39 @@ class test_callback : public TestFixture if(ss.get_packet(data,cid)) break; } - // don't call atexit and stuff - _exit(0); + } 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()