1212#include " CppUnit/TestCaller.h"
1313#include " CppUnit/TestSuite.h"
1414#include " Poco/Net/UDPServer.h"
15+ #include " Poco/Net/UDPServerParams.h"
1516#include " Poco/Net/UDPClient.h"
1617#include " Poco/Net/UDPHandler.h"
1718#include " Poco/Net/DatagramSocket.h"
@@ -83,16 +84,20 @@ namespace
8384 AtomicCounter TestUDPHandler::errors;
8485
8586 template <typename SRVT, typename CLTT, typename HNDLRT>
86- bool server (int handlerCount, int reps , int port )
87+ bool server (int handlerCount, int clientCount , int reps, const typename SRVT::ServerParams& params )
8788 {
8889 Poco::Net::UDPHandler::List handlers;
8990 for (int i = 0 ; i < handlerCount; ++i)
9091 handlers.push_back (new HNDLRT ());
9192
92- SRVT server (handlers, Poco::Net::SocketAddress ( " 127.0.0.1 " , port) );
93+ SRVT server (handlers, params );
9394 Poco::Thread::sleep (100 );
9495
95- CLTT client (" 127.0.0.1" , server.port (), true );
96+ using ClientPtr = Poco::AutoPtr<CLTT>;
97+ std::vector<ClientPtr> clients;
98+ for (int i = 0 ; i < clientCount; i++)
99+ clients.push_back ( new CLTT (" 127.0.0.1" , server.port (), true ) );
100+
96101 Poco::Thread::sleep (10 );
97102
98103 std::vector<std::string> data;
@@ -105,7 +110,7 @@ namespace
105110 {
106111 data.back ().append (1 , ' \0 ' );
107112 std::size_t sz = (data.size () * strlen (str)) + 1 ;
108- int sent = client. send (Poco::Net::Socket::makeBufVec (data));
113+ int sent = clients[i % clientCount]-> send (Poco::Net::Socket::makeBufVec (data));
109114 if (sz != sent)
110115 {
111116 std::cerr << " Send count mismatch, expected: " << sz
@@ -136,6 +141,7 @@ namespace
136141 << " , received: " << count << std::endl;
137142 return false ;
138143 }
144+ const auto address = clients[0 ]->address ().toString ();
139145 for (const auto & he: handlers)
140146 {
141147 const auto &h = dynamic_cast <const HNDLRT &>(*he);
@@ -148,12 +154,13 @@ namespace
148154 return false ;
149155 }
150156
151- if (h.counter && (h.addr .empty () || h.addr != client.address ().toString ()))
152- {
153- std::cerr << " Address mismatch, expected: " << client.address ().toString ()
154- << " , received: " << h.addr << std::endl;
155- return false ;
156- }
157+ if (clientCount == 1 )
158+ if (h.counter && (h.addr .empty () || h.addr != address))
159+ {
160+ std::cerr << " Address mismatch, expected: " << address
161+ << " , received: " << h.addr << std::endl;
162+ return false ;
163+ }
157164 }
158165 return true ;
159166 }
@@ -173,9 +180,11 @@ UDPServerTest::~UDPServerTest()
173180void UDPServerTest::testUDPSingleSocket ()
174181{
175182 TestUDPHandler::errors = 0 ;
176- int msgs = 10000 ;
183+ int msgs = 10000 ;
184+ Poco::Net::UDPServerParams params (Poco::Net::SocketAddress (" 127.0.0.1" , 0 ));
185+
177186 auto tf = server<Poco::Net::UDPServer, Poco::Net::UDPClient, TestUDPHandler>;
178- assertTrue ( tf (1 , msgs, 0 ) );
187+ assertTrue ( tf (1 , 1 , msgs, params ) );
179188 assertTrue (TestUDPHandler::errors == 0 );
180189}
181190
@@ -184,8 +193,10 @@ void UDPServerTest::testUDPMultiSocket()
184193{
185194 TestUDPHandler::errors = 0 ;
186195 int msgs = 10000 ;
196+ Poco::Net::UDPServerParams params (Poco::Net::SocketAddress (" 127.0.0.1" , 22080 ));
197+
187198 auto tf = server<Poco::Net::UDPMultiServer, Poco::Net::UDPClient, TestUDPHandler>;
188- assertTrue ( tf (1 , msgs, 22080 ) );
199+ assertTrue ( tf (1 , 1 , msgs, params ) );
189200 assertTrue (TestUDPHandler::errors == 0 );
190201}
191202
@@ -194,8 +205,10 @@ void UDPServerTest::testUDPSingleSocketMultipleHandlers()
194205{
195206 TestUDPHandler::errors = 0 ;
196207 int msgs = 10000 ;
208+ Poco::Net::UDPServerParams params (Poco::Net::SocketAddress (" 127.0.0.1" , 0 ));
209+
197210 auto tf = server<Poco::Net::UDPServer, Poco::Net::UDPClient, TestUDPHandler>;
198- assertTrue ( tf (10 , msgs, 0 ) );
211+ assertTrue ( tf (10 , 1 , msgs, params ) );
199212 assertTrue (TestUDPHandler::errors == 0 );
200213}
201214
@@ -204,8 +217,52 @@ void UDPServerTest::testUDPMultiSocketMultipleHandlers()
204217{
205218 TestUDPHandler::errors = 0 ;
206219 int msgs = 10000 ;
220+ Poco::Net::UDPServerParams params (Poco::Net::SocketAddress (" 127.0.0.1" , 22080 ));
221+
222+ auto tf = server<Poco::Net::UDPMultiServer, Poco::Net::UDPClient, TestUDPHandler>;
223+ assertTrue ( tf (10 , 1 , msgs, params) );
224+ assertTrue (TestUDPHandler::errors == 0 );
225+ }
226+
227+
228+ void UDPServerTest::testUDPMultiSocketMultipleHandlersLessSockets ()
229+ {
230+ TestUDPHandler::errors = 0 ;
231+ int msgs = 10000 ;
232+ Poco::Net::UDPServerParams params (
233+ Poco::Net::SocketAddress (" 127.0.0.1" , 22080 ),
234+ 2
235+ );
236+
207237 auto tf = server<Poco::Net::UDPMultiServer, Poco::Net::UDPClient, TestUDPHandler>;
208- assertTrue ( tf (10 , msgs, 22080 ) );
238+ assertTrue ( tf (10 , 1 , msgs, params) );
239+ assertTrue (TestUDPHandler::errors == 0 );
240+ }
241+
242+
243+ void UDPServerTest::testUDPMultiSocketMultipleHandlersMoreSockets ()
244+ {
245+ TestUDPHandler::errors = 0 ;
246+ int msgs = 10000 ;
247+ Poco::Net::UDPServerParams params (
248+ Poco::Net::SocketAddress (" 127.0.0.1" , 22080 ),
249+ 10
250+ );
251+
252+ auto tf = server<Poco::Net::UDPMultiServer, Poco::Net::UDPClient, TestUDPHandler>;
253+ assertTrue ( tf (2 , 1 , msgs, params) );
254+ assertTrue (TestUDPHandler::errors == 0 );
255+ }
256+
257+
258+ void UDPServerTest::testUDPMultiSocketMultipleHandlersMultipleClients ()
259+ {
260+ TestUDPHandler::errors = 0 ;
261+ int msgs = 50000 ;
262+ Poco::Net::UDPServerParams params (Poco::Net::SocketAddress (" 127.0.0.1" , 0 ));
263+
264+ auto tf = server<Poco::Net::UDPServer, Poco::Net::UDPClient, TestUDPHandler>;
265+ assertTrue ( tf (10 , 5 , msgs, params) );
209266 assertTrue (TestUDPHandler::errors == 0 );
210267}
211268
@@ -228,6 +285,9 @@ CppUnit::Test* UDPServerTest::suite()
228285 CppUnit_addTest (pSuite, UDPServerTest, testUDPMultiSocket);
229286 CppUnit_addTest (pSuite, UDPServerTest, testUDPSingleSocketMultipleHandlers);
230287 CppUnit_addTest (pSuite, UDPServerTest, testUDPMultiSocketMultipleHandlers);
288+ CppUnit_addTest (pSuite, UDPServerTest, testUDPMultiSocketMultipleHandlersLessSockets);
289+ CppUnit_addTest (pSuite, UDPServerTest, testUDPMultiSocketMultipleHandlersMoreSockets);
290+ CppUnit_addTest (pSuite, UDPServerTest, testUDPMultiSocketMultipleHandlersMultipleClients);
231291
232292 return pSuite;
233293}
0 commit comments