Remove code duplication in test fixtures
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 4 Feb 2010 15:38:18 +0000 (16:38 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 4 Feb 2010 15:38:18 +0000 (16:38 +0100)
test/hello.cpp
test/reconnect.cpp
test/test_fixtures.hxx
test/timeout.cpp
test/wrapper.cpp

index d53a312..6bce679 100644 (file)
@@ -49,43 +49,8 @@ on this file might be covered by the GNU General Public License.
 using namespace std;
 using namespace libt2n;
 
-// this is an evil hack to get access to real_write, don't ever do this in an app!!!
-class real_write_connection: public socket_server_connection
-{
-    public:
-        void real_write(const std::string& data)
-            { socket_write(data); }
-};
-
-class test_helloFixture : public KillChildOnShutdownFixture
-{
-protected:
-    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 send_raw_socket(string hello_string, socket_server* ss, int conn_id)
-    {
-        socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
-
-        // this is an evil hack to get access to real_write, don't ever do this in an app!!!
-        real_write_connection *rwc=(real_write_connection*)ssc;
-        rwc->real_write(hello_string);
-    }
-
-public:
-    test_helloFixture()
-    {
-    }
-
-    ~test_helloFixture()
-    {
-    }
-};
 
-BOOST_FIXTURE_TEST_SUITE(test_hello, test_helloFixture)
+BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture)
 
 BOOST_AUTO_TEST_CASE(HelloOk)
 {
index 6c70458..80fc57f 100644 (file)
@@ -46,37 +46,7 @@ on this file might be covered by the GNU General Public License.
 using namespace std;
 using namespace libt2n;
 
-// this is an evil hack to get access to real_write, don't ever do this in an app!!!
-class real_write_connection: public socket_server_connection
-{
-    public:
-        void real_write(const std::string& data)
-            { socket_write(data); }
-};
-
-class test_reconnectFixture : public KillChildOnShutdownFixture
-{
-protected:
-    void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
-    {
-        socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
-
-        // this is an evil hack to get access to real_write, don't ever do this in an app!!!
-        real_write_connection *rwc=(real_write_connection*)ssc;
-        rwc->real_write(hello_string);
-    }
-
-public:
-    test_reconnectFixture()
-    {
-    }
-
-    ~test_reconnectFixture()
-    {
-    }
-};
-
-BOOST_FIXTURE_TEST_SUITE(test_reconnect, test_reconnectFixture)
+BOOST_FIXTURE_TEST_SUITE(test_reconnect, KillChildOnShutdownFixture)
 
 BOOST_AUTO_TEST_CASE(simple_reconnect)
 {
index 882457c..255202e 100644 (file)
@@ -26,11 +26,40 @@ on this file might be covered by the GNU General Public License.
 #include <sys/types.h>
 #include <signal.h>
 
+#include <socket_server.hxx>
+#include <string>
+
 class KillChildOnShutdownFixture
 {
 protected:
     pid_t child_pid;
 
+    // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+    class real_write_connection: public libt2n::socket_server_connection
+    {
+        public:
+            void real_write(const std::string& data)
+                { socket_write(data); }
+    };
+
+    //
+    // common functions used by some tests
+    //
+    void send_hello(std::string hello_string, libt2n::socket_server* ss, int conn_id)
+    {
+        libt2n::server_connection *sc=ss->get_connection(conn_id);
+        sc->write(hello_string);
+    }
+
+    void send_raw_socket(std::string hello_string, libt2n::socket_server* ss, int conn_id)
+    {
+        libt2n::socket_server_connection *ssc=dynamic_cast<libt2n::socket_server_connection*>(ss->get_connection(conn_id));
+
+        // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+        real_write_connection *rwc=(real_write_connection*)ssc;
+        rwc->real_write(hello_string);
+    }
+
 public:
     KillChildOnShutdownFixture()
         : child_pid(0)
index 3434c0b..8f67cac 100644 (file)
@@ -127,33 +127,18 @@ class testfunc2_cmd : public libt2n::command
 BOOST_CLASS_EXPORT(testfunc2_cmd)
 BOOST_CLASS_EXPORT(testfunc2_res)
 
-// this is an evil hack to get access to real_write, don't ever do this in an app!!!
-class real_write_connection: public socket_server_connection
-{
-    public:
-        void real_write(const std::string& data)
-            { socket_write(data); }
-};
-
-// this is an evil hack to get access to real_write, don't ever do this in an app!!!
-class real_write_client_connection: public socket_client_connection
-{
-    public:
-        void real_write(const std::string& data)
-            { socket_write(data); }
-};
-
-
 class test_timeoutFixture : public KillChildOnShutdownFixture
 {
 protected:
     typedef uint32_t packet_size_indicator;
 
-    void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
+    // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+    class real_write_client_connection: public socket_client_connection
     {
-        server_connection *sc=ss->get_connection(conn_id);
-        sc->write(hello_string);
-    }
+        public:
+            void real_write(const std::string& data)
+                { socket_write(data); }
+    };
 
     void send_slow_raw_socket(string data, socket_server* ss, unsigned int conn_id)
     {
@@ -171,15 +156,6 @@ protected:
             usleep(200000);
         }
     }
-
-public:
-    test_timeoutFixture()
-    {
-    }
-
-    ~test_timeoutFixture()
-    {
-    }
 };
 
 BOOST_FIXTURE_TEST_SUITE(test_timeout, test_timeoutFixture)
index e4bffb6..b1d30cd 100644 (file)
@@ -424,30 +424,10 @@ BOOST_AUTO_TEST_CASE(ignore_handler_reconnects)
 
     BOOST_CHECK_EQUAL(2,cnt);
 }
-BOOST_AUTO_TEST_SUITE_END()
-
-
-
-class test_wrapper_noserverFixture : public KillChildOnShutdownFixture
-{
-protected:
-    void send_hello(string hello_string, socket_server* ss, int conn_id)
-    {
-        server_connection *sc=ss->get_connection(conn_id);
-        sc->write(hello_string);
-    }
 
-public:
-    test_wrapper_noserverFixture()
-    {
-    }
-
-    ~test_wrapper_noserverFixture()
-    {
-    }
-};
+BOOST_AUTO_TEST_SUITE_END()
 
-BOOST_FIXTURE_TEST_SUITE(test_wrapper_noserver, test_wrapper_noserverFixture)
+BOOST_FIXTURE_TEST_SUITE(test_wrapper_noserver, KillChildOnShutdownFixture)
 
 BOOST_AUTO_TEST_CASE(ignore_noserver)
 {