1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/ipc.cpp
3 // Purpose: IPC classes unit tests
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
18 // for all others, include the necessary headers
22 // this test needs threads as it runs the test server in a secondary thread
26 #include "wx/thread.h"
31 const char *IPC_TEST_PORT
= "4242";
32 const char *IPC_TEST_TOPIC
= "IPC TEST";
34 } // anonymous namespace
36 // ----------------------------------------------------------------------------
37 // test connection class used by IPCTestServer
38 // ----------------------------------------------------------------------------
40 class IPCTestConnection
: public wxConnection
43 IPCTestConnection() { }
45 virtual bool OnExec(const wxString
& topic
, const wxString
& data
)
47 if ( topic
!= IPC_TEST_TOPIC
)
50 return data
== "Date";
54 DECLARE_NO_COPY_CLASS(IPCTestConnection
)
57 // ----------------------------------------------------------------------------
58 // event dispatching thread class
59 // ----------------------------------------------------------------------------
61 class EventThread
: public wxThread
65 : wxThread(wxTHREAD_JOINABLE
)
79 DECLARE_NO_COPY_CLASS(EventThread
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class IPCTestServer
: public wxServer
93 // we must call this from the main thread
94 wxSocketBase
::Initialize();
96 // we need event dispatching to work for IPC server to work
97 m_thread
= new EventThread
;
99 Create(IPC_TEST_PORT
);
102 virtual ~IPCTestServer()
104 wxTheApp
->ExitMainLoop();
110 wxSocketBase
::Shutdown();
113 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
)
115 if ( topic
!= IPC_TEST_TOPIC
)
118 m_conn
= new IPCTestConnection
;
123 EventThread
*m_thread
;
124 IPCTestConnection
*m_conn
;
126 DECLARE_NO_COPY_CLASS(IPCTestServer
)
129 static IPCTestServer
*gs_server
= NULL
;
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class IPCTestClient
: public wxClient
143 virtual ~IPCTestClient()
149 Connect(const wxString
& host
, const wxString
& service
, const wxString
& topic
)
151 m_conn
= MakeConnection(host
, service
, topic
);
153 return m_conn
!= NULL
;
165 wxConnectionBase
& GetConn() const
167 CPPUNIT_ASSERT( m_conn
);
173 wxConnectionBase
*m_conn
;
175 DECLARE_NO_COPY_CLASS(IPCTestClient
)
178 static IPCTestClient gs_client
;
180 // ----------------------------------------------------------------------------
181 // the test code itself
182 // ----------------------------------------------------------------------------
184 class IPCTestCase
: public CppUnit
::TestCase
190 CPPUNIT_TEST_SUITE( IPCTestCase
);
191 CPPUNIT_TEST( Connect
);
192 CPPUNIT_TEST( Execute
);
193 CPPUNIT_TEST( Disconnect
);
194 CPPUNIT_TEST_SUITE_END();
200 DECLARE_NO_COPY_CLASS(IPCTestCase
)
203 // this test is not enabled by default because it requires an IPC server to run
204 //CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
205 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase
, "IPCTestCase" );
207 void IPCTestCase
::Connect()
209 gs_server
= new IPCTestServer
;
211 // connecting to the wrong port should fail
212 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", "2424", IPC_TEST_TOPIC
) );
214 // connecting using an unsupported topic should fail (unless the server
215 // expects a ROT-13'd topic name...)
216 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", IPC_TEST_PORT
, "VCP GRFG") );
218 // connecting to the right port on the right topic should succeed
219 CPPUNIT_ASSERT( gs_client
.Connect("localhost", IPC_TEST_PORT
, IPC_TEST_TOPIC
) );
222 void IPCTestCase
::Execute()
224 wxConnectionBase
& conn
= gs_client
.GetConn();
226 const wxString
s("Date");
227 CPPUNIT_ASSERT( conn
.Execute(s
) );
228 CPPUNIT_ASSERT( conn
.Execute(s
.mb_str(), s
.length() + 1) );
230 char bytes
[] = { 1, 2, 3 };
231 CPPUNIT_ASSERT( conn
.Execute(bytes
, WXSIZEOF(bytes
)) );
234 void IPCTestCase
::Disconnect()
236 gs_client
.Disconnect();
245 #endif // wxUSE_THREADS