X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fwrapper.cpp;h=b1d30cd75139431438f2810bcdc6431cdc19c288;hp=062f9b47451bd1b4096216895c7ee24cd8f0fe5c;hb=ca55612532d9f5f9422d7fe367e00929b1856486;hpb=e1614a6d1d9c022b83c0aa8cdb948bd7dc98ff23 diff --git a/test/wrapper.cpp b/test/wrapper.cpp index 062f9b4..b1d30cd 100644 --- a/test/wrapper.cpp +++ b/test/wrapper.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 @@ -15,9 +30,8 @@ #include #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #include @@ -34,13 +48,14 @@ #include #include +#include "test_fixtures.hxx" + #ifdef HAVE_CONFIG_H #include #endif using namespace std; using namespace libt2n; -using namespace CppUnit; // the server part @@ -188,7 +203,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) @@ -223,52 +238,43 @@ std::auto_ptr wraptype::SingletonObject = std::auto_ptr(); template<> std::auto_ptr wraptype::WrappedConnection = std::auto_ptr(); -class test_wrapper : public TestFixture -{ - CPPUNIT_TEST_SUITE(test_wrapper); - - CPPUNIT_TEST(no_init_exception); // must be called first!!! - CPPUNIT_TEST(simple_wrap); - CPPUNIT_TEST(double_use); - 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_SUITE_END(); - - public: - - void setUp() +class test_wrapperFixture : public KillChildOnShutdownFixture +{ +public: + test_wrapperFixture() { - pid_t pid; + close_server=false; + kill_server=false; - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { - CPPUNIT_FAIL("fork error"); + BOOST_FAIL("fork error"); break; } 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 @@ -278,105 +284,282 @@ class test_wrapper : public TestFixture default: // parent { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); + // wait till server is up sleep(1); - } } } - void tearDown() - { } - - void simple_wrap() + ~test_wrapperFixture() { - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + } +}; - int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); +BOOST_FIXTURE_TEST_SUITE(test_wrapper, test_wrapperFixture) - CPPUNIT_ASSERT_EQUAL(2,i); - } +BOOST_AUTO_TEST_CASE(no_init_exception) // must be called first! +{ + BOOST_REQUIRE_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); +} - void no_init_exception() - { - CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); - } +BOOST_AUTO_TEST_CASE(simple_wrap) +{ + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - void double_use() - { - // only one connection used? - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + BOOST_CHECK_EQUAL(2,i); +} - t2n_exec(&cmd_group_x_client::serverfunc)(17); - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); +BOOST_AUTO_TEST_CASE(double_use) +{ + // only one connection used? + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - // 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++; + t2n_exec(&cmd_group_x_client::serverfunc)(17); + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); - CPPUNIT_ASSERT_EQUAL(1,cnt); - } + // 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++; - void double_use_with_close() - { - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + BOOST_CHECK_EQUAL(1,cnt); +} - t2n_exec(&cmd_group_x_client::serverfunc)(17); +BOOST_AUTO_TEST_CASE(double_use_with_close) +{ + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - // closes the connection from the client side - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + t2n_exec(&cmd_group_x_client::serverfunc)(17); - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); + // closes the connection from the client side + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - // 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++; + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); - CPPUNIT_ASSERT_EQUAL(2,cnt); - } + // 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++; - void reconnect_after_close() - { - wraptype::set_connection(auto_ptr - (new ReconnectSocketWrapper("./socket"))); + BOOST_CHECK_EQUAL(2,cnt); +} + +BOOST_AUTO_TEST_CASE(reconnect_after_close) +{ + wraptype::set_connection(auto_ptr + (new ReconnectSocketWrapper("./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++; + + BOOST_CHECK_EQUAL(2,cnt); +} + +BOOST_AUTO_TEST_CASE(reconnect_not_possible) +{ + wraptype::set_connection(auto_ptr + (new ReconnectSocketWrapper("./socket"))); + + // the server doens't like the beast + t2n_exec(&cmd_group_x_client::serverfunc)(666); + + BOOST_REQUIRE_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error); +} + +BOOST_AUTO_TEST_CASE(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 + + BOOST_CHECK_EQUAL(0,i); +} - wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); - wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); +BOOST_AUTO_TEST_CASE(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++; + + BOOST_CHECK_EQUAL(2,cnt); +} + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_FIXTURE_TEST_SUITE(test_wrapper_noserver, KillChildOnShutdownFixture) + +BOOST_AUTO_TEST_CASE(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 + + BOOST_CHECK_EQUAL(0,i); +} + +BOOST_AUTO_TEST_CASE(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: + { + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + int i=0; + while(i < 10 && !kill_server) + { + close_server=false; - // 42 closes connection on the server side - t2n_exec(&cmd_group_x_client::serverfunc)(42); + socket_server ss("./socket"); + group_command_server cs(ss); + ss.set_logging(&logstream,debug); - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); + // max 10 sec + for (; !close_server && !kill_server && i < 10; i++) + cs.handle(1000000); + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } - // 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++; + // don't call atexit and stuff + _exit(0); + } - CPPUNIT_ASSERT_EQUAL(2,cnt); + default: + // parent + { + // wait till server is up + sleep(1); + } } - void reconnect_not_possible() + // server should be active + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + BOOST_CHECK_EQUAL(2,i); +} + +BOOST_AUTO_TEST_CASE(ignore_wrongserver) +{ + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // launch a server + + switch(child_pid=fork()) { - wraptype::set_connection(auto_ptr - (new ReconnectSocketWrapper("./socket"))); + case -1: + { + BOOST_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::ignore_wrongserver::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"; + } - // the server doens't like the beast - t2n_exec(&cmd_group_x_client::serverfunc)(666); + // don't call atexit and stuff + _exit(0); + } - CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error); + 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 + + BOOST_CHECK_EQUAL(0,i); +} -CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper); +BOOST_AUTO_TEST_SUITE_END()