X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fnewserver.cpp;fp=test%2Fnewserver.cpp;h=2631d0ee4f3ff1ac9571bcc13f6b67c377cdbb16;hp=0000000000000000000000000000000000000000;hb=039e52da9b40d0e729360ff0a953dfbddf975b8a;hpb=1bdd13c030b192cd3087eee64a581a0d43766d16 diff --git a/test/newserver.cpp b/test/newserver.cpp new file mode 100644 index 0000000..2631d0e --- /dev/null +++ b/test/newserver.cpp @@ -0,0 +1,197 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace std; +using namespace CppUnit; + +int newserver_func(int i) +{ + + return 1; +} + +class newserver_res : public libt2n::result +{ + private: + int res; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result); + ar & BOOST_SERIALIZATION_NVP(res); + } + + public: + newserver_res() + { } + + newserver_res(int i) + { + res=i; + } + + int get_data() + { + return res; + } +}; + + +class newserver_cmd : public libt2n::command +{ + private: + int param; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command); + ar & BOOST_SERIALIZATION_NVP(param); + } + + public: + newserver_cmd() + { } + + newserver_cmd(int i) + { + param=i; + } + + libt2n::result* operator()() + { + return new newserver_res(newserver_func(param)); + } +}; + +#include + +BOOST_CLASS_EXPORT(newserver_cmd) +BOOST_CLASS_EXPORT(newserver_res) + +using namespace libt2n; + +class test_newserver : public TestFixture +{ + CPPUNIT_TEST_SUITE(test_newserver); + + CPPUNIT_TEST(NewServerSocket); + + 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 NewServerSocket() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + { + socket_server ss("./socket"); + command_server cs(ss); + + // handle new connection and just one command + cs.handle(10000000); + cs.handle(10000000); + } + // close socket, create new one + { + socket_server ss("./socket"); + ss.set_logging(&cerr,debug); + command_server cs(ss); + + // max 30 sec + for (int i=0; i < 30; i++) + cs.handle(1000000); + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + sc.set_logging(&cerr,debug); + command_client cc(&sc); + + result_container rc; + cc.send_command(new newserver_cmd(1),rc); + + // very short sleep to make sure new server socket is up + usleep(10000); + + // still has connection to the old server-socket + string errormsg; + + try + { + sc.write("some stuff"); + } + catch(t2n_transfer_error &e) + { errormsg=e.what(); } + catch(...) + { throw; } + + CPPUNIT_ASSERT_EQUAL(string("error reading from socket : Connection reset by peer"),errormsg); + } + } + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(test_newserver);