]> git.saurik.com Git - wxWidgets.git/blame - tests/net/ipc.cpp
Updated translations manual page with latest status given by the website translations...
[wxWidgets.git] / tests / net / ipc.cpp
CommitLineData
e7ce668e
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/net/ipc.cpp
3// Purpose: IPC classes unit tests
4// Author: Vadim Zeitlin
5// RCS-ID: $Id$
6// Copyright: (c) 2008 Vadim Zeitlin
7// Licence: wxWidgets licence
8///////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx/wx.h".
11// and "wx/cppunit.h"
12#include "testprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
68baf10e
VZ
18// FIXME: this tests currently hangs under Windows and this prevents buildbot
19// builds from working so disabling it, but the real problem needs to
20// be fixed, of course
21#ifndef __WXMSW__
22
4c141c5a
VZ
23// this test needs threads as it runs the test server in a secondary thread
24#if wxUSE_THREADS
25
e7ce668e
VZ
26// for all others, include the necessary headers
27#ifndef WX_PRECOMP
4c141c5a 28 #include "wx/app.h"
e7ce668e
VZ
29#endif
30
e7ce668e
VZ
31#include "wx/ipc.h"
32#include "wx/thread.h"
33
02ab8d3a
VZ
34#define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)
35
e7ce668e
VZ
36namespace
37{
38
39const char *IPC_TEST_PORT = "4242";
40const char *IPC_TEST_TOPIC = "IPC TEST";
41
42} // anonymous namespace
43
44// ----------------------------------------------------------------------------
45// test connection class used by IPCTestServer
46// ----------------------------------------------------------------------------
47
48class IPCTestConnection : public wxConnection
49{
50public:
51 IPCTestConnection() { }
52
53 virtual bool OnExec(const wxString& topic, const wxString& data)
54 {
55 if ( topic != IPC_TEST_TOPIC )
56 return false;
57
58 return data == "Date";
59 }
60
61private:
62 DECLARE_NO_COPY_CLASS(IPCTestConnection)
63};
64
65// ----------------------------------------------------------------------------
66// event dispatching thread class
67// ----------------------------------------------------------------------------
68
69class EventThread : public wxThread
70{
71public:
72 EventThread()
73 : wxThread(wxTHREAD_JOINABLE)
74 {
75 Create();
76 Run();
77 }
78
79protected:
80 virtual void *Entry()
81 {
82 wxTheApp->MainLoop();
83
84 return NULL;
85 }
86
87 DECLARE_NO_COPY_CLASS(EventThread)
88};
89
90// ----------------------------------------------------------------------------
91// test server class
92// ----------------------------------------------------------------------------
93
94class IPCTestServer : public wxServer
95{
96public:
97 IPCTestServer()
98 {
99 m_conn = NULL;
100
02ab8d3a 101#if wxUSE_SOCKETS_FOR_IPC
e7ce668e
VZ
102 // we must call this from the main thread
103 wxSocketBase::Initialize();
02ab8d3a 104#endif // wxUSE_SOCKETS_FOR_IPC
e7ce668e
VZ
105
106 // we need event dispatching to work for IPC server to work
107 m_thread = new EventThread;
108
109 Create(IPC_TEST_PORT);
110 }
111
112 virtual ~IPCTestServer()
113 {
114 wxTheApp->ExitMainLoop();
115
116 m_thread->Wait();
117 delete m_thread;
4c141c5a
VZ
118
119 delete m_conn;
e7ce668e 120
02ab8d3a 121#if wxUSE_SOCKETS_FOR_IPC
e7ce668e 122 wxSocketBase::Shutdown();
02ab8d3a 123#endif // wxUSE_SOCKETS_FOR_IPC
e7ce668e
VZ
124 }
125
126 virtual wxConnectionBase *OnAcceptConnection(const wxString& topic)
127 {
128 if ( topic != IPC_TEST_TOPIC )
129 return NULL;
130
131 m_conn = new IPCTestConnection;
132 return m_conn;
133 }
134
135private:
136 EventThread *m_thread;
137 IPCTestConnection *m_conn;
138
139 DECLARE_NO_COPY_CLASS(IPCTestServer)
140};
141
142static IPCTestServer *gs_server = NULL;
143
144// ----------------------------------------------------------------------------
145// test client class
146// ----------------------------------------------------------------------------
147
148class IPCTestClient : public wxClient
149{
150public:
151 IPCTestClient()
152 {
153 m_conn = NULL;
154 }
155
156 virtual ~IPCTestClient()
157 {
158 Disconnect();
159 }
160
161 bool
162 Connect(const wxString& host, const wxString& service, const wxString& topic)
163 {
164 m_conn = MakeConnection(host, service, topic);
165
166 return m_conn != NULL;
167 }
168
169 void Disconnect()
170 {
171 if ( m_conn )
172 {
173 delete m_conn;
174 m_conn = NULL;
175 }
176 }
177
178 wxConnectionBase& GetConn() const
179 {
180 CPPUNIT_ASSERT( m_conn );
181
182 return *m_conn;
183 }
184
185private:
186 wxConnectionBase *m_conn;
187
188 DECLARE_NO_COPY_CLASS(IPCTestClient)
189};
190
76a10a0d 191static IPCTestClient *gs_client = NULL;
e7ce668e
VZ
192
193// ----------------------------------------------------------------------------
194// the test code itself
195// ----------------------------------------------------------------------------
196
197class IPCTestCase : public CppUnit::TestCase
198{
199public:
200 IPCTestCase() { }
201
202private:
203 CPPUNIT_TEST_SUITE( IPCTestCase );
204 CPPUNIT_TEST( Connect );
205 CPPUNIT_TEST( Execute );
206 CPPUNIT_TEST( Disconnect );
207 CPPUNIT_TEST_SUITE_END();
208
209 void Connect();
210 void Execute();
211 void Disconnect();
212
213 DECLARE_NO_COPY_CLASS(IPCTestCase)
214};
215
02ab8d3a 216CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
e7ce668e
VZ
217CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase, "IPCTestCase" );
218
219void IPCTestCase::Connect()
220{
221 gs_server = new IPCTestServer;
76a10a0d 222 gs_client = new IPCTestClient;
e7ce668e
VZ
223
224 // connecting to the wrong port should fail
76a10a0d 225 CPPUNIT_ASSERT( !gs_client->Connect("localhost", "2424", IPC_TEST_TOPIC) );
e7ce668e
VZ
226
227 // connecting using an unsupported topic should fail (unless the server
228 // expects a ROT-13'd topic name...)
76a10a0d 229 CPPUNIT_ASSERT( !gs_client->Connect("localhost", IPC_TEST_PORT, "VCP GRFG") );
e7ce668e
VZ
230
231 // connecting to the right port on the right topic should succeed
76a10a0d 232 CPPUNIT_ASSERT( gs_client->Connect("localhost", IPC_TEST_PORT, IPC_TEST_TOPIC) );
e7ce668e
VZ
233}
234
235void IPCTestCase::Execute()
236{
76a10a0d 237 wxConnectionBase& conn = gs_client->GetConn();
e7ce668e
VZ
238
239 const wxString s("Date");
240 CPPUNIT_ASSERT( conn.Execute(s) );
241 CPPUNIT_ASSERT( conn.Execute(s.mb_str(), s.length() + 1) );
242
243 char bytes[] = { 1, 2, 3 };
244 CPPUNIT_ASSERT( conn.Execute(bytes, WXSIZEOF(bytes)) );
245}
246
247void IPCTestCase::Disconnect()
248{
76a10a0d
VZ
249 if ( gs_client )
250 {
251 gs_client->Disconnect();
252 delete gs_client;
253 gs_client = NULL;
254 }
e7ce668e
VZ
255
256 if ( gs_server )
257 {
258 delete gs_server;
259 gs_server = NULL;
260 }
261}
262
263#endif // wxUSE_THREADS
68baf10e
VZ
264
265#endif // !__WXMSW__