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(10000);
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();
135 void ReentrantServer()
141 CPPUNIT_FAIL("fork error");
147 // wait till server is up
150 // 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 testfunc_res *res = dynamic_cast<testfunc_res*>(rc.get_result());
168 string ret = res->get_data();
169 if (ret != "hello, testfunc() was here")
170 std::cout << "ERROR reentrant server testfunc_res failed, res: " << ret << "\n";
174 std::cout << "ERROR result from reentrant server empty\n";
177 } catch (exception &e)
179 cerr << "caught exception: " << e.what() << endl;
182 // don't call atexit and stuff
189 // don't kill us on broken pipe
190 signal(SIGPIPE, SIG_IGN);
192 socket_server ss("./socket");
193 command_server cs(ss);
198 long long maxtime=1000000;
200 cs.handle(maxtime,&maxtime);
205 cs.handle(maxtime,&maxtime);
210 cs.handle(maxtime,&maxtime);
212 global_server = NULL;
215 // we are still alive, everything is ok
216 CPPUNIT_ASSERT_EQUAL(1,1);
223 CPPUNIT_TEST_SUITE_REGISTRATION(test_reentrant);