libt2n: (gerd) fix client-connection-logic, finish wrappers, all tests are working...
[libt2n] / test / wrapper.cpp
index 062f9b4..c820531 100644 (file)
@@ -188,7 +188,7 @@ BOOST_CLASS_EXPORT(getserverlog_cmd)
 class cmd_group_x_client : public command_client
 {
     public:
-        cmd_group_x_client(libt2n::client_connection &_c,
+        cmd_group_x_client(libt2n::client_connection *_c,
          long long _command_timeout_usec=command_timeout_usec_default,
          long long _hello_timeout_usec=hello_timeout_usec_default)
          : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)
@@ -233,9 +233,10 @@ class test_wrapper : public TestFixture
     CPPUNIT_TEST(double_use_with_close);
     CPPUNIT_TEST(reconnect_after_close);
     CPPUNIT_TEST(reconnect_not_possible);
+    CPPUNIT_TEST(ignore_server_disconnect);
+    CPPUNIT_TEST(ignore_handler_reconnects);
 
 //  TODO: missing tests:
-//  ignore: init, use, server die, ignore
 //  ignore: init, no server, ignore
 //  ignore: init, no server, ignore, server ok, connect?
 
@@ -243,11 +244,14 @@ class test_wrapper : public TestFixture
 
     public:
 
+    pid_t child_pid;
+
     void setUp()
     {
-        pid_t pid;
+        close_server=false;
+        kill_server=false;
 
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -258,7 +262,7 @@ class test_wrapper : public TestFixture
             // child
             {
                 int i=0;
-                while(i < 15 && !kill_server)
+                while(i < 10 && !kill_server)
                 {
                     close_server=false;
 
@@ -267,7 +271,7 @@ class test_wrapper : public TestFixture
                     ss.set_logging(&logstream,debug);
 
                     // max 10 sec
-                    for (; !close_server && !kill_server && i < 15; i++)
+                    for (; !close_server && !kill_server && i < 10; i++)
                         cs.handle(1000000);
                 }
 
@@ -286,7 +290,16 @@ class test_wrapper : public TestFixture
     }
 
     void tearDown()
-    { }
+    {
+        // make sure the server-child is dead before the next test runs
+        kill(child_pid,SIGKILL);
+        sleep(1);
+    }
+
+    void no_init_exception()
+    {
+        CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error);
+    }
 
     void simple_wrap()
     {
@@ -298,11 +311,6 @@ class test_wrapper : public TestFixture
         CPPUNIT_ASSERT_EQUAL(2,i);
     }
 
-    void no_init_exception()
-    {
-        CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error);
-    }
-
     void double_use()
     {
         // only one connection used?
@@ -376,7 +384,145 @@ class test_wrapper : public TestFixture
         CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error);
     }
 
+    void ignore_server_disconnect()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+        // the server doens't like the beast
+        t2n_exec(&cmd_group_x_client::serverfunc)(666);
+
+        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);
+    }
+
+    void ignore_handler_reconnects()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+        wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
+        wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
+
+        // 42 closes connection on the server side
+        t2n_exec(&cmd_group_x_client::serverfunc)(42);
+
+        string out=t2n_exec(&cmd_group_x_client::getserverlog)();
+
+        // count the number of times that "new connection accepted" appears in the server log
+        string::size_type p=0;
+        int cnt=0;
+        while ((p=out.find("new connection accepted",p))++ != string::npos)
+            cnt++;
+
+        CPPUNIT_ASSERT_EQUAL(2,cnt);
+    }
+
+
 
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper);
+
+
+class test_wrapper_noserver : public TestFixture
+{
+    CPPUNIT_TEST_SUITE(test_wrapper_noserver);
+
+    CPPUNIT_TEST(ignore_noserver);
+    CPPUNIT_TEST(ignore_finds_lateserver);
+
+    CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+    pid_t child_pid;
+
+    void setUp()
+    {  }
+
+    void tearDown()
+    {  }
+
+    void ignore_noserver()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+        // wraptype::get_connection_wrapper()->set_logging(&cerr,debug);
+
+        // there is no 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);
+    }
+
+    void ignore_finds_lateserver()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+        // there is no server
+        t2n_exec(&cmd_group_x_client::serverfunc)(1);
+
+        // launch a server
+
+        close_server=false;
+        kill_server=false;
+
+        switch(child_pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                int i=0;
+                while(i < 10 && !kill_server)
+                {
+                    close_server=false;
+
+                    socket_server ss("./socket");
+                    group_command_server<cmd_group_x> cs(ss);
+                    ss.set_logging(&logstream,debug);
+
+                    // max 10 sec
+                    for (; !close_server && !kill_server && i < 10; i++)
+                        cs.handle(1000000);
+                }
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // wait till server is up
+                sleep(1);
+            }
+        }
+
+        // server should be active
+        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);
+    }
+
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper_noserver);