]>
git.saurik.com Git - wxWidgets.git/blob - tests/net/ipc.cpp
1ac9167f75303b42236e53a2a412fd01d42d1723
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 // this test needs threads as it runs the test server in a secondary thread
21 // for all others, include the necessary headers
27 #include "wx/thread.h"
32 const char *IPC_TEST_PORT
= "4242";
33 const char *IPC_TEST_TOPIC
= "IPC TEST";
35 } // anonymous namespace
37 // ----------------------------------------------------------------------------
38 // test connection class used by IPCTestServer
39 // ----------------------------------------------------------------------------
41 class IPCTestConnection
: public wxConnection
44 IPCTestConnection() { }
46 virtual bool OnExec(const wxString
& topic
, const wxString
& data
)
48 if ( topic
!= IPC_TEST_TOPIC
)
51 return data
== "Date";
55 DECLARE_NO_COPY_CLASS(IPCTestConnection
)
58 // ----------------------------------------------------------------------------
59 // event dispatching thread class
60 // ----------------------------------------------------------------------------
62 class EventThread
: public wxThread
66 : wxThread(wxTHREAD_JOINABLE
)
80 DECLARE_NO_COPY_CLASS(EventThread
)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class IPCTestServer
: public wxServer
94 // we must call this from the main thread
95 wxSocketBase::Initialize();
97 // we need event dispatching to work for IPC server to work
98 m_thread
= new EventThread
;
100 Create(IPC_TEST_PORT
);
103 virtual ~IPCTestServer()
105 wxTheApp
->ExitMainLoop();
112 wxSocketBase::Shutdown();
115 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
)
117 if ( topic
!= IPC_TEST_TOPIC
)
120 m_conn
= new IPCTestConnection
;
125 EventThread
*m_thread
;
126 IPCTestConnection
*m_conn
;
128 DECLARE_NO_COPY_CLASS(IPCTestServer
)
131 static IPCTestServer
*gs_server
= NULL
;
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 class IPCTestClient
: public wxClient
145 virtual ~IPCTestClient()
151 Connect(const wxString
& host
, const wxString
& service
, const wxString
& topic
)
153 m_conn
= MakeConnection(host
, service
, topic
);
155 return m_conn
!= NULL
;
167 wxConnectionBase
& GetConn() const
169 CPPUNIT_ASSERT( m_conn
);
175 wxConnectionBase
*m_conn
;
177 DECLARE_NO_COPY_CLASS(IPCTestClient
)
180 static IPCTestClient gs_client
;
182 // ----------------------------------------------------------------------------
183 // the test code itself
184 // ----------------------------------------------------------------------------
186 class IPCTestCase
: public CppUnit::TestCase
192 CPPUNIT_TEST_SUITE( IPCTestCase
);
193 CPPUNIT_TEST( Connect
);
194 CPPUNIT_TEST( Execute
);
195 CPPUNIT_TEST( Disconnect
);
196 CPPUNIT_TEST_SUITE_END();
202 DECLARE_NO_COPY_CLASS(IPCTestCase
)
205 // this test is not enabled by default because it requires an IPC server to run
206 //CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
207 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase
, "IPCTestCase" );
209 void IPCTestCase::Connect()
211 gs_server
= new IPCTestServer
;
213 // connecting to the wrong port should fail
214 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", "2424", IPC_TEST_TOPIC
) );
216 // connecting using an unsupported topic should fail (unless the server
217 // expects a ROT-13'd topic name...)
218 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", IPC_TEST_PORT
, "VCP GRFG") );
220 // connecting to the right port on the right topic should succeed
221 CPPUNIT_ASSERT( gs_client
.Connect("localhost", IPC_TEST_PORT
, IPC_TEST_TOPIC
) );
224 void IPCTestCase::Execute()
226 wxConnectionBase
& conn
= gs_client
.GetConn();
228 const wxString
s("Date");
229 CPPUNIT_ASSERT( conn
.Execute(s
) );
230 CPPUNIT_ASSERT( conn
.Execute(s
.mb_str(), s
.length() + 1) );
232 char bytes
[] = { 1, 2, 3 };
233 CPPUNIT_ASSERT( conn
.Execute(bytes
, WXSIZEOF(bytes
)) );
236 void IPCTestCase::Disconnect()
238 gs_client
.Disconnect();
247 #endif // wxUSE_THREADS