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;
37 string testfunc(const string& str)
41 throw libt2n::t2n_runtime_error("throw me around");
43 ret.insert(0,100*1024,'x');
45 ret=str+", testfunc() was here";
49 class testfunc_res : public libt2n::result
54 friend class boost::serialization::access;
55 template<class Archive>
56 void serialize(Archive & ar, const unsigned int version)
58 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
59 ar & BOOST_SERIALIZATION_NVP(res);
66 testfunc_res(const string& str)
78 class testfunc_cmd : public libt2n::command
83 friend class boost::serialization::access;
84 template<class Archive>
85 void serialize(Archive & ar, const unsigned int version)
87 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
88 ar & BOOST_SERIALIZATION_NVP(param);
95 testfunc_cmd(const string& str)
100 libt2n::result* operator()()
102 return new testfunc_res(testfunc(param));
106 #include <boost/serialization/export.hpp>
108 BOOST_CLASS_EXPORT(testfunc_cmd)
109 BOOST_CLASS_EXPORT(testfunc_res)
111 using namespace libt2n;
113 class test_simplecmd : public TestFixture
115 CPPUNIT_TEST_SUITE(test_simplecmd);
117 CPPUNIT_TEST(SimpleCmd);
118 CPPUNIT_TEST(SimpleException);
119 CPPUNIT_TEST(BigReturn);
120 CPPUNIT_TEST(BigParameter);
122 CPPUNIT_TEST_SUITE_END();
133 // make sure the server-child is dead before the next test runs
134 kill(child_pid,SIGKILL);
140 switch(child_pid=fork())
144 CPPUNIT_FAIL("fork error");
152 socket_server ss("./socket");
153 command_server cs(ss);
156 for (int i=0; i < 10; i++)
160 std::cerr << "exception in child. ignoring\n";
163 // don't call atexit and stuff
170 // wait till server is up
172 socket_client_connection sc("./socket");
173 sc.set_logging(&cerr,debug);
174 command_client cc(&sc);
177 cc.send_command(new testfunc_cmd("hello"),rc);
179 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
181 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
186 void SimpleException()
188 switch(child_pid=fork())
192 CPPUNIT_FAIL("fork error");
200 socket_server ss("./socket");
201 command_server cs(ss);
204 for (int i=0; i < 10; i++)
208 std::cerr << "exception in child. ignoring\n";
211 // don't call atexit and stuff
218 // wait till server is up
220 socket_client_connection sc("./socket");
221 sc.set_logging(&cerr,debug);
222 command_client cc(&sc);
225 cc.send_command(new testfunc_cmd("throw"),rc);
231 ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
233 catch(t2n_runtime_error &e)
238 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
245 switch(child_pid=fork())
249 CPPUNIT_FAIL("fork error");
257 socket_server ss("./socket");
258 command_server cs(ss);
261 for (int i=0; i < 10; i++)
265 std::cerr << "exception in child. ignoring\n";
268 // don't call atexit and stuff
275 // wait till server is up
277 socket_client_connection sc("./socket");
278 command_client cc(&sc);
281 cc.send_command(new testfunc_cmd("big"),rc);
283 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
285 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
292 switch(child_pid=fork())
296 CPPUNIT_FAIL("fork error");
304 socket_server ss("./socket");
305 command_server cs(ss);
307 // max 60 sec - we need atleast 28 handle calls to transfer the buffer
308 for (int i=0; i < 60; i++) {
313 std::cerr << "exception in child. ignoring\n";
316 // don't call atexit and stuff
323 // wait till server is up
325 socket_client_connection sc("./socket");
326 command_client cc(&sc);
329 cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc);
331 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
333 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
340 CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);