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>
27 #include <boost/serialization/export.hpp>
29 #include <container.hxx>
30 #include <socket_client.hxx>
31 #include <socket_server.hxx>
32 #include <command_client.hxx>
33 #include <command_server.hxx>
34 #include <client_wrapper.hxx>
35 #include <socket_wrapper.hxx>
42 using namespace libt2n;
43 using namespace CppUnit;
47 stringstream logstream;
48 bool close_server=false;
49 bool kill_server=false;
62 std::string getserverlog(void)
64 return logstream.str();
67 class serverfunc_res : public libt2n::result
72 friend class boost::serialization::access;
73 template<class Archive>
74 void serialize(Archive & ar, const unsigned int version)
76 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
77 ar & BOOST_SERIALIZATION_NVP(res);
95 class getserverlog_res : public libt2n::result
100 friend class boost::serialization::access;
101 template<class Archive>
102 void serialize(Archive & ar, const unsigned int version)
104 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
105 ar & BOOST_SERIALIZATION_NVP(res);
112 getserverlog_res(std::string s)
117 std::string get_data()
123 class cmd_group_x : public command
126 friend class boost::serialization::access;
127 template<class Archive>
128 void serialize(Archive & ar, const unsigned int version)
130 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
134 class serverfunc_cmd : public cmd_group_x
139 friend class boost::serialization::access;
140 template<class Archive>
141 void serialize(Archive & ar, const unsigned int version)
143 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_x);
144 ar & BOOST_SERIALIZATION_NVP(param);
151 serverfunc_cmd(int i)
156 libt2n::result* operator()()
158 return new serverfunc_res(serverfunc(param));
162 class getserverlog_cmd : public cmd_group_x
165 friend class boost::serialization::access;
166 template<class Archive>
167 void serialize(Archive & ar, const unsigned int version)
169 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_x);
176 libt2n::result* operator()()
178 return new getserverlog_res(getserverlog());
182 BOOST_CLASS_EXPORT(serverfunc_res)
183 BOOST_CLASS_EXPORT(getserverlog_res)
184 BOOST_CLASS_EXPORT(cmd_group_x)
185 BOOST_CLASS_EXPORT(serverfunc_cmd)
186 BOOST_CLASS_EXPORT(getserverlog_cmd)
188 class cmd_group_x_client : public command_client
191 cmd_group_x_client(libt2n::client_connection *_c,
192 long long _command_timeout_usec=command_timeout_usec_default,
193 long long _hello_timeout_usec=hello_timeout_usec_default)
194 : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)
197 int serverfunc(int i)
199 libt2n::result_container rc;
201 send_command(new serverfunc_cmd(i), rc);
202 serverfunc_res* res=dynamic_cast<serverfunc_res*>(rc.get_result());
203 if (!res) throw libt2n::t2n_communication_error("result object of wrong type");
204 return res->get_data();
207 std::string getserverlog(void)
209 libt2n::result_container rc;
211 send_command(new getserverlog_cmd(), rc);
212 getserverlog_res* res=dynamic_cast<getserverlog_res*>(rc.get_result());
213 if (!res) throw libt2n::t2n_communication_error("result object of wrong type");
214 return res->get_data();
218 typedef T2nSingletonWrapper<cmd_group_x_client> wraptype;
221 std::auto_ptr<wraptype> wraptype::SingletonObject = std::auto_ptr<wraptype>();
224 std::auto_ptr<ConnectionWrapper> wraptype::WrappedConnection = std::auto_ptr<ConnectionWrapper>();
226 class test_wrapper : public TestFixture
228 CPPUNIT_TEST_SUITE(test_wrapper);
230 CPPUNIT_TEST(no_init_exception); // must be called first!!!
231 CPPUNIT_TEST(simple_wrap);
232 CPPUNIT_TEST(double_use);
233 CPPUNIT_TEST(double_use_with_close);
234 CPPUNIT_TEST(reconnect_after_close);
235 CPPUNIT_TEST(reconnect_not_possible);
236 CPPUNIT_TEST(ignore_server_disconnect);
237 CPPUNIT_TEST(ignore_handler_reconnects);
239 CPPUNIT_TEST_SUITE_END();
250 switch(child_pid=fork())
254 CPPUNIT_FAIL("fork error");
263 while(i < 10 && !kill_server)
267 socket_server ss("./socket");
268 group_command_server<cmd_group_x> cs(ss);
269 ss.set_logging(&logstream,debug);
272 for (; !close_server && !kill_server && i < 10; i++)
277 std::cerr << "exception in child. ignoring\n";
280 // don't call atexit and stuff
287 // wait till server is up
296 // make sure the server-child is dead before the next test runs
297 kill(child_pid,SIGKILL);
301 void no_init_exception()
303 CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error);
308 wraptype::set_connection(auto_ptr<ConnectionWrapper>
309 (new BasicSocketWrapper("./socket")));
311 int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
313 CPPUNIT_ASSERT_EQUAL(2,i);
318 // only one connection used?
319 wraptype::set_connection(auto_ptr<ConnectionWrapper>
320 (new BasicSocketWrapper("./socket")));
322 t2n_exec(&cmd_group_x_client::serverfunc)(17);
323 string out=t2n_exec(&cmd_group_x_client::getserverlog)();
325 // count the number of times that "new connection accepted" appears in the server log
326 string::size_type p=0;
328 while ((p=out.find("new connection accepted",p))++ != string::npos)
331 CPPUNIT_ASSERT_EQUAL(1,cnt);
334 void double_use_with_close()
336 wraptype::set_connection(auto_ptr<ConnectionWrapper>
337 (new BasicSocketWrapper("./socket")));
339 t2n_exec(&cmd_group_x_client::serverfunc)(17);
341 // closes the connection from the client side
342 wraptype::set_connection(auto_ptr<ConnectionWrapper>
343 (new BasicSocketWrapper("./socket")));
345 string out=t2n_exec(&cmd_group_x_client::getserverlog)();
347 // count the number of times that "new connection accepted" appears in the server log
348 string::size_type p=0;
350 while ((p=out.find("new connection accepted",p))++ != string::npos)
353 CPPUNIT_ASSERT_EQUAL(2,cnt);
356 void reconnect_after_close()
358 wraptype::set_connection(auto_ptr<ConnectionWrapper>
359 (new ReconnectSocketWrapper("./socket")));
361 wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
362 wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
364 // 42 closes connection on the server side
365 t2n_exec(&cmd_group_x_client::serverfunc)(42);
367 string out=t2n_exec(&cmd_group_x_client::getserverlog)();
369 // count the number of times that "new connection accepted" appears in the server log
370 string::size_type p=0;
372 while ((p=out.find("new connection accepted",p))++ != string::npos)
375 CPPUNIT_ASSERT_EQUAL(2,cnt);
378 void reconnect_not_possible()
380 wraptype::set_connection(auto_ptr<ConnectionWrapper>
381 (new ReconnectSocketWrapper("./socket")));
383 // the server doens't like the beast
384 t2n_exec(&cmd_group_x_client::serverfunc)(666);
386 CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error);
389 void ignore_server_disconnect()
391 wraptype::set_connection(auto_ptr<ConnectionWrapper>
392 (new ReconnectIgnoreFailureSocketWrapper("./socket")));
394 // the server doens't like the beast
395 t2n_exec(&cmd_group_x_client::serverfunc)(666);
397 int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
399 // result is constructed with default constructor on error-and-ignore -> i=0
401 CPPUNIT_ASSERT_EQUAL(0,i);
404 void ignore_handler_reconnects()
406 wraptype::set_connection(auto_ptr<ConnectionWrapper>
407 (new ReconnectIgnoreFailureSocketWrapper("./socket")));
409 wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
410 wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
412 // 42 closes connection on the server side
413 t2n_exec(&cmd_group_x_client::serverfunc)(42);
415 string out=t2n_exec(&cmd_group_x_client::getserverlog)();
417 // count the number of times that "new connection accepted" appears in the server log
418 string::size_type p=0;
420 while ((p=out.find("new connection accepted",p))++ != string::npos)
423 CPPUNIT_ASSERT_EQUAL(2,cnt);
428 CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper);
431 class test_wrapper_noserver : public TestFixture
433 CPPUNIT_TEST_SUITE(test_wrapper_noserver);
435 CPPUNIT_TEST(ignore_noserver);
436 CPPUNIT_TEST(ignore_finds_lateserver);
437 CPPUNIT_TEST(ignore_wrongserver);
439 CPPUNIT_TEST_SUITE_END();
452 // make sure the server-child is dead before the next test runs
455 kill(child_pid,SIGKILL);
460 void ignore_noserver()
462 wraptype::set_connection(auto_ptr<ConnectionWrapper>
463 (new ReconnectIgnoreFailureSocketWrapper("./socket")));
465 // wraptype::get_connection_wrapper()->set_logging(&cerr,debug);
467 // there is no server
469 int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
471 // result is constructed with default constructor on error-and-ignore -> i=0
473 CPPUNIT_ASSERT_EQUAL(0,i);
476 void ignore_finds_lateserver()
478 wraptype::set_connection(auto_ptr<ConnectionWrapper>
479 (new ReconnectIgnoreFailureSocketWrapper("./socket")));
481 // there is no server
482 t2n_exec(&cmd_group_x_client::serverfunc)(1);
488 switch(child_pid=fork())
492 CPPUNIT_FAIL("fork error");
501 while(i < 10 && !kill_server)
505 socket_server ss("./socket");
506 group_command_server<cmd_group_x> cs(ss);
507 ss.set_logging(&logstream,debug);
510 for (; !close_server && !kill_server && i < 10; i++)
515 std::cerr << "exception in child. ignoring\n";
518 // don't call atexit and stuff
525 // wait till server is up
530 // server should be active
531 int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
533 CPPUNIT_ASSERT_EQUAL(2,i);
536 void send_hello(string hello_string, socket_server* ss, int conn_id)
538 server_connection *sc=ss->get_connection(conn_id);
539 sc->write(hello_string);
542 void ignore_wrongserver()
544 wraptype::set_connection(auto_ptr<ConnectionWrapper>
545 (new ReconnectIgnoreFailureSocketWrapper("./socket")));
549 switch(child_pid=fork())
553 CPPUNIT_FAIL("fork error");
561 socket_server ss("./socket");
563 // server sends garbage
568 ss.add_callback(new_connection,bind(&test_wrapper_noserver::send_hello, boost::ref(*this), hello.str(),&ss, _1));
571 for (int i=0; i < 10; i++)
572 ss.fill_buffer(1000000);
575 std::cerr << "exception in child. ignoring\n";
578 // don't call atexit and stuff
585 // wait till server is up
590 // there is no valid server
592 int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
594 // result is constructed with default constructor on error-and-ignore -> i=0
596 CPPUNIT_ASSERT_EQUAL(0,i);
602 CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper_noserver);