1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
19 #include <boost/bind.hpp>
21 #include <cppunit/extensions/TestFactoryRegistry.h>
22 #include <cppunit/ui/text/TestRunner.h>
23 #include <cppunit/extensions/HelperMacros.h>
25 #include <socket_client.hxx>
26 #include <socket_server.hxx>
29 using namespace libt2n;
30 using namespace CppUnit;
32 class test_callback : public TestFixture
34 CPPUNIT_TEST_SUITE(test_callback);
36 CPPUNIT_TEST(NewConnCallback);
37 CPPUNIT_TEST(ConnClosedCallback);
38 CPPUNIT_TEST(ConnDeletedCallback);
39 CPPUNIT_TEST(CallbackOrder);
41 CPPUNIT_TEST_SUITE_END();
43 std::vector<bool> callback_done;
49 callback_done.resize(__events_end);
54 callback_done.clear();
57 void callback_func(callback_event_type ev, int conn_id)
59 // we don't care for the conn_id, we just mark the callback as done
60 if (callback_done[ev])
61 throw runtime_error("callback already done for this event");
63 callback_done[ev]=true;
66 void NewConnCallback()
74 CPPUNIT_FAIL("fork error");
81 // wait till server is up
85 socket_client_connection sc("./socket");
90 sc.fill_buffer(500000);
93 // close the connection
96 // don't call atexit and stuff
103 socket_server ss("./socket");
105 ss.add_callback(new_connection,bind(&test_callback::callback_func, boost::ref(*this), new_connection, _1));
108 for (int i=0; i < 3; i++)
110 ss.fill_buffer(1000000);
114 if(ss.get_packet(data,cid))
116 server_connection* con=ss.get_connection(cid);
120 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[new_connection]));
121 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
122 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
127 void ConnClosedCallback()
135 CPPUNIT_FAIL("fork error");
142 // wait till server is up
146 socket_client_connection sc("./socket");
151 sc.fill_buffer(500000);
154 // close the connection
157 // don't call atexit and stuff
164 socket_server ss("./socket");
166 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
169 for (int i=0; i < 3; i++)
171 ss.fill_buffer(1000000);
175 if(ss.get_packet(data,cid))
177 server_connection* con=ss.get_connection(cid);
181 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
182 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
183 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
188 void ConnDeletedCallback()
196 CPPUNIT_FAIL("fork error");
203 // wait till server is up
207 socket_client_connection sc("./socket");
212 sc.fill_buffer(500000);
215 // close the connection
218 // don't call atexit and stuff
225 socket_server ss("./socket");
227 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
230 for (int i=0; i < 3; i++)
232 ss.fill_buffer(1000000);
236 if(ss.get_packet(data,cid))
238 server_connection* con=ss.get_connection(cid);
245 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
246 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
247 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
260 CPPUNIT_FAIL("fork error");
267 // wait till server is up
271 socket_client_connection sc("./socket");
276 sc.fill_buffer(500000);
281 // close the connection
284 // don't call atexit and stuff
291 socket_server ss("./socket");
293 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
294 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
298 for (int i=0; i < 4; i++)
300 ss.fill_buffer(500000);
304 if(!got_1 && ss.get_packet(data,cid))
306 server_connection* con=ss.get_connection(cid);
309 // don't call get_packet anymore
315 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
316 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
318 for (int i=0; i < 4; i++)
320 ss.fill_buffer(500000);
324 ss.get_packet(data,cid);
329 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
330 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
337 CPPUNIT_TEST_SUITE_REGISTRATION(test_callback);