X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fwrapper.cpp;h=17fb0ef36cd3581fa32785c016a42742cb203798;hp=062f9b47451bd1b4096216895c7ee24cd8f0fe5c;hb=0995cebb9189b872e8d7e58d8b8122fa00cc9061;hpb=e1614a6d1d9c022b83c0aa8cdb948bd7dc98ff23 diff --git a/test/wrapper.cpp b/test/wrapper.cpp index 062f9b4..17fb0ef 100644 --- a/test/wrapper.cpp +++ b/test/wrapper.cpp @@ -188,7 +188,7 @@ BOOST_CLASS_EXPORT(getserverlog_cmd) class cmd_group_x_client : public command_client { public: - cmd_group_x_client(libt2n::client_connection &_c, + cmd_group_x_client(libt2n::client_connection *_c, long long _command_timeout_usec=command_timeout_usec_default, long long _hello_timeout_usec=hello_timeout_usec_default) : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec) @@ -233,21 +233,21 @@ class test_wrapper : public TestFixture CPPUNIT_TEST(double_use_with_close); CPPUNIT_TEST(reconnect_after_close); CPPUNIT_TEST(reconnect_not_possible); - -// TODO: missing tests: -// ignore: init, use, server die, ignore -// ignore: init, no server, ignore -// ignore: init, no server, ignore, server ok, connect? + CPPUNIT_TEST(ignore_server_disconnect); + CPPUNIT_TEST(ignore_handler_reconnects); CPPUNIT_TEST_SUITE_END(); public: + pid_t child_pid; + void setUp() { - pid_t pid; + close_server=false; + kill_server=false; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -257,18 +257,24 @@ class test_wrapper : public TestFixture case 0: // child { - int i=0; - while(i < 15 && !kill_server) + try { - close_server=false; - - socket_server ss("./socket"); - group_command_server cs(ss); - ss.set_logging(&logstream,debug); - - // max 10 sec - for (; !close_server && !kill_server && i < 15; i++) - cs.handle(1000000); + int i=0; + while(i < 10 && !kill_server) + { + close_server=false; + + socket_server ss("./socket"); + group_command_server cs(ss); + ss.set_logging(&logstream,debug); + + // max 10 sec + for (; !close_server && !kill_server && i < 10; i++) + cs.handle(1000000); + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -286,7 +292,16 @@ class test_wrapper : public TestFixture } void tearDown() - { } + { + // make sure the server-child is dead before the next test runs + kill(child_pid,SIGKILL); + sleep(1); + } + + void no_init_exception() + { + CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); + } void simple_wrap() { @@ -298,11 +313,6 @@ class test_wrapper : public TestFixture CPPUNIT_ASSERT_EQUAL(2,i); } - void no_init_exception() - { - CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); - } - void double_use() { // only one connection used? @@ -376,7 +386,217 @@ class test_wrapper : public TestFixture CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error); } + void ignore_server_disconnect() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // the server doens't like the beast + t2n_exec(&cmd_group_x_client::serverfunc)(666); + + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + // result is constructed with default constructor on error-and-ignore -> i=0 + + CPPUNIT_ASSERT_EQUAL(0,i); + } + + void ignore_handler_reconnects() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); + wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); + + // 42 closes connection on the server side + t2n_exec(&cmd_group_x_client::serverfunc)(42); + + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); + + // count the number of times that "new connection accepted" appears in the server log + string::size_type p=0; + int cnt=0; + while ((p=out.find("new connection accepted",p))++ != string::npos) + cnt++; + + CPPUNIT_ASSERT_EQUAL(2,cnt); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper); + + +class test_wrapper_noserver : public TestFixture +{ + CPPUNIT_TEST_SUITE(test_wrapper_noserver); + + CPPUNIT_TEST(ignore_noserver); + CPPUNIT_TEST(ignore_finds_lateserver); + CPPUNIT_TEST(ignore_wrongserver); + + CPPUNIT_TEST_SUITE_END(); + + public: + + pid_t child_pid; + + void setUp() + { + child_pid=0; + } + + void tearDown() + { + // make sure the server-child is dead before the next test runs + if (child_pid != 0) + { + kill(child_pid,SIGKILL); + sleep(1); + } + } + + void ignore_noserver() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // wraptype::get_connection_wrapper()->set_logging(&cerr,debug); + + // there is no server + + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + // result is constructed with default constructor on error-and-ignore -> i=0 + + CPPUNIT_ASSERT_EQUAL(0,i); + } + + void ignore_finds_lateserver() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // there is no server + t2n_exec(&cmd_group_x_client::serverfunc)(1); + + // launch a server + close_server=false; + kill_server=false; + + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + int i=0; + while(i < 10 && !kill_server) + { + close_server=false; + + socket_server ss("./socket"); + group_command_server cs(ss); + ss.set_logging(&logstream,debug); + + // max 10 sec + for (; !close_server && !kill_server && i < 10; i++) + cs.handle(1000000); + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + } + } + + // server should be active + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + CPPUNIT_ASSERT_EQUAL(2,i); + } + + void send_hello(string hello_string, socket_server* ss, int conn_id) + { + server_connection *sc=ss->get_connection(conn_id); + sc->write(hello_string); + } + + void ignore_wrongserver() + { + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // launch a server + + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss("./socket"); + + // server sends garbage + + ostringstream hello; + hello << "XYZ 123"; + + ss.add_callback(new_connection,bind(&test_wrapper_noserver::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"; + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + } + } + + // there is no valid server + + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + // result is constructed with default constructor on error-and-ignore -> i=0 + + CPPUNIT_ASSERT_EQUAL(0,i); + } + + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper_noserver);