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