]>
git.saurik.com Git - wxWidgets.git/blob - tests/net/ipc.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/ipc.cpp
3 // Purpose: IPC classes unit tests
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2008 Vadim Zeitlin
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx/wx.h".
17 // FIXME: this tests currently sometimes hangs in Connect() for unknown reason
18 // and this prevents buildbot builds from working so disabling it, but
19 // the real problem needs to be fixed, of course
22 // this test needs threads as it runs the test server in a secondary thread
25 // for all others, include the necessary headers
31 #include "wx/thread.h"
33 #define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)
38 const char *IPC_TEST_PORT
= "4242";
39 const char *IPC_TEST_TOPIC
= "IPC TEST";
41 } // anonymous namespace
43 // ----------------------------------------------------------------------------
44 // test connection class used by IPCTestServer
45 // ----------------------------------------------------------------------------
47 class IPCTestConnection
: public wxConnection
50 IPCTestConnection() { }
52 virtual bool OnExec(const wxString
& topic
, const wxString
& data
)
54 if ( topic
!= IPC_TEST_TOPIC
)
57 return data
== "Date";
61 DECLARE_NO_COPY_CLASS(IPCTestConnection
)
64 // ----------------------------------------------------------------------------
65 // event dispatching thread class
66 // ----------------------------------------------------------------------------
68 class EventThread
: public wxThread
72 : wxThread(wxTHREAD_JOINABLE
)
86 DECLARE_NO_COPY_CLASS(EventThread
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 class IPCTestServer
: public wxServer
100 #if wxUSE_SOCKETS_FOR_IPC
101 // we must call this from the main thread
102 wxSocketBase::Initialize();
103 #endif // wxUSE_SOCKETS_FOR_IPC
105 // we need event dispatching to work for IPC server to work
106 m_thread
= new EventThread
;
108 Create(IPC_TEST_PORT
);
111 virtual ~IPCTestServer()
113 wxTheApp
->ExitMainLoop();
120 #if wxUSE_SOCKETS_FOR_IPC
121 wxSocketBase::Shutdown();
122 #endif // wxUSE_SOCKETS_FOR_IPC
125 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
)
127 if ( topic
!= IPC_TEST_TOPIC
)
130 m_conn
= new IPCTestConnection
;
135 EventThread
*m_thread
;
136 IPCTestConnection
*m_conn
;
138 DECLARE_NO_COPY_CLASS(IPCTestServer
)
141 static IPCTestServer
*gs_server
= NULL
;
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 class IPCTestClient
: public wxClient
155 virtual ~IPCTestClient()
161 Connect(const wxString
& host
, const wxString
& service
, const wxString
& topic
)
163 m_conn
= MakeConnection(host
, service
, topic
);
165 return m_conn
!= NULL
;
177 wxConnectionBase
& GetConn() const
179 CPPUNIT_ASSERT( m_conn
);
185 wxConnectionBase
*m_conn
;
187 DECLARE_NO_COPY_CLASS(IPCTestClient
)
190 static IPCTestClient
*gs_client
= NULL
;
192 // ----------------------------------------------------------------------------
193 // the test code itself
194 // ----------------------------------------------------------------------------
196 class IPCTestCase
: public CppUnit::TestCase
202 CPPUNIT_TEST_SUITE( IPCTestCase
);
203 CPPUNIT_TEST( Connect
);
204 CPPUNIT_TEST( Execute
);
205 CPPUNIT_TEST( Disconnect
);
206 CPPUNIT_TEST_SUITE_END();
212 DECLARE_NO_COPY_CLASS(IPCTestCase
)
215 CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase
);
216 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase
, "IPCTestCase" );
218 void IPCTestCase::Connect()
220 gs_server
= new IPCTestServer
;
221 gs_client
= new IPCTestClient
;
223 // connecting to the wrong port should fail
224 CPPUNIT_ASSERT( !gs_client
->Connect("localhost", "2424", IPC_TEST_TOPIC
) );
226 // connecting using an unsupported topic should fail (unless the server
227 // expects a ROT-13'd topic name...)
228 CPPUNIT_ASSERT( !gs_client
->Connect("localhost", IPC_TEST_PORT
, "VCP GRFG") );
230 // connecting to the right port on the right topic should succeed
231 CPPUNIT_ASSERT( gs_client
->Connect("localhost", IPC_TEST_PORT
, IPC_TEST_TOPIC
) );
234 void IPCTestCase::Execute()
236 wxConnectionBase
& conn
= gs_client
->GetConn();
238 const wxString
s("Date");
239 CPPUNIT_ASSERT( conn
.Execute(s
) );
240 CPPUNIT_ASSERT( conn
.Execute(s
.mb_str(), s
.length() + 1) );
242 char bytes
[] = { 1, 2, 3 };
243 CPPUNIT_ASSERT( conn
.Execute(bytes
, WXSIZEOF(bytes
)) );
246 void IPCTestCase::Disconnect()
250 gs_client
->Disconnect();
262 #endif // wxUSE_THREADS
264 #endif // !__WINDOWS__