libt2n: (tomj) added exception handling to every child after fork(). This is needed...
[libt2n] / test / comm.cpp
index 5bba916..47ca385 100644 (file)
@@ -9,6 +9,7 @@
 #include <errno.h>
 #include <signal.h>
 #include <stdio.h>
+#include <time.h>
 
 #include <iostream>
 #include <string>
@@ -40,20 +41,25 @@ class test_comm : 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 UnixCommToServer()
     {
-        pid_t pid;
         string data;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -63,10 +69,17 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                // wait till server is up
-                sleep(1);
-                socket_client_connection sc("./socket");
-                sc.write("hello");
+                try
+                {
+                    // wait till server is up
+                    sleep(1);
+                    socket_client_connection sc("./socket");
+                    sc.write("hello");
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
+
                 // don't call atexit and stuff
                 _exit(0);
             }
@@ -76,8 +89,10 @@ class test_comm : public TestFixture
             {
                 socket_server ss("./socket");
 
+                time_t t0 = time(NULL);
+
                 // max 10 sec
-                for (int i=0; i < 10; i++)
+                while (time(NULL) < t0 + 10 )
                 {
                     ss.fill_buffer(1000000);
 
@@ -91,9 +106,7 @@ class test_comm : public TestFixture
 
     void UnixCommToServerAndBack()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -103,30 +116,39 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                ss.set_logging(&cerr,debug);
-
-                // max 10 sec
-                for (int i=0; i < 10; i++)
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
+                    ss.set_logging(&cerr,debug);
 
-                    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(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
 
-                        if (data=="QUIT")
-                            break;
+                            if (data=="QUIT")
+                                break;
 
-                        if (data=="ABC")
-                            con->write("DEF");
-                        else
-                            con->write("xyz");
+                            if (data=="ABC")
+                                con->write("DEF");
+                            else
+                                con->write("xyz");
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
+
                 // don't call atexit and stuff
                 _exit(0);
             }
@@ -161,9 +183,7 @@ class test_comm : public TestFixture
 
     void UnixCommToServerAndBackBig()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -173,27 +193,37 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                ss.set_logging(&cerr,debug);
-
-                // max 10 sec
-                for (int i=0; i < 10; i++)
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss("./socket");
+                    ss.set_logging(&cerr,debug);
 
-                    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;
 
-                        con->write(string().insert(0,100*1024,'y'));
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
+
+                            if (data=="QUIT")
+                                break;
+
+                            con->write(string().insert(0,100*1024,'y'));
+                        }
                     }
+                    std::cerr << "child: OVER" << std::endl;
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
+
                 // don't call atexit and stuff
                 _exit(0);
             }
@@ -221,10 +251,9 @@ class test_comm : public TestFixture
 
     void IPCommToServer()
     {
-        pid_t pid;
         string data;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -234,10 +263,17 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                // wait till server is up
-                sleep(1);
-                socket_client_connection sc(6666);
-                sc.write("hello");
+                try
+                {
+                    // wait till server is up
+                    sleep(1);
+                    socket_client_connection sc(6666);
+                    sc.write("hello");
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
+
                 // don't call atexit and stuff
                 _exit(0);
             }
@@ -247,8 +283,10 @@ class test_comm : public TestFixture
             {
                 socket_server ss(6666);
 
+                time_t t0 = time(NULL);
+
                 // max 10 sec
-                for (int i=0; i < 10; i++)
+                while (time(NULL) < t0 + 10 )
                 {
                     ss.fill_buffer(1000000);
 
@@ -262,9 +300,7 @@ class test_comm : public TestFixture
 
     void IPCommToServerAndBack()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -274,29 +310,37 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                socket_server ss(6666);
-                ss.set_logging(&cerr,debug);
-
-                // max 10 sec
-                for (int i=0; i < 10; i++)
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss(6666);
+                    ss.set_logging(&cerr,debug);
 
-                    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=="ABC")
-                            con->write("DEF");
-                        else
-                            con->write("xyz");
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
+
+                            if (data=="QUIT")
+                                break;
+
+                            if (data=="ABC")
+                                con->write("DEF");
+                            else
+                                con->write("xyz");
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
                 // don't call atexit and stuff
                 _exit(0);
@@ -332,9 +376,7 @@ class test_comm : public TestFixture
 
     void IPCommToServerAndBackBig()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -344,29 +386,37 @@ class test_comm : public TestFixture
             case 0:
             // child
             {
-                socket_server ss(6666);
-                ss.set_logging(&cerr,debug);
-
-                // max 10 sec
-                for (int i=0; i < 10; i++)
+                try
                 {
-                    ss.fill_buffer(1000000);
+                    socket_server ss(6666);
+                    ss.set_logging(&cerr,debug);
 
-                    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;
 
-                        socket_handler* alias= dynamic_cast< socket_handler* >(con);
+                        if(ss.get_packet(data,cid))
+                        {
+                            server_connection* con=ss.get_connection(cid);
 
-                        if (data=="QUIT")
-                            break;
+                            socket_handler* alias= dynamic_cast< socket_handler* >(con);
 
-                        alias->set_write_block_size( 4093 );
-                        con->write(string().insert(0,2048*1024,'y'));
+                            if (data=="QUIT")
+                                break;
+
+                            alias->set_write_block_size( 4093 );
+                            con->write(string().insert(0,2048*1024,'y'));
+                        }
                     }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
                 }
                 // don't call atexit and stuff
                 _exit(0);
@@ -392,9 +442,6 @@ class test_comm : public TestFixture
             }
         }
     } // eo IPCommToServerAndBackBig()
-
-
-
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);