]>
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
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
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
23 // this test needs threads as it runs the test server in a secondary thread
26 // for all others, include the necessary headers
32 #include "wx/thread.h"
34 #define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)
39 const char *IPC_TEST_PORT
= "4242";
40 const char *IPC_TEST_TOPIC
= "IPC TEST";
42 } // anonymous namespace
44 // ----------------------------------------------------------------------------
45 // test connection class used by IPCTestServer
46 // ----------------------------------------------------------------------------
48 class IPCTestConnection
: public wxConnection
51 IPCTestConnection() { }
53 virtual bool OnExec(const wxString
& topic
, const wxString
& data
)
55 if ( topic
!= IPC_TEST_TOPIC
)
58 return data
== "Date";
62 DECLARE_NO_COPY_CLASS(IPCTestConnection
)
65 // ----------------------------------------------------------------------------
66 // event dispatching thread class
67 // ----------------------------------------------------------------------------
69 class EventThread
: public wxThread
73 : wxThread(wxTHREAD_JOINABLE
)
87 DECLARE_NO_COPY_CLASS(EventThread
)
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 class IPCTestServer
: public wxServer
101 #if wxUSE_SOCKETS_FOR_IPC
102 // we must call this from the main thread
103 wxSocketBase::Initialize();
104 #endif // wxUSE_SOCKETS_FOR_IPC
106 // we need event dispatching to work for IPC server to work
107 m_thread
= new EventThread
;
109 Create(IPC_TEST_PORT
);
112 virtual ~IPCTestServer()
114 wxTheApp
->ExitMainLoop();
121 #if wxUSE_SOCKETS_FOR_IPC
122 wxSocketBase::Shutdown();
123 #endif // wxUSE_SOCKETS_FOR_IPC
126 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
)
128 if ( topic
!= IPC_TEST_TOPIC
)
131 m_conn
= new IPCTestConnection
;
136 EventThread
*m_thread
;
137 IPCTestConnection
*m_conn
;
139 DECLARE_NO_COPY_CLASS(IPCTestServer
)
142 static IPCTestServer
*gs_server
= NULL
;
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 class IPCTestClient
: public wxClient
156 virtual ~IPCTestClient()
162 Connect(const wxString
& host
, const wxString
& service
, const wxString
& topic
)
164 m_conn
= MakeConnection(host
, service
, topic
);
166 return m_conn
!= NULL
;
178 wxConnectionBase
& GetConn() const
180 CPPUNIT_ASSERT( m_conn
);
186 wxConnectionBase
*m_conn
;
188 DECLARE_NO_COPY_CLASS(IPCTestClient
)
191 static IPCTestClient
*gs_client
= NULL
;
193 // ----------------------------------------------------------------------------
194 // the test code itself
195 // ----------------------------------------------------------------------------
197 class IPCTestCase
: public CppUnit::TestCase
203 CPPUNIT_TEST_SUITE( IPCTestCase
);
204 CPPUNIT_TEST( Connect
);
205 CPPUNIT_TEST( Execute
);
206 CPPUNIT_TEST( Disconnect
);
207 CPPUNIT_TEST_SUITE_END();
213 DECLARE_NO_COPY_CLASS(IPCTestCase
)
216 CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase
);
217 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase
, "IPCTestCase" );
219 void IPCTestCase::Connect()
221 gs_server
= new IPCTestServer
;
222 gs_client
= new IPCTestClient
;
224 // connecting to the wrong port should fail
225 CPPUNIT_ASSERT( !gs_client
->Connect("localhost", "2424", IPC_TEST_TOPIC
) );
227 // connecting using an unsupported topic should fail (unless the server
228 // expects a ROT-13'd topic name...)
229 CPPUNIT_ASSERT( !gs_client
->Connect("localhost", IPC_TEST_PORT
, "VCP GRFG") );
231 // connecting to the right port on the right topic should succeed
232 CPPUNIT_ASSERT( gs_client
->Connect("localhost", IPC_TEST_PORT
, IPC_TEST_TOPIC
) );
235 void IPCTestCase::Execute()
237 wxConnectionBase
& conn
= gs_client
->GetConn();
239 const wxString
s("Date");
240 CPPUNIT_ASSERT( conn
.Execute(s
) );
241 CPPUNIT_ASSERT( conn
.Execute(s
.mb_str(), s
.length() + 1) );
243 char bytes
[] = { 1, 2, 3 };
244 CPPUNIT_ASSERT( conn
.Execute(bytes
, WXSIZEOF(bytes
)) );
247 void IPCTestCase::Disconnect()
251 gs_client
->Disconnect();
263 #endif // wxUSE_THREADS