X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Ftimeout.cpp;h=3658d6df3c14ed9f23f727cecd1a4e93905723b1;hp=6618ef6b6719b641b382d38e1a2ea3e60789b08a;hb=441d41fe583765902aa2f9641c0977e295e62be3;hpb=fb3345ada7ea94225b78994fd50e3de693a2a3d5 diff --git a/test/timeout.cpp b/test/timeout.cpp index 6618ef6..3658d6d 100644 --- a/test/timeout.cpp +++ b/test/timeout.cpp @@ -138,11 +138,14 @@ class test_timeout : public TestFixture CPPUNIT_TEST(CommandTimeout); CPPUNIT_TEST(CommandSlowResponse); CPPUNIT_TEST(DisconnectOnWrite); + CPPUNIT_TEST(WriteTwice); CPPUNIT_TEST(DisconnectOnRead); CPPUNIT_TEST(BreakAccept); CPPUNIT_TEST_SUITE_END(); + pid_t child_pid; + public: typedef uint32_t packet_size_indicator; @@ -151,7 +154,11 @@ class test_timeout : public TestFixture { } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } void send_hello(string hello_string, socket_server* ss, unsigned int conn_id) { @@ -178,9 +185,7 @@ class test_timeout : public TestFixture void ConnectTimeout() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -190,7 +195,13 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } // don't call atexit and stuff _exit(0); @@ -208,16 +219,16 @@ class test_timeout : public TestFixture socket_client_connection sc("./socket"); - CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed()); + CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed()); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong errormessage",string("no more retries left after connect error"),sc.get_last_error_msg()); } } } void HelloTimeoutNothing() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -227,11 +238,18 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + + // max 10 sec + for (int i=0; i < 10; i++) + ss.fill_buffer(1000000); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -244,30 +262,22 @@ class test_timeout : public TestFixture // wait till server is up sleep(1); socket_client_connection sc("./socket"); + command_client cc(&sc,1000000,1000000); - string errormsg; + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc,1000000,1000000); - } - catch(t2n_transfer_error &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void HelloTimeoutSlowData() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -277,24 +287,31 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - // create a valid packet - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION << ';'; - int byteordercheck=1; - hello.write((char*)&byteordercheck,sizeof(byteordercheck)); - hello << ';'; + // create a valid packet + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + int byteordercheck=1; + hello.write((char*)&byteordercheck,sizeof(byteordercheck)); + hello << ';'; - packet_size_indicator psize=htonl(hello.str().size()); - std::string send_data(hello.str()); - send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); + packet_size_indicator psize=htonl(hello.str().size()); + std::string send_data(hello.str()); + send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); - ss.add_callback(new_connection,bind(&test_timeout::send_slow_raw_socket, boost::ref(*this), send_data,&ss, _1)); + ss.add_callback(new_connection,bind(&test_timeout::send_slow_raw_socket, boost::ref(*this), send_data,&ss, _1)); + + // max 10 sec + for (int i=0; i < 10; i++) + ss.fill_buffer(1000000); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -307,30 +324,22 @@ class test_timeout : public TestFixture // wait till server is up sleep(1); socket_client_connection sc("./socket"); + command_client cc(&sc,1000000,1000000); - string errormsg; + t2n_exception* ep=cc.get_constuctor_exception(); - try - { - command_client cc(&sc,1000000,1000000); - } - catch(t2n_transfer_error &e) - { errormsg=e.what(); } - catch(...) - { throw; } + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void CommandTimeout() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -340,19 +349,26 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION << ';'; - int byteordercheck=1; - hello.write((char*)&byteordercheck,sizeof(byteordercheck)); - hello << ';'; + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + int byteordercheck=1; + hello.write((char*)&byteordercheck,sizeof(byteordercheck)); + hello << ';'; - ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + + // max 10 sec + for (int i=0; i < 10; i++) + ss.fill_buffer(1000000); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -381,17 +397,13 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void CommandSlowResponse() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -401,34 +413,41 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION << ';'; - int byteordercheck=1; - hello.write((char*)&byteordercheck,sizeof(byteordercheck)); - hello << ';'; + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + int byteordercheck=1; + hello.write((char*)&byteordercheck,sizeof(byteordercheck)); + hello << ';'; - ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1)); - // max 10 sec - for (int i=0; i < 10; i++) - { - ss.fill_buffer(1000000); + // max 10 sec + for (int i=0; i < 10; i++) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - // create a valid packet & send - string response="abcdefghijklmnopqrstuvwxyz"; - packet_size_indicator psize=htonl(response.size()); - std::string send_data(response); - send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); - send_slow_raw_socket(send_data,&ss,cid); + if(ss.get_packet(data,cid)) + { + // create a valid packet & send + string response="abcdefghijklmnopqrstuvwxyz"; + packet_size_indicator psize=htonl(response.size()); + std::string send_data(response); + send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); + send_slow_raw_socket(send_data,&ss,cid); + } } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + // don't call atexit and stuff _exit(0); } @@ -457,17 +476,13 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg); - - kill(pid,SIGKILL); } } } void DisconnectOnWrite() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -477,10 +492,17 @@ class test_timeout : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + + // bail out as soon as we get something + ss.fill_buffer(-1); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - // bail out as soon as we get something - ss.fill_buffer(-1); // don't call atexit and stuff _exit(0); } @@ -499,7 +521,7 @@ class test_timeout : public TestFixture string errormsg; - string huge(1000000,'x'); + string huge(5000000,'x'); try { @@ -511,17 +533,76 @@ class test_timeout : public TestFixture { throw; } CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg); + } + } + } + + void WriteTwice() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss("./socket"); + + // bail out as soon as we get something + ss.fill_buffer(-1); + ss.fill_buffer(-1); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; + + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + string errormsg; + + sc.write("somedata"); + + sleep(1); + + // server should disconnect now + try + { + sc.write("other data"); + } + catch(t2n_transfer_error &e) + { errormsg=e.what(); } + catch(...) + { throw; } - kill(pid,SIGKILL); + CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg); } } } void DisconnectOnRead() { - pid_t pid1, pid2; + pid_t pid2; - switch(pid1=fork()) + switch(child_pid=fork()) { case -1: { @@ -531,14 +612,20 @@ class test_timeout : public TestFixture case 0: // child { - // wait till server is up - sleep(1); + try + { + // wait till server is up + sleep(1); - socket_client_connection sc("./socket"); + socket_client_connection sc("./socket"); - // this is an evil hack to get access to real_write, don't ever do this in an app!!! - real_write_client_connection *rwc=(real_write_client_connection*)≻ - rwc->real_write(string(10000,'x')); + // this is an evil hack to get access to real_write, don't ever do this in an app!!! + real_write_client_connection *rwc=(real_write_client_connection*)≻ + rwc->real_write(string(10000,'x')); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } // don't call atexit and stuff _exit(0); @@ -575,10 +662,16 @@ class test_timeout : public TestFixture case 0: // child { - socket_client_connection *sc=new socket_client_connection("./socket"); - sc->write(string(10000,'x')); - delete sc; - // socket is closed regularly + try + { + socket_client_connection *sc=new socket_client_connection("./socket"); + sc->write(string(10000,'x')); + // socket is closed regularly + delete sc; + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } // don't run regular cleanup, otherwise cppunit stuff gets called _exit(0); @@ -605,15 +698,14 @@ class test_timeout : public TestFixture } } } - kill(pid1,SIGKILL); kill(pid2,SIGKILL); } void BreakAccept() { - pid_t pid1,pid2; + pid_t pid2; - switch(pid1=fork()) + switch(child_pid=fork()) { case -1: { @@ -623,11 +715,17 @@ class test_timeout : public TestFixture case 0: // child { - // wait till server is really up and waiting - sleep(2); + try + { + // wait till server is really up and waiting + sleep(2); - // connect with very tight timeout and only 1 retry - socket_client_connection sc("./socket",50,1); + // connect with very tight timeout and only 1 retry + socket_client_connection sc("./socket",50,1); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } // don't call atexit and stuff _exit(0); @@ -667,10 +765,16 @@ class test_timeout : public TestFixture case 0: // child { - socket_client_connection *sc=new socket_client_connection("./socket"); - sc->write(string(10000,'x')); - delete sc; - // socket is closed regularly + try + { + socket_client_connection *sc=new socket_client_connection("./socket"); + sc->write(string(10000,'x')); + delete sc; + // socket is closed regularly + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } // don't run regular cleanup, otherwise cppunit stuff gets called _exit(0); @@ -697,7 +801,6 @@ class test_timeout : public TestFixture } } } - kill(pid1,SIGKILL); kill(pid2,SIGKILL); } };