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 = NULL;
43 string testfunc(const string& str)
46 ret=str+", testfunc() was here";
48 // call handle, eventually reentrant
50 global_server->handle(1000);
55 class testfunc_res : public libt2n::result
60 friend class boost::serialization::access;
61 template<class Archive>
62 void serialize(Archive & ar, const unsigned int version)
64 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
65 ar & BOOST_SERIALIZATION_NVP(res);
72 testfunc_res(const string& str)
84 class testfunc_cmd : public libt2n::command
89 friend class boost::serialization::access;
90 template<class Archive>
91 void serialize(Archive & ar, const unsigned int version)
93 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
94 ar & BOOST_SERIALIZATION_NVP(param);
101 testfunc_cmd(const string& str)
106 libt2n::result* operator()()
108 return new testfunc_res(testfunc(param));
114 #include <boost/serialization/export.hpp>
116 BOOST_CLASS_EXPORT(testfunc_cmd)
117 BOOST_CLASS_EXPORT(testfunc_res)
119 class test_reentrant : public TestFixture
121 CPPUNIT_TEST_SUITE(test_reentrant);
123 CPPUNIT_TEST(ReentrantServer);
125 CPPUNIT_TEST_SUITE_END();
137 void ReentrantServer()
139 switch(child_pid=fork())
143 CPPUNIT_FAIL("fork error");
149 // wait till server is up
152 // we want 8 identical childs hammering the server
157 for (int i=0; i < 100; i++)
159 socket_client_connection sc("./socket");
160 command_client cc(&sc);
163 cc.send_command(new testfunc_cmd("hello"),rc);
165 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
167 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
170 // don't call atexit and stuff
177 socket_server ss("./socket");
178 command_server cs(ss);
183 long long maxtime=5000000;
185 cs.handle(maxtime,&maxtime);
187 global_server = NULL;
190 // we are still alive, everything is ok
191 CPPUNIT_ASSERT_EQUAL(1,1);
198 CPPUNIT_TEST_SUITE_REGISTRATION(test_reentrant);