2 Copyright (C) 2004 by Intra2net AG
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
22 #include <sys/types.h>
33 #include <cppunit/extensions/TestFactoryRegistry.h>
34 #include <cppunit/ui/text/TestRunner.h>
35 #include <cppunit/extensions/HelperMacros.h>
37 #include <boost/archive/binary_oarchive.hpp>
38 #include <boost/archive/binary_iarchive.hpp>
39 #include <boost/archive/xml_oarchive.hpp>
40 #include <boost/archive/xml_iarchive.hpp>
41 #include <boost/serialization/serialization.hpp>
43 #include <container.hxx>
44 #include <socket_client.hxx>
45 #include <socket_server.hxx>
46 #include <command_client.hxx>
47 #include <command_server.hxx>
50 using namespace CppUnit;
52 string testfunc(const string& str)
56 throw libt2n::t2n_runtime_error("throw me around");
58 ret.insert(0,100*1024,'x');
60 ret=str+", testfunc() was here";
64 class testfunc_res : public libt2n::result
69 friend class boost::serialization::access;
70 template<class Archive>
71 void serialize(Archive & ar, const unsigned int version)
73 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
74 ar & BOOST_SERIALIZATION_NVP(res);
81 testfunc_res(const string& str)
93 class testfunc_cmd : public libt2n::command
98 friend class boost::serialization::access;
99 template<class Archive>
100 void serialize(Archive & ar, const unsigned int version)
102 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
103 ar & BOOST_SERIALIZATION_NVP(param);
110 testfunc_cmd(const string& str)
115 libt2n::result* operator()()
117 return new testfunc_res(testfunc(param));
121 #include <boost/serialization/export.hpp>
123 BOOST_CLASS_EXPORT(testfunc_cmd)
124 BOOST_CLASS_EXPORT(testfunc_res)
126 using namespace libt2n;
128 class test_simplecmd : public TestFixture
130 CPPUNIT_TEST_SUITE(test_simplecmd);
132 CPPUNIT_TEST(SimpleCmd);
133 CPPUNIT_TEST(SimpleException);
134 CPPUNIT_TEST(BigReturn);
135 CPPUNIT_TEST(BigParameter);
137 CPPUNIT_TEST_SUITE_END();
148 // make sure the server-child is dead before the next test runs
149 kill(child_pid,SIGKILL);
155 switch(child_pid=fork())
159 CPPUNIT_FAIL("fork error");
167 socket_server ss("./socket");
168 command_server cs(ss);
171 for (int i=0; i < 10; i++)
175 std::cerr << "exception in child. ignoring\n";
178 // don't call atexit and stuff
185 // wait till server is up
187 socket_client_connection sc("./socket");
188 sc.set_logging(&cerr,debug);
189 command_client cc(&sc);
192 cc.send_command(new testfunc_cmd("hello"),rc);
194 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
196 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
201 void SimpleException()
203 switch(child_pid=fork())
207 CPPUNIT_FAIL("fork error");
215 socket_server ss("./socket");
216 command_server cs(ss);
219 for (int i=0; i < 10; i++)
223 std::cerr << "exception in child. ignoring\n";
226 // don't call atexit and stuff
233 // wait till server is up
235 socket_client_connection sc("./socket");
236 sc.set_logging(&cerr,debug);
237 command_client cc(&sc);
240 cc.send_command(new testfunc_cmd("throw"),rc);
246 ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
248 catch(t2n_runtime_error &e)
253 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
260 switch(child_pid=fork())
264 CPPUNIT_FAIL("fork error");
272 socket_server ss("./socket");
273 command_server cs(ss);
276 for (int i=0; i < 10; i++)
280 std::cerr << "exception in child. ignoring\n";
283 // don't call atexit and stuff
290 // wait till server is up
292 socket_client_connection sc("./socket");
293 command_client cc(&sc);
296 cc.send_command(new testfunc_cmd("big"),rc);
298 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
300 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
307 switch(child_pid=fork())
311 CPPUNIT_FAIL("fork error");
319 socket_server ss("./socket");
320 command_server cs(ss);
322 // max 60 sec - we need atleast 28 handle calls to transfer the buffer
323 for (int i=0; i < 60; i++) {
328 std::cerr << "exception in child. ignoring\n";
331 // don't call atexit and stuff
338 // wait till server is up
340 socket_client_connection sc("./socket");
341 command_client cc(&sc);
344 cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc);
346 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
348 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
355 CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);