libt2n: (tomj) added exception handling to every child after fork(). This is needed...
[libt2n] / test / simplecmd.cpp
index 42721ea..99c720f 100644 (file)
@@ -121,20 +121,23 @@ class test_simplecmd : 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 SimpleCmd()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -144,12 +147,18 @@ class test_simplecmd : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                command_server cs(ss);
+                try
+                {
+                    socket_server ss("./socket");
+                    command_server cs(ss);
 
-                // max 10 sec
-                for (int i=0; i < 10; i++)
-                    cs.handle(1000000);
+                    // max 10 sec
+                    for (int i=0; i < 10; i++)
+                        cs.handle(1000000);
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
 
                 // don't call atexit and stuff
                 _exit(0);
@@ -172,14 +181,11 @@ class test_simplecmd : public TestFixture
                 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
             }
         }
-        kill(pid,SIGKILL);
     }
 
     void SimpleException()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -189,12 +195,18 @@ class test_simplecmd : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                command_server cs(ss);
+                try
+                {
+                    socket_server ss("./socket");
+                    command_server cs(ss);
 
-                // max 10 sec
-                for (int i=0; i < 10; i++)
-                    cs.handle(1000000);
+                    // max 10 sec
+                    for (int i=0; i < 10; i++)
+                        cs.handle(1000000);
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
 
                 // don't call atexit and stuff
                 _exit(0);
@@ -224,17 +236,13 @@ class test_simplecmd : public TestFixture
                     { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void BigReturn()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -244,12 +252,18 @@ class test_simplecmd : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                command_server cs(ss);
+                try
+                {
+                    socket_server ss("./socket");
+                    command_server cs(ss);
 
-                // max 10 sec
-                for (int i=0; i < 10; i++)
-                    cs.handle(1000000);
+                    // max 10 sec
+                    for (int i=0; i < 10; i++)
+                        cs.handle(1000000);
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
 
                 // don't call atexit and stuff
                 _exit(0);
@@ -269,17 +283,13 @@ class test_simplecmd : public TestFixture
                 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
 
                 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void BigParameter()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -289,12 +299,19 @@ class test_simplecmd : public TestFixture
             case 0:
             // child
             {
-                socket_server ss("./socket");
-                command_server cs(ss);
+                try
+                {
+                    socket_server ss("./socket");
+                    command_server cs(ss);
 
-                // max 10 sec
-                for (int i=0; i < 10; i++)
-                    cs.handle(1000000);
+                    // max 60 sec - we need atleast 28 handle calls to transfer the buffer
+                    for (int i=0; i < 60; i++) {
+                        cs.handle(1000000);
+                }
+                } catch(...)
+                {
+                    std::cerr << "exception in child. ignoring\n";
+                }
 
                 // don't call atexit and stuff
                 _exit(0);
@@ -314,8 +331,6 @@ class test_simplecmd : public TestFixture
                 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
 
                 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
-
-                kill(pid,SIGKILL);
             }
         }
     }