Change license from LGPL to GPL version 2 + linking exception. This fixes C++ templat...
[libt2n] / test / reconnect.cpp
index 9e64a8e..f078a95 100644 (file)
@@ -1,9 +1,24 @@
-/***************************************************************************
- *   Copyright (C) 2004 by Intra2net AG                                    *
- *   info@intra2net.com                                                    *
- *                                                                         *
- ***************************************************************************/
+/*
+Copyright (C) 2004 by Intra2net AG
 
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
@@ -52,13 +67,19 @@ class test_reconnect : public TestFixture
 
     CPPUNIT_TEST_SUITE_END();
 
+    pid_t child_pid;
+
     public:
 
     void setUp()
     { }
 
     void tearDown()
-    { }
+    {
+        // make sure the server-child is dead before the next test runs
+        kill(child_pid,SIGKILL);
+        sleep(1);
+    }
 
     void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
     {
@@ -71,9 +92,7 @@ class test_reconnect : public TestFixture
 
     void simple_reconnect()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -83,30 +102,36 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
 
-                        if (data=="QUIT")
-                            break;
+                        string data;
+                        unsigned int cid;
 
-                        if (data=="x")
-                            con->write(string().insert(0,100,'X'));
-                        else
-                            con->write(string().insert(0,100,'Y'));
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
+
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="x")
+                                con->write(string().insert(0,100,'X'));
+                            else
+                                con->write(string().insert(0,100,'Y'));
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff
@@ -144,9 +169,7 @@ class test_reconnect : public TestFixture
 
     void reconnect_with_close()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -156,30 +179,36 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
 
-                        if (data=="QUIT")
-                            break;
+                        string data;
+                        unsigned int cid;
+
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
 
-                        if (data=="x")
-                            con->write(string().insert(0,100,'X'));
-                        else
-                            con->write(string().insert(0,100,'Y'));
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="x")
+                                con->write(string().insert(0,100,'X'));
+                            else
+                                con->write(string().insert(0,100,'Y'));
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff
@@ -222,9 +251,7 @@ class test_reconnect : public TestFixture
 
     void reconnect_buffer_complete()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -234,28 +261,34 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
+
+                        string data;
+                        unsigned int cid;
 
-                        if (data=="QUIT")
-                            break;
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
 
-                        if (data=="x")
-                            con->write(string().insert(0,100,'X'));
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="x")
+                                con->write(string().insert(0,100,'X'));
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff
@@ -290,11 +323,9 @@ class test_reconnect : public TestFixture
 
     void reconnect_buffer_several_complete()
     {
-        pid_t pid;
-
         const int packets=3;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -304,31 +335,37 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
 
-                        if (data=="QUIT")
-                            break;
+                        string data;
+                        unsigned int cid;
 
-                        if (data=="x")
+                        if(ss.get_packet(data,cid))
                         {
-                            for (int i=0; i<packets; i++)
-                                con->write(string().insert(0,100,'X'));
+                            server_connection* con=ss.get_connection(cid);
+
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="x")
+                            {
+                                for (int i=0; i<packets; i++)
+                                    con->write(string().insert(0,100,'X'));
+                            }
                         }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff
@@ -380,9 +417,7 @@ class test_reconnect : public TestFixture
 
     void reconnect_buffer_no_incomplete1()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -392,31 +427,37 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
 
-                        if (data=="QUIT")
-                            break;
+                        string data;
+                        unsigned int cid;
 
-                        if (data=="x")
+                        if(ss.get_packet(data,cid))
                         {
-                            con->write(string().insert(0,100,'X'));
-                            send_raw_socket("aaaab",&ss,cid);
+                            server_connection* con=ss.get_connection(cid);
+
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="x")
+                            {
+                                con->write(string().insert(0,100,'X'));
+                                send_raw_socket("aaaab",&ss,cid);
+                            }
                         }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff
@@ -458,9 +499,7 @@ class test_reconnect : public TestFixture
 
     void reconnect_buffer_no_incomplete2()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -470,42 +509,48 @@ class test_reconnect : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-
-                time_t t0 = time(NULL);
-
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
 
-                    string data;
-                    unsigned int cid;
+                    time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data,cid))
+                    // max 10 sec
+                    while (time(NULL) < t0 + 10 )
                     {
-                        server_connection* con=ss.get_connection(cid);
+                        ss.fill_buffer(1000000);
 
-                        if (data=="QUIT")
-                            break;
+                        string data;
+                        unsigned int cid;
 
-                        if (data=="x")
+                        if(ss.get_packet(data,cid))
                         {
-                            con->write(string().insert(0,100,'X'));
+                            server_connection* con=ss.get_connection(cid);
 
-                            string blob=string().insert(0,100,'Y');
+                            if (data=="QUIT")
+                                break;
 
-                            // one byte will be missing...
-                            int size=blob.size()+1;
-                            char sizetransfer[sizeof(int)+1];
-                            memcpy(sizetransfer,(void*)&size,sizeof(int));
-                            sizetransfer[sizeof(int)+1]=0;
+                            if (data=="x")
+                            {
+                                con->write(string().insert(0,100,'X'));
+
+                                string blob=string().insert(0,100,'Y');
 
-                            string packet=string(sizetransfer)+blob;
+                                // one byte will be missing...
+                                int size=blob.size()+1;
+                                char sizetransfer[sizeof(int)+1];
+                                memcpy(sizetransfer,(void*)&size,sizeof(int));
+                                sizetransfer[sizeof(int)+1]=0;
 
-                            send_raw_socket(packet,&ss,cid);
+                                string packet=string(sizetransfer)+blob;
+
+                                send_raw_socket(packet,&ss,cid);
+                            }
                         }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
 
                 // don't call atexit and stuff