X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Freentrant.cpp;fp=test%2Freentrant.cpp;h=bb9420a4db0d118d5f0937d09cb6289a7d6a0f37;hp=0000000000000000000000000000000000000000;hb=3b2543e7dfd705d6e624560dd5a681898c0f242c;hpb=f6d1a1e3b9d04b63158288ebf2917d9fd47afff8 diff --git a/test/reentrant.cpp b/test/reentrant.cpp new file mode 100644 index 0000000..bb9420a --- /dev/null +++ b/test/reentrant.cpp @@ -0,0 +1,195 @@ +/*************************************************************************** + * 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; +using namespace libt2n; + +namespace +{ + +command_server *global_server; + +string testfunc(const string& str) +{ + string ret; + ret=str+", testfunc() was here"; + + // call handle, eventually reentrant + global_server->handle(1000); + + return ret; +} + +class testfunc_res : public libt2n::result +{ + private: + string 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: + testfunc_res() + { } + + testfunc_res(const string& str) + { + res=str; + } + + string get_data() + { + return res; + } +}; + + +class testfunc_cmd : public libt2n::command +{ + private: + string 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: + testfunc_cmd() + { } + + testfunc_cmd(const string& str) + { + param=str; + } + + libt2n::result* operator()() + { + return new testfunc_res(testfunc(param)); + } +}; + +} + +#include + +BOOST_CLASS_EXPORT(testfunc_cmd) +BOOST_CLASS_EXPORT(testfunc_res) + +class test_reentrant : public TestFixture +{ + CPPUNIT_TEST_SUITE(test_reentrant); + + CPPUNIT_TEST(ReentrantServer); + + CPPUNIT_TEST_SUITE_END(); + + pid_t child_pid; + + public: + + void setUp() + { } + + void tearDown() + { } + + void ReentrantServer() + { + switch(child_pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + // wait till server is up + sleep(1); + + // we want 8 identical childs hammering the server + fork(); + fork(); + fork(); + + for (int i=0; i < 100; i++) + { + socket_client_connection sc("./socket"); + command_client cc(&sc); + + result_container rc; + cc.send_command(new testfunc_cmd("hello"),rc); + + string ret=dynamic_cast(rc.get_result())->get_data(); + + CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret); + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + socket_server ss("./socket"); + command_server cs(ss); + + global_server=&cs; + + // max 10 sec + long long maxtime=5000000; + while(maxtime > 0) + cs.handle(maxtime,&maxtime); + } + + // we are still alive, everything is ok + CPPUNIT_ASSERT_EQUAL(1,1); + } + } + +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION(test_reentrant);