]> git.saurik.com Git - wxWidgets.git/commitdiff
added a simple IPC unit test
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 14 Jul 2008 02:19:34 +0000 (02:19 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 14 Jul 2008 02:19:34 +0000 (02:19 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54614 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

tests/Makefile.in
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/net/ipc.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj

index 6d50737e88a18e2e5c9c697a09a0846a378e6eea..7de6fe5e8817717609cd39b65b200c6a6888d576 100644 (file)
@@ -71,6 +71,7 @@ TEST_OBJECTS =  \
        test_longlongtest.o \
        test_convautotest.o \
        test_mbconvtest.o \
+       test_ipc.o \
        test_regextest.o \
        test_wxregextest.o \
        test_scopeguardtest.o \
@@ -394,6 +395,9 @@ test_convautotest.o: $(srcdir)/mbconv/convautotest.cpp $(TEST_ODEP)
 test_mbconvtest.o: $(srcdir)/mbconv/mbconvtest.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/mbconv/mbconvtest.cpp
 
+test_ipc.o: $(srcdir)/net/ipc.cpp $(TEST_ODEP)
+       $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/ipc.cpp
+
 test_regextest.o: $(srcdir)/regex/regextest.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/regex/regextest.cpp
 
index 4852c79e4fad8a2423a780e91c3084ee295b1032..f5d622f02e47add812a7036f845e3a9b81603b3e 100644 (file)
@@ -57,6 +57,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.obj \
        $(OBJS)\test_convautotest.obj \
        $(OBJS)\test_mbconvtest.obj \
+       $(OBJS)\test_ipc.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
        $(OBJS)\test_scopeguardtest.obj \
@@ -423,6 +424,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp
 
+$(OBJS)\test_ipc.obj: .\net\ipc.cpp
+       $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
+
 $(OBJS)\test_regextest.obj: .\regex\regextest.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
 
index fabe0d9fa19e93de3bd9dcaab8e8befedf9ba32f..e53f54bb2eed751b352a6f2d148bc7123b48092f 100644 (file)
@@ -49,6 +49,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.o \
        $(OBJS)\test_convautotest.o \
        $(OBJS)\test_mbconvtest.o \
+       $(OBJS)\test_ipc.o \
        $(OBJS)\test_regextest.o \
        $(OBJS)\test_wxregextest.o \
        $(OBJS)\test_scopeguardtest.o \
@@ -401,6 +402,9 @@ $(OBJS)\test_convautotest.o: ./mbconv/convautotest.cpp
 $(OBJS)\test_mbconvtest.o: ./mbconv/mbconvtest.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
+$(OBJS)\test_ipc.o: ./net/ipc.cpp
+       $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+
 $(OBJS)\test_regextest.o: ./regex/regextest.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
index 6776a9f083e4829224b783d4650e479ed672ea30..225bda8f92672f2f930b0ce063e948f45d02cbcb 100644 (file)
@@ -50,6 +50,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.obj \
        $(OBJS)\test_convautotest.obj \
        $(OBJS)\test_mbconvtest.obj \
+       $(OBJS)\test_ipc.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
        $(OBJS)\test_scopeguardtest.obj \
@@ -508,6 +509,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp
 
+$(OBJS)\test_ipc.obj: .\net\ipc.cpp
+       $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
+
 $(OBJS)\test_regextest.obj: .\regex\regextest.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
 
index 582af283a0e8f4043ea60f022804b330f0e58e17..c177331470876d3b65b860f9f6af609fa831563d 100644 (file)
@@ -262,6 +262,7 @@ TEST_OBJECTS =  &
        $(OBJS)\test_longlongtest.obj &
        $(OBJS)\test_convautotest.obj &
        $(OBJS)\test_mbconvtest.obj &
+       $(OBJS)\test_ipc.obj &
        $(OBJS)\test_regextest.obj &
        $(OBJS)\test_wxregextest.obj &
        $(OBJS)\test_scopeguardtest.obj &
@@ -454,6 +455,9 @@ $(OBJS)\test_convautotest.obj :  .AUTODEPEND .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj :  .AUTODEPEND .\mbconv\mbconvtest.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
+$(OBJS)\test_ipc.obj :  .AUTODEPEND .\net\ipc.cpp
+       $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+
 $(OBJS)\test_regextest.obj :  .AUTODEPEND .\regex\regextest.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
diff --git a/tests/net/ipc.cpp b/tests/net/ipc.cpp
new file mode 100644 (file)
index 0000000..5ca82bd
--- /dev/null
@@ -0,0 +1,245 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/net/ipc.cpp
+// Purpose:     IPC classes unit tests
+// Author:      Vadim Zeitlin
+// RCS-ID:      $Id$
+// Copyright:   (c) 2008 Vadim Zeitlin
+// Licence:     wxWidgets licence
+///////////////////////////////////////////////////////////////////////////////
+
+// For compilers that support precompilation, includes "wx/wx.h".
+// and "wx/cppunit.h"
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+// for all others, include the necessary headers
+#ifndef WX_PRECOMP
+#endif
+
+// this test needs threads as it runs the test server in a secondary thread
+#if wxUSE_THREADS
+
+#include "wx/ipc.h"
+#include "wx/thread.h"
+
+namespace
+{
+
+const char *IPC_TEST_PORT = "4242";
+const char *IPC_TEST_TOPIC = "IPC TEST";
+
+} // anonymous namespace
+
+// ----------------------------------------------------------------------------
+// test connection class used by IPCTestServer
+// ----------------------------------------------------------------------------
+
+class IPCTestConnection : public wxConnection
+{
+public:
+    IPCTestConnection() { }
+
+    virtual bool OnExec(const wxString& topic, const wxString& data)
+    {
+        if ( topic != IPC_TEST_TOPIC )
+            return false;
+
+        return data == "Date";
+    }
+
+private:
+    DECLARE_NO_COPY_CLASS(IPCTestConnection)
+};
+
+// ----------------------------------------------------------------------------
+// event dispatching thread class
+// ----------------------------------------------------------------------------
+
+class EventThread : public wxThread
+{
+public:
+    EventThread()
+        : wxThread(wxTHREAD_JOINABLE)
+    {
+        Create();
+        Run();
+    }
+
+protected:
+    virtual void *Entry()
+    {
+        wxTheApp->MainLoop();
+
+        return NULL;
+    }
+
+    DECLARE_NO_COPY_CLASS(EventThread)
+};
+
+// ----------------------------------------------------------------------------
+// test server class
+// ----------------------------------------------------------------------------
+
+class IPCTestServer : public wxServer
+{
+public:
+    IPCTestServer()
+    {
+        m_conn = NULL;
+
+        // we must call this from the main thread
+        wxSocketBase::Initialize();
+
+        // we need event dispatching to work for IPC server to work
+        m_thread = new EventThread;
+
+        Create(IPC_TEST_PORT);
+    }
+
+    virtual ~IPCTestServer()
+    {
+        wxTheApp->ExitMainLoop();
+
+        m_thread->Wait();
+        delete m_thread;
+        m_thread = NULL;
+
+        wxSocketBase::Shutdown();
+    }
+
+    virtual wxConnectionBase *OnAcceptConnection(const wxString& topic)
+    {
+        if ( topic != IPC_TEST_TOPIC )
+            return NULL;
+
+        m_conn = new IPCTestConnection;
+        return m_conn;
+    }
+
+private:
+    EventThread *m_thread;
+    IPCTestConnection *m_conn;
+
+    DECLARE_NO_COPY_CLASS(IPCTestServer)
+};
+
+static IPCTestServer *gs_server = NULL;
+
+// ----------------------------------------------------------------------------
+// test client class
+// ----------------------------------------------------------------------------
+
+class IPCTestClient : public wxClient
+{
+public:
+    IPCTestClient()
+    {
+        m_conn = NULL;
+    }
+
+    virtual ~IPCTestClient()
+    {
+        Disconnect();
+    }
+
+    bool
+    Connect(const wxString& host, const wxString& service, const wxString& topic)
+    {
+        m_conn = MakeConnection(host, service, topic);
+
+        return m_conn != NULL;
+    }
+
+    void Disconnect()
+    {
+        if ( m_conn )
+        {
+            delete m_conn;
+            m_conn = NULL;
+        }
+    }
+
+    wxConnectionBase& GetConn() const
+    {
+        CPPUNIT_ASSERT( m_conn );
+
+        return *m_conn;
+    }
+
+private:
+    wxConnectionBase *m_conn;
+
+    DECLARE_NO_COPY_CLASS(IPCTestClient)
+};
+
+static IPCTestClient gs_client;
+
+// ----------------------------------------------------------------------------
+// the test code itself
+// ----------------------------------------------------------------------------
+
+class IPCTestCase : public CppUnit::TestCase
+{
+public:
+    IPCTestCase() { }
+
+private:
+    CPPUNIT_TEST_SUITE( IPCTestCase );
+        CPPUNIT_TEST( Connect );
+        CPPUNIT_TEST( Execute );
+        CPPUNIT_TEST( Disconnect );
+    CPPUNIT_TEST_SUITE_END();
+
+    void Connect();
+    void Execute();
+    void Disconnect();
+
+    DECLARE_NO_COPY_CLASS(IPCTestCase)
+};
+
+// this test is not enabled by default because it requires an IPC server to run
+//CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase, "IPCTestCase" );
+
+void IPCTestCase::Connect()
+{
+    gs_server = new IPCTestServer;
+
+    // connecting to the wrong port should fail
+    CPPUNIT_ASSERT( !gs_client.Connect("localhost", "2424", IPC_TEST_TOPIC) );
+
+    // connecting using an unsupported topic should fail (unless the server
+    // expects a ROT-13'd topic name...)
+    CPPUNIT_ASSERT( !gs_client.Connect("localhost", IPC_TEST_PORT, "VCP GRFG") );
+
+    // connecting to the right port on the right topic should succeed
+    CPPUNIT_ASSERT( gs_client.Connect("localhost", IPC_TEST_PORT, IPC_TEST_TOPIC) );
+}
+
+void IPCTestCase::Execute()
+{
+    wxConnectionBase& conn = gs_client.GetConn();
+
+    const wxString s("Date");
+    CPPUNIT_ASSERT( conn.Execute(s) );
+    CPPUNIT_ASSERT( conn.Execute(s.mb_str(), s.length() + 1) );
+
+    char bytes[] = { 1, 2, 3 };
+    CPPUNIT_ASSERT( conn.Execute(bytes, WXSIZEOF(bytes)) );
+}
+
+void IPCTestCase::Disconnect()
+{
+    gs_client.Disconnect();
+
+    if ( gs_server )
+    {
+        delete gs_server;
+        gs_server = NULL;
+    }
+}
+
+#endif // wxUSE_THREADS
index 05fb5a75afb5df811e283df3fdfe21cf54eecc3c..34e49912ab0c68550acbec9507c1eb56a86c55f2 100644 (file)
@@ -43,6 +43,7 @@
             longlong/longlongtest.cpp
             mbconv/convautotest.cpp
             mbconv/mbconvtest.cpp
+            net/ipc.cpp
             regex/regextest.cpp
             regex/wxregextest.cpp
             scopeguard/scopeguardtest.cpp
index c54d2c6954cd0224fb22ba56a9c443f18639fbc8..f7d86fa6864f9b6f3340f6dc29c5c2edeb21f032 100644 (file)
@@ -337,6 +337,10 @@ SOURCE=.\streams\iostreams.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\net\ipc.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\streams\largefile.cpp\r
 # End Source File\r
 # Begin Source File\r
index 2e19cd5bf788f7fb79ee01a9f3fd5928d22983e6..63fdfa870177f1407439bcc6f323a94150e48af8 100644 (file)
                                RelativePath=".\strings\iostream.cpp"/>\r
                        <File\r
                                RelativePath=".\streams\iostreams.cpp"/>\r
+                       <File\r
+                               RelativePath=".\net\ipc.cpp"/>\r
                        <File\r
                                RelativePath=".\streams\largefile.cpp"/>\r
                        <File\r
index 10c1bc2f51ed1496044223ee0f2ca2fea66fcfae..d20c74ab47aaa24ca699d52ac404692079910b3a 100644 (file)
                        <File\r
                                RelativePath=".\streams\iostreams.cpp"\r
                        />\r
+                       <File\r
+                               RelativePath=".\net\ipc.cpp"\r
+                       />\r
                        <File\r
                                RelativePath=".\streams\largefile.cpp"\r
                        />\r