libt2n: (gerd) make sure no exception is thrown in command_client constructor, fix...
[libt2n] / test / wrapper.cpp
index c820531..8a7c16d 100644 (file)
@@ -236,10 +236,6 @@ class test_wrapper : public TestFixture
     CPPUNIT_TEST(ignore_server_disconnect);
     CPPUNIT_TEST(ignore_handler_reconnects);
 
-//  TODO: missing tests:
-//  ignore: init, no server, ignore
-//  ignore: init, no server, ignore, server ok, connect?
-
     CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -421,8 +417,6 @@ class test_wrapper : public TestFixture
         CPPUNIT_ASSERT_EQUAL(2,cnt);
     }
 
-
-
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper);
@@ -434,6 +428,7 @@ class test_wrapper_noserver : public TestFixture
 
     CPPUNIT_TEST(ignore_noserver);
     CPPUNIT_TEST(ignore_finds_lateserver);
+    CPPUNIT_TEST(ignore_wrongserver);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -442,10 +437,19 @@ class test_wrapper_noserver : public TestFixture
     pid_t child_pid;
 
     void setUp()
-    {  }
+    {
+        child_pid=0;
+    }
 
     void tearDown()
-    {  }
+    {
+        // make sure the server-child is dead before the next test runs
+        if (child_pid != 0)
+        {
+            kill(child_pid,SIGKILL);
+            sleep(1);
+        }
+    }
 
     void ignore_noserver()
     {
@@ -472,7 +476,6 @@ class test_wrapper_noserver : public TestFixture
         t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
         // launch a server
-
         close_server=false;
         kill_server=false;
 
@@ -516,10 +519,62 @@ class test_wrapper_noserver : public TestFixture
         int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
         CPPUNIT_ASSERT_EQUAL(2,i);
+    }
 
-        // 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, int conn_id)
+    {
+        server_connection *sc=ss->get_connection(conn_id);
+        sc->write(hello_string);
+    }
+
+    void ignore_wrongserver()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+        // launch a server
+
+        switch(child_pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+
+                // server sends garbage
+
+                ostringstream hello;
+                hello << "XYZ 123";
+
+                ss.add_callback(new_connection,bind(&test_wrapper_noserver::send_hello, boost::ref(*this), hello.str(),&ss, _1));
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                    ss.fill_buffer(1000000);
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // wait till server is up
+                sleep(1);
+            }
+        }
+
+        // there is no valid server
+
+        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+
+        // result is constructed with default constructor on error-and-ignore  -> i=0
+
+        CPPUNIT_ASSERT_EQUAL(0,i);
     }