libt2n: (gerd) make sure no exception is thrown in command_client constructor, fix...
[libt2n] / test / timeout.cpp
index 6618ef6..bf32527 100644 (file)
@@ -143,6 +143,8 @@ class test_timeout : public TestFixture
 
     CPPUNIT_TEST_SUITE_END();
 
+    pid_t child_pid;
+
     public:
 
     typedef uint32_t packet_size_indicator;
@@ -151,7 +153,11 @@ class test_timeout : public TestFixture
     { }
 
     void tearDown()
-    { }
+    {
+        // make sure the server-child is dead before the next test runs
+        kill(child_pid,SIGKILL);
+        sleep(1);
+    }
 
     void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
     {
@@ -178,9 +184,7 @@ class test_timeout : public TestFixture
 
     void ConnectTimeout()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -208,16 +212,16 @@ class test_timeout : public TestFixture
 
                 socket_client_connection sc("./socket");
 
-                CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed());
+                CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed());
+
+                CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong errormessage",string("no more retries left after connect error"),sc.get_last_error_msg());
             }
         }
     }
 
     void HelloTimeoutNothing()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -244,30 +248,22 @@ class test_timeout : public TestFixture
                 // wait till server is up
                 sleep(1);
                 socket_client_connection sc("./socket");
+                command_client cc(&sc,1000000,1000000);
 
-                string errormsg;
+                t2n_exception* ep=cc.get_constuctor_exception();
 
-                try
-                {
-                    command_client cc(&sc,1000000,1000000);
-                }
-                catch(t2n_transfer_error &e)
-                { errormsg=e.what(); }
-                catch(...)
-                { throw; }
+                string errormsg;
+                if (ep)
+                    errormsg=ep->what();
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void HelloTimeoutSlowData()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -307,30 +303,22 @@ class test_timeout : public TestFixture
                 // wait till server is up
                 sleep(1);
                 socket_client_connection sc("./socket");
+                command_client cc(&sc,1000000,1000000);
 
-                string errormsg;
+                t2n_exception* ep=cc.get_constuctor_exception();
 
-                try
-                {
-                    command_client cc(&sc,1000000,1000000);
-                }
-                catch(t2n_transfer_error &e)
-                { errormsg=e.what(); }
-                catch(...)
-                { throw; }
+                string errormsg;
+                if (ep)
+                    errormsg=ep->what();
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void CommandTimeout()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -381,17 +369,13 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void CommandSlowResponse()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -457,17 +441,13 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void DisconnectOnWrite()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -511,17 +491,15 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
-
-                kill(pid,SIGKILL);
             }
         }
     }
 
     void DisconnectOnRead()
     {
-        pid_t pid1, pid2;
+        pid_t pid2;
 
-        switch(pid1=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -605,15 +583,14 @@ class test_timeout : public TestFixture
                 }
             }
         }
-        kill(pid1,SIGKILL);
         kill(pid2,SIGKILL);
     }
 
     void BreakAccept()
     {
-        pid_t pid1,pid2;
+        pid_t pid2;
 
-        switch(pid1=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -697,7 +674,6 @@ class test_timeout : public TestFixture
                 }
             }
         }
-        kill(pid1,SIGKILL);
         kill(pid2,SIGKILL);
     }
 };