X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcomm.cpp;h=47ca385f13027f9d41cbe1dc689b942aaf9a93d5;hp=60907b206656a260a21dc1dcb067396e47698068;hb=441d41fe583765902aa2f9641c0977e295e62be3;hpb=07e98688a1a8c3e915ce923f79261a88251a9edd diff --git a/test/comm.cpp b/test/comm.cpp index 60907b2..47ca385 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -26,29 +27,39 @@ using namespace std; using namespace libt2n; using namespace CppUnit; + class test_comm : public TestFixture { CPPUNIT_TEST_SUITE(test_comm); CPPUNIT_TEST(UnixCommToServer); CPPUNIT_TEST(UnixCommToServerAndBack); + CPPUNIT_TEST(UnixCommToServerAndBackBig); + CPPUNIT_TEST(IPCommToServer); + CPPUNIT_TEST(IPCommToServerAndBack); + CPPUNIT_TEST(IPCommToServerAndBackBig); CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: void setUp() { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void UnixCommToServer() { - pid_t pid; string data; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -58,10 +69,17 @@ class test_comm : public TestFixture case 0: // child { - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); - sc.write("hello"); + 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); } @@ -71,8 +89,10 @@ class test_comm : public TestFixture { socket_server ss("./socket"); + time_t t0 = time(NULL); + // max 10 sec - for (int i=0; i < 10; i++) + while (time(NULL) < t0 + 10 ) { ss.fill_buffer(1000000); @@ -86,9 +106,84 @@ class test_comm : public TestFixture void UnixCommToServerAndBack() { - pid_t pid; + 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 ) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + + if (data=="QUIT") + break; + + 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); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + sc.write("ABC"); - switch(pid=fork()) + sc.fill_buffer(1000000); + sc.get_packet(data); + + CPPUNIT_ASSERT_EQUAL(string("DEF"),data); + + sc.write("HAHA"); + + sc.fill_buffer(1000000); + sc.get_packet(data); + + CPPUNIT_ASSERT_EQUAL(string("xyz"),data); + + sc.write("QUIT"); + } + } + } + + void UnixCommToServerAndBackBig() + { + switch(child_pid=fork()) { case -1: { @@ -98,29 +193,154 @@ class test_comm : public TestFixture case 0: // child { - socket_server ss("./socket"); - ss.set_logging(&cerr,debug); + try + { + socket_server ss("./socket"); + ss.set_logging(&cerr,debug); + + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + + if (data=="QUIT") + break; + + 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); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + sc.write(string().insert(0,100*1024,'x')); + + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); + + CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data); + + sc.write("QUIT"); + } + } + } + + void IPCommToServer() + { + string data; + + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + 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); + } + + default: + // parent + { + socket_server ss(6666); + + time_t t0 = time(NULL); // max 10 sec - for (int i=0; i < 10; i++) + while (time(NULL) < t0 + 10 ) { ss.fill_buffer(1000000); - string data; - unsigned int cid; + if(ss.get_packet(data)) + break; + } + } + } + CPPUNIT_ASSERT_EQUAL(string("hello"),data); + } + + void IPCommToServerAndBack() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss(6666); + ss.set_logging(&cerr,debug); - if(ss.get_packet(data,cid)) + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) { - server_connection* con=ss.get_connection(cid); + ss.fill_buffer(1000000); + + string data; + unsigned int 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); @@ -133,8 +353,7 @@ class test_comm : public TestFixture // wait till server is up sleep(1); - socket_client_connection sc("./socket"); - + socket_client_connection sc(6666); sc.write("ABC"); sc.fill_buffer(1000000); @@ -154,6 +373,75 @@ class test_comm : public TestFixture } } + + void IPCommToServerAndBackBig() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss(6666); + ss.set_logging(&cerr,debug); + + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + + socket_handler* alias= dynamic_cast< socket_handler* >(con); + + if (data=="QUIT") + break; + + 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); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc(6666); + + sc.write(string().insert(0,100*1024,'x')); + + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); + + CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data); + + sc.write("QUIT"); + } + } + } // eo IPCommToServerAndBackBig() }; CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);