libt2n: (gerd) bugfixes, better logging, unit tests for wrapper, ignore handler still...
[libt2n] / test / wrapper.cpp
index 2b2e41f..062f9b4 100644 (file)
 #include <cppunit/ui/text/TestRunner.h>
 #include <cppunit/extensions/HelperMacros.h>
 
+#include <boost/archive/binary_oarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/export.hpp>
+
+#include <container.hxx>
+#include <socket_client.hxx>
+#include <socket_server.hxx>
 #include <command_client.hxx>
+#include <command_server.hxx>
 #include <client_wrapper.hxx>
 #include <socket_wrapper.hxx>
 
@@ -31,21 +42,180 @@ using namespace std;
 using namespace libt2n;
 using namespace CppUnit;
 
-class testme : public command_client
+// the server part
+
+stringstream logstream;
+bool close_server=false;
+bool kill_server=false;
+
+int serverfunc(int i)
+{
+    // magic commands
+    if (i==42)
+        close_server=true;
+    if (i==666)
+        kill_server=true;
+
+    return i+1;
+}
+
+std::string getserverlog(void)
+{
+    return logstream.str();
+}
+
+class serverfunc_res : public libt2n::result
+{
+    private:
+        int res;
+
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
+            ar & BOOST_SERIALIZATION_NVP(res);
+        }
+
+    public:
+        serverfunc_res()
+            { }
+
+        serverfunc_res(int i)
+        {
+            res=i;
+        }
+
+        int get_data()
+        {
+            return res;
+        }
+};
+
+class getserverlog_res : public libt2n::result
+{
+    private:
+        std::string res;
+
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
+            ar & BOOST_SERIALIZATION_NVP(res);
+        }
+
+    public:
+        getserverlog_res()
+            { }
+
+        getserverlog_res(std::string s)
+        {
+            res=s;
+        }
+
+        std::string get_data()
+        {
+            return res;
+        }
+};
+
+class cmd_group_x : public command
+{
+    private:
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
+        }
+};
+
+class serverfunc_cmd : public cmd_group_x
+{
+    private:
+        int param;
+
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_x);
+            ar & BOOST_SERIALIZATION_NVP(param);
+        }
+
+    public:
+        serverfunc_cmd()
+            { }
+
+        serverfunc_cmd(int i)
+        {
+            param=i;
+        }
+
+        libt2n::result* operator()()
+        {
+            return new serverfunc_res(serverfunc(param));
+        }
+};
+
+class getserverlog_cmd : public cmd_group_x
 {
+    private:
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_x);
+        }
+
     public:
+        getserverlog_cmd()
+            { }
 
-    testme(client_connection &x, long long a, long long b)
-        : command_client(x,100000,10000)
-        { }
+        libt2n::result* operator()()
+        {
+            return new getserverlog_res(getserverlog());
+        }
+};
+
+BOOST_CLASS_EXPORT(serverfunc_res)
+BOOST_CLASS_EXPORT(getserverlog_res)
+BOOST_CLASS_EXPORT(cmd_group_x)
+BOOST_CLASS_EXPORT(serverfunc_cmd)
+BOOST_CLASS_EXPORT(getserverlog_cmd)
 
-    void helloworld(const std::string& text)
+class cmd_group_x_client : public command_client
+{
+    public:
+        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)
+        {}
+
+    int serverfunc(int i)
     {
-        std::cout << "Hello world, " << text << std::endl;
+        libt2n::result_container rc;
+
+        send_command(new serverfunc_cmd(i), rc);
+        serverfunc_res* res=dynamic_cast<serverfunc_res*>(rc.get_result());
+        if (!res) throw libt2n::t2n_communication_error("result object of wrong type");
+        return res->get_data();
+    }
+
+    std::string getserverlog(void)
+    {
+        libt2n::result_container rc;
+
+        send_command(new getserverlog_cmd(), rc);
+        getserverlog_res* res=dynamic_cast<getserverlog_res*>(rc.get_result());
+        if (!res) throw libt2n::t2n_communication_error("result object of wrong type");
+        return res->get_data();
     }
 };
 
-typedef T2nSingletonWrapper<testme> wraptype;
+typedef T2nSingletonWrapper<cmd_group_x_client> wraptype;
 
 template<>
 std::auto_ptr<wraptype> wraptype::SingletonObject = std::auto_ptr<wraptype>();
@@ -57,23 +227,153 @@ class test_wrapper : public TestFixture
 {
     CPPUNIT_TEST_SUITE(test_wrapper);
 
+    CPPUNIT_TEST(no_init_exception); // must be called first!!!
     CPPUNIT_TEST(simple_wrap);
+    CPPUNIT_TEST(double_use);
+    CPPUNIT_TEST(double_use_with_close);
+    CPPUNIT_TEST(reconnect_after_close);
+    CPPUNIT_TEST(reconnect_not_possible);
+
+//  TODO: missing tests:
+//  ignore: init, use, server die, ignore
+//  ignore: init, no server, ignore
+//  ignore: init, no server, ignore, server ok, connect?
 
     CPPUNIT_TEST_SUITE_END();
 
     public:
 
     void setUp()
-    { }
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                int i=0;
+                while(i < 15 && !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 < 15; i++)
+                        cs.handle(1000000);
+                }
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // wait till server is up
+                sleep(1);
+
+            }
+        }
+    }
 
     void tearDown()
     { }
 
     void simple_wrap()
     {
-//        t2n_exec(&testme::helloworld)("gurke");
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new BasicSocketWrapper("./socket")));
+
+        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+
+        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?
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new BasicSocketWrapper("./socket")));
+
+        t2n_exec(&cmd_group_x_client::serverfunc)(17);
+        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(1,cnt);
+    }
+
+    void double_use_with_close()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new BasicSocketWrapper("./socket")));
+
+        t2n_exec(&cmd_group_x_client::serverfunc)(17);
+
+        // closes the connection from the client side
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new BasicSocketWrapper("./socket")));
+
+        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);
+    }
+
+    void reconnect_after_close()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectSocketWrapper("./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);
+    }
+
+    void reconnect_not_possible()
+    {
+        wraptype::set_connection(auto_ptr<ConnectionWrapper>
+            (new ReconnectSocketWrapper("./socket")));
+
+        // the server doens't like the beast
+        t2n_exec(&cmd_group_x_client::serverfunc)(666);
 
-        CPPUNIT_ASSERT_EQUAL(true,true);
+        CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error);
     }