X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcomm.cpp;h=7b301a6bd2e67f7d981d365b9a8063d137930e1a;hp=f9c58e313301c6c2ae60c99ca8a3eba47fd773bb;hb=307b5e74c506b609d5c407be0943f45255ab5122;hpb=3d9c0861e86edcbfad10b84da332093fd6438f32 diff --git a/test/comm.cpp b/test/comm.cpp index f9c58e3..7b301a6 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -31,78 +31,84 @@ on this file might be covered by the GNU General Public License. #include #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; +BOOST_FIXTURE_TEST_SUITE(test_comm, KillChildOnShutdownFixture) -class test_comm : public TestFixture +BOOST_AUTO_TEST_CASE(UnixCommToServer) { - CPPUNIT_TEST_SUITE(test_comm); + string data; - CPPUNIT_TEST(UnixCommToServer); - CPPUNIT_TEST(UnixCommToServerAndBack); - CPPUNIT_TEST(UnixCommToServerAndBackBig); - CPPUNIT_TEST(IPCommToServer); - CPPUNIT_TEST(IPCommToServerAndBack); - CPPUNIT_TEST(IPCommToServerAndBackBig); + switch(child_pid=fork()) + { + case -1: + { + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + sc.write("hello"); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - CPPUNIT_TEST_SUITE_END(); + // don't call atexit and stuff + _exit(0); + } - pid_t child_pid; + default: + // parent + { + socket_server ss("./socket"); - public: + time_t t0 = time(NULL); - void setUp() - { } + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - void tearDown() - { - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); + if(ss.get_packet(data)) + break; + } + } } + BOOST_CHECK_EQUAL(string("hello"),data); +} - void UnixCommToServer() +BOOST_AUTO_TEST_CASE(UnixCommToServerAndBack) +{ + switch(child_pid=fork()) { - string data; - - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child - { - try - { - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); - sc.write("hello"); - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; - } - - // don't call atexit and stuff - _exit(0); - } - - default: - // parent + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); + ss.set_logging(&cerr,debug); time_t t0 = time(NULL); @@ -111,352 +117,319 @@ class test_comm : public TestFixture { ss.fill_buffer(1000000); - if(ss.get_packet(data)) - break; - } - } - } - CPPUNIT_ASSERT_EQUAL(string("hello"),data); - } + string data; + unsigned int cid; - void UnixCommToServerAndBack() - { - switch(child_pid=fork()) - { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child - { - try - { - socket_server ss("./socket"); - ss.set_logging(&cerr,debug); - - time_t t0 = time(NULL); - - // max 10 sec - while (time(NULL) < t0 + 10 ) + if(ss.get_packet(data,cid)) { - ss.fill_buffer(1000000); - - string data; - unsigned int cid; + server_connection* con=ss.get_connection(cid); - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if (data=="QUIT") + break; - if (data=="QUIT") - break; - - if (data=="ABC") - con->write("DEF"); - else - con->write("xyz"); - } + if (data=="ABC") + con->write("DEF"); + else + con->write("xyz"); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + string data; - sc.write("ABC"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - sc.fill_buffer(1000000); - sc.get_packet(data); + sc.write("ABC"); - CPPUNIT_ASSERT_EQUAL(string("DEF"),data); + sc.fill_buffer(1000000); + sc.get_packet(data); - sc.write("HAHA"); + BOOST_CHECK_EQUAL(string("DEF"),data); - sc.fill_buffer(1000000); - sc.get_packet(data); + sc.write("HAHA"); - CPPUNIT_ASSERT_EQUAL(string("xyz"),data); + sc.fill_buffer(1000000); + sc.get_packet(data); - sc.write("QUIT"); - } + BOOST_CHECK_EQUAL(string("xyz"),data); + + sc.write("QUIT"); } } +} - void UnixCommToServerAndBackBig() +BOOST_AUTO_TEST_CASE(UnixCommToServerAndBackBig) +{ + 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"); - ss.set_logging(&cerr,debug); + socket_server ss("./socket"); + ss.set_logging(&cerr,debug); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - con->write(string().insert(0,100*1024,'y')); - } + con->write(string().insert(0,100*1024,'y')); } - std::cerr << "child: OVER" << std::endl; - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // don't call atexit and stuff - _exit(0); + std::cerr << "child: OVER" << std::endl; + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + string data; - sc.write(string().insert(0,100*1024,'x')); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + sc.write(string().insert(0,100*1024,'x')); - CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data); + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); - sc.write("QUIT"); - } + BOOST_CHECK_EQUAL(string().insert(0,100*1024,'y'),data); + + sc.write("QUIT"); } } +} - void IPCommToServer() - { - string data; +BOOST_AUTO_TEST_CASE(IPCommToServer) +{ + string data; - switch(child_pid=fork()) + switch(child_pid=fork()) + { + case -1: { - case -1: + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + // wait till server is up + sleep(1); + socket_client_connection sc(6666); + sc.write("hello"); + } catch(...) { - try - { - // wait till server is up - sleep(1); - socket_client_connection sc(6666); - sc.write("hello"); - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; - } - - // don't call atexit and stuff - _exit(0); + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - socket_server ss(6666); + // don't call atexit and stuff + _exit(0); + } - time_t t0 = time(NULL); + default: + // parent + { + socket_server ss(6666); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + time_t t0 = time(NULL); - if(ss.get_packet(data)) - break; - } + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); + + if(ss.get_packet(data)) + break; } } - CPPUNIT_ASSERT_EQUAL(string("hello"),data); } + BOOST_CHECK_EQUAL(string("hello"),data); +} - void IPCommToServerAndBack() +BOOST_AUTO_TEST_CASE(IPCommToServerAndBack) +{ + 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(6666); - ss.set_logging(&cerr,debug); + socket_server ss(6666); + ss.set_logging(&cerr,debug); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - if (data=="ABC") - con->write("DEF"); - else - con->write("xyz"); - } + if (data=="ABC") + con->write("DEF"); + else + con->write("xyz"); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + // don't call atexit and stuff + _exit(0); + } - default: - // parent - { - string data; + default: + // parent + { + string data; - // wait till server is up - sleep(1); - socket_client_connection sc(6666); - sc.write("ABC"); + // wait till server is up + sleep(1); + socket_client_connection sc(6666); + sc.write("ABC"); - sc.fill_buffer(1000000); - sc.get_packet(data); + sc.fill_buffer(1000000); + sc.get_packet(data); - CPPUNIT_ASSERT_EQUAL(string("DEF"),data); + BOOST_CHECK_EQUAL(string("DEF"),data); - sc.write("HAHA"); + sc.write("HAHA"); - sc.fill_buffer(1000000); - sc.get_packet(data); + sc.fill_buffer(1000000); + sc.get_packet(data); - CPPUNIT_ASSERT_EQUAL(string("xyz"),data); + BOOST_CHECK_EQUAL(string("xyz"),data); - sc.write("QUIT"); - } + sc.write("QUIT"); } } +} - void IPCommToServerAndBackBig() +BOOST_AUTO_TEST_CASE(IPCommToServerAndBackBig) +{ + 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(6666); - ss.set_logging(&cerr,debug); + socket_server ss(6666); + ss.set_logging(&cerr,debug); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - socket_handler* alias= dynamic_cast< socket_handler* >(con); + socket_handler* alias= dynamic_cast< socket_handler* >(con); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - alias->set_write_block_size( 4093 ); - con->write(string().insert(0,2048*1024,'y')); - } + alias->set_write_block_size( 4093 ); + con->write(string().insert(0,2048*1024,'y')); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + // don't call atexit and stuff + _exit(0); + } - default: - // parent - { - string data; + default: + // parent + { + string data; - // wait till server is up - sleep(1); - socket_client_connection sc(6666); + // wait till server is up + sleep(1); + socket_client_connection sc(6666); - sc.write(string().insert(0,100*1024,'x')); + sc.write(string().insert(0,100*1024,'x')); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); - CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data); + BOOST_CHECK_EQUAL(string().insert(0,2048*1024,'y'),data); - sc.write("QUIT"); - } + sc.write("QUIT"); } - } // eo IPCommToServerAndBackBig() -}; + } +} // eo IPCommToServerAndBackBig() -CPPUNIT_TEST_SUITE_REGISTRATION(test_comm); +BOOST_AUTO_TEST_SUITE_END()