libt2n: (tomj) fixed call of virtual function close() from destructor, fixed return...
[libt2n] / test / timeout.cpp
index 8bdfa31..5156869 100644 (file)
@@ -138,11 +138,14 @@ class test_timeout : public TestFixture
     CPPUNIT_TEST(CommandTimeout);
     CPPUNIT_TEST(CommandSlowResponse);
     CPPUNIT_TEST(DisconnectOnWrite);
+    CPPUNIT_TEST(WriteTwice);
     CPPUNIT_TEST(DisconnectOnRead);
     CPPUNIT_TEST(BreakAccept);
 
     CPPUNIT_TEST_SUITE_END();
 
+    pid_t child_pid;
+
     public:
 
     typedef uint32_t packet_size_indicator;
@@ -151,7 +154,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 +185,7 @@ class test_timeout : public TestFixture
 
     void ConnectTimeout()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -206,26 +211,18 @@ class test_timeout : public TestFixture
 
                 string errormsg;
 
-                try
-                {
-                    socket_client_connection sc("./socket");
-                    command_client cc(sc,1000000,1000000);
-                }
-                catch(t2n_transfer_error &e)
-                { errormsg=e.what(); }
-                catch(...)
-                { throw; }
+                socket_client_connection sc("./socket");
+
+                CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed());
 
-                CPPUNIT_ASSERT_EQUAL(string("error reading from socket : Invalid argument"),errormsg);
+                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:
             {
@@ -252,17 +249,13 @@ 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);
             }
@@ -271,9 +264,7 @@ class test_timeout : public TestFixture
 
     void HelloTimeoutSlowData()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -313,17 +304,13 @@ 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);
             }
@@ -332,9 +319,7 @@ class test_timeout : public TestFixture
 
     void CommandTimeout()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -370,7 +355,7 @@ class test_timeout : public TestFixture
                 sleep(1);
                 socket_client_connection sc("./socket");
 
-                command_client cc(sc,1000000,1000000);
+                command_client cc(&sc,1000000,1000000);
                 result_container rc;
 
                 string errormsg;
@@ -391,9 +376,7 @@ class test_timeout : public TestFixture
 
     void CommandSlowResponse()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -444,7 +427,7 @@ class test_timeout : public TestFixture
                 sleep(1);
                 socket_client_connection sc("./socket");
 
-                command_client cc(sc,1000000,1000000);
+                command_client cc(&sc,1000000,1000000);
                 result_container rc;
 
                 string errormsg;
@@ -465,9 +448,7 @@ class test_timeout : public TestFixture
 
     void DisconnectOnWrite()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -499,7 +480,7 @@ class test_timeout : public TestFixture
 
                 string errormsg;
 
-                string huge(1000000,'x');
+                string huge(5000000,'x');
 
                 try
                 {
@@ -515,11 +496,65 @@ class test_timeout : public TestFixture
         }
     }
 
+    void WriteTwice()
+    {
+        switch(child_pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+
+                // bail out as soon as we get something
+                ss.fill_buffer(-1);
+                ss.fill_buffer(-1);
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // don't kill us on broken pipe
+                signal(SIGPIPE, SIG_IGN);
+
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("./socket");
+
+                string errormsg;
+
+                sc.write("somedata");
+
+                sleep(1);
+
+                // server should disconnect now
+                try
+                {
+                    sc.write("other data");
+                }
+                catch(t2n_transfer_error &e)
+                { errormsg=e.what(); }
+                catch(...)
+                { throw; }
+
+                CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
+            }
+        }
+    }
+
     void DisconnectOnRead()
     {
-        pid_t pid;
+        pid_t pid2;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -563,7 +598,7 @@ class test_timeout : public TestFixture
 
                 // are we still alive and able to process data?
 
-                switch(pid=fork())
+                switch(pid2=fork())
                 {
                     case -1:
                     {
@@ -603,13 +638,14 @@ class test_timeout : public TestFixture
                 }
             }
         }
+        kill(pid2,SIGKILL);
     }
 
     void BreakAccept()
     {
-        pid_t pid;
+        pid_t pid2;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -653,7 +689,7 @@ class test_timeout : public TestFixture
 
                 // are we still alive and able to process data?
 
-                switch(pid=fork())
+                switch(pid2=fork())
                 {
                     case -1:
                     {
@@ -693,6 +729,7 @@ class test_timeout : public TestFixture
                 }
             }
         }
+        kill(pid2,SIGKILL);
     }
 };