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>
34 #define BOOST_TEST_DYN_LINK
35 #include <boost/test/unit_test.hpp>
37 #include <socket_client.hxx>
38 #include <socket_server.hxx>
40 #include "test_fixtures.hxx"
45 using namespace libt2n;
47 BOOST_FIXTURE_TEST_SUITE(test_reconnect, KillChildOnShutdownFixture)
49 BOOST_AUTO_TEST_CASE(simple_reconnect)
51 switch(child_pid=fork())
55 BOOST_FAIL("fork error");
63 socket_server ss("./socket");
65 time_t t0 = time(NULL);
68 while (time(NULL) < t0 + 10 )
70 ss.fill_buffer(1000000);
75 if(ss.get_packet(data,cid))
77 server_connection* con=ss.get_connection(cid);
83 con->write(string().insert(0,100,'X'));
85 con->write(string().insert(0,100,'Y'));
90 std::cerr << "exception in child. ignoring\n";
93 // don't call atexit and stuff
100 // don't kill us on broken pipe
101 signal(SIGPIPE, SIG_IGN);
103 // wait till server is up
105 socket_client_connection sc("./socket");
111 while (!sc.get_packet(data))
112 sc.fill_buffer(1000000);
118 while (!sc.get_packet(data))
119 sc.fill_buffer(1000000);
121 BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
126 BOOST_AUTO_TEST_CASE(reconnect_with_close)
128 switch(child_pid=fork())
132 BOOST_FAIL("fork error");
140 socket_server ss("./socket");
142 time_t t0 = time(NULL);
145 while (time(NULL) < t0 + 10 )
147 ss.fill_buffer(1000000);
152 if(ss.get_packet(data,cid))
154 server_connection* con=ss.get_connection(cid);
160 con->write(string().insert(0,100,'X'));
162 con->write(string().insert(0,100,'Y'));
167 std::cerr << "exception in child. ignoring\n";
170 // don't call atexit and stuff
177 // don't kill us on broken pipe
178 signal(SIGPIPE, SIG_IGN);
180 // wait till server is up
182 socket_client_connection sc("./socket");
188 while (!sc.get_packet(data))
189 sc.fill_buffer(1000000);
200 while (!sc.get_packet(data))
201 sc.fill_buffer(1000000);
203 BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
208 BOOST_AUTO_TEST_CASE(reconnect_buffer_complete)
210 switch(child_pid=fork())
214 BOOST_FAIL("fork error");
222 socket_server ss("./socket");
224 time_t t0 = time(NULL);
227 while (time(NULL) < t0 + 10 )
229 ss.fill_buffer(1000000);
234 if(ss.get_packet(data,cid))
236 server_connection* con=ss.get_connection(cid);
242 con->write(string().insert(0,100,'X'));
247 std::cerr << "exception in child. ignoring\n";
250 // don't call atexit and stuff
257 // don't kill us on broken pipe
258 signal(SIGPIPE, SIG_IGN);
260 // wait till server is up
262 socket_client_connection sc("./socket");
268 while (!sc.packet_available())
269 sc.fill_buffer(1000000);
273 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
275 BOOST_CHECK_MESSAGE(data == string().insert(0,100,'X'), "data incorrect");
280 BOOST_AUTO_TEST_CASE(reconnect_buffer_several_complete)
284 switch(child_pid=fork())
288 BOOST_FAIL("fork error");
296 socket_server ss("./socket");
298 time_t t0 = time(NULL);
301 while (time(NULL) < t0 + 10 )
303 ss.fill_buffer(1000000);
308 if(ss.get_packet(data,cid))
310 server_connection* con=ss.get_connection(cid);
317 for (int i=0; i<packets; i++)
318 con->write(string().insert(0,100,'X'));
324 std::cerr << "exception in child. ignoring\n";
327 // don't call atexit and stuff
334 // don't kill us on broken pipe
335 signal(SIGPIPE, SIG_IGN);
337 // wait till server is up
339 socket_client_connection sc("./socket");
343 // retrieve packets for 3 seconds
344 time_t t0 = time(NULL);
347 while (time(NULL) < t0 + 3 )
348 sc.fill_buffer(1000000);
350 // we now should have packets complete packets in the buffer
354 // are these packets still there?
356 for (int i=0; i < packets; i++)
361 os << "packet " << i << " not there";
363 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, os.str());
366 os << "packet " << i << " incorrect";
368 BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, os.str());
374 BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete1)
376 switch(child_pid=fork())
380 BOOST_FAIL("fork error");
388 socket_server ss("./socket");
390 time_t t0 = time(NULL);
393 while (time(NULL) < t0 + 10 )
395 ss.fill_buffer(1000000);
400 if(ss.get_packet(data,cid))
402 server_connection* con=ss.get_connection(cid);
409 con->write(string().insert(0,100,'X'));
410 send_raw_socket("aaaab",&ss,cid);
416 std::cerr << "exception in child. ignoring\n";
419 // don't call atexit and stuff
426 // don't kill us on broken pipe
427 signal(SIGPIPE, SIG_IGN);
429 // wait till server is up
431 socket_client_connection sc("./socket");
435 // retrieve packets for 3 seconds
436 time_t t0 = time(NULL);
439 while (time(NULL) < t0 + 3 )
440 sc.fill_buffer(1000000);
442 // we now should have one complete packet and some stuff in the buffer
447 BOOST_CHECK_MESSAGE((sc.peek_packet(data))>0, "no incomplete packet");
451 BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
456 BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete2)
458 switch(child_pid=fork())
462 BOOST_FAIL("fork error");
470 socket_server ss("./socket");
472 time_t t0 = time(NULL);
475 while (time(NULL) < t0 + 10 )
477 ss.fill_buffer(1000000);
482 if(ss.get_packet(data,cid))
484 server_connection* con=ss.get_connection(cid);
491 con->write(string().insert(0,100,'X'));
493 string blob=string().insert(0,100,'Y');
495 // one byte will be missing...
496 int size=blob.size()+1;
497 char sizetransfer[sizeof(int)+1];
498 memcpy(sizetransfer,(void*)&size,sizeof(int));
499 sizetransfer[sizeof(int)+1]=0;
501 string packet=string(sizetransfer)+blob;
503 send_raw_socket(packet,&ss,cid);
509 std::cerr << "exception in child. ignoring\n";
512 // don't call atexit and stuff
519 // don't kill us on broken pipe
520 signal(SIGPIPE, SIG_IGN);
522 // wait till server is up
524 socket_client_connection sc("./socket");
528 // retrieve packets for 3 seconds
529 time_t t0 = time(NULL);
532 while (time(NULL) < t0 + 3 )
533 sc.fill_buffer(1000000);
535 // we now should have one complete packet and some stuff in the buffer
541 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
542 BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, "data incorrect");
544 BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
549 BOOST_AUTO_TEST_SUITE_END()