1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <cppunit/extensions/TestFactoryRegistry.h>
19 #include <cppunit/ui/text/TestRunner.h>
20 #include <cppunit/extensions/HelperMacros.h>
22 #include <boost/archive/binary_oarchive.hpp>
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/xml_oarchive.hpp>
25 #include <boost/archive/xml_iarchive.hpp>
26 #include <boost/serialization/serialization.hpp>
28 #include <container.hxx>
29 #include <socket_client.hxx>
30 #include <socket_server.hxx>
31 #include <command_client.hxx>
32 #include <command_server.hxx>
35 using namespace CppUnit;
36 using namespace libt2n;
41 command_server *global_server;
43 string testfunc(const string& str)
46 ret=str+", testfunc() was here";
48 // call handle, eventually reentrant
49 global_server->handle(1000);
54 class testfunc_res : public libt2n::result
59 friend class boost::serialization::access;
60 template<class Archive>
61 void serialize(Archive & ar, const unsigned int version)
63 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
64 ar & BOOST_SERIALIZATION_NVP(res);
71 testfunc_res(const string& str)
83 class testfunc_cmd : public libt2n::command
88 friend class boost::serialization::access;
89 template<class Archive>
90 void serialize(Archive & ar, const unsigned int version)
92 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
93 ar & BOOST_SERIALIZATION_NVP(param);
100 testfunc_cmd(const string& str)
105 libt2n::result* operator()()
107 return new testfunc_res(testfunc(param));
113 #include <boost/serialization/export.hpp>
115 BOOST_CLASS_EXPORT(testfunc_cmd)
116 BOOST_CLASS_EXPORT(testfunc_res)
118 class test_reentrant : public TestFixture
120 CPPUNIT_TEST_SUITE(test_reentrant);
122 CPPUNIT_TEST(ReentrantServer);
124 CPPUNIT_TEST_SUITE_END();
136 void ReentrantServer()
138 switch(child_pid=fork())
142 CPPUNIT_FAIL("fork error");
148 // wait till server is up
151 // we want 8 identical childs hammering the server
156 for (int i=0; i < 100; i++)
158 socket_client_connection sc("./socket");
159 command_client cc(&sc);
162 cc.send_command(new testfunc_cmd("hello"),rc);
164 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
166 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
169 // don't call atexit and stuff
176 socket_server ss("./socket");
177 command_server cs(ss);
182 long long maxtime=5000000;
184 cs.handle(maxtime,&maxtime);
187 // we are still alive, everything is ok
188 CPPUNIT_ASSERT_EQUAL(1,1);
195 CPPUNIT_TEST_SUITE_REGISTRATION(test_reentrant);