Skip to content

Commit a57b329

Browse files
committed
test(UDPServer): add more unit tests with different parameters
1 parent 8cc4860 commit a57b329

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

Net/include/Poco/Net/UDPClient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
#include "Poco/Net/SocketAddress.h"
2323
#include "Poco/Net/DatagramSocket.h"
2424
#include "Poco/Runnable.h"
25+
#include "Poco/RefCountedObject.h"
2526
#include "Poco/Thread.h"
2627

2728

2829
namespace Poco {
2930
namespace Net {
3031

3132

32-
class Net_API UDPClient : public Poco::Runnable
33+
class Net_API UDPClient : public Poco::Runnable, public RefCountedObject
3334
/// UDP client can either send, or send/receive UDP packets.
3435
/// The mode of operation is specified at construction time.
3536
/// If receiving functionality is enabled, it will run in a
@@ -42,7 +43,7 @@ class Net_API UDPClient : public Poco::Runnable
4243
UDPClient(const std::string& address, Poco::UInt16 port, bool listen = false);
4344
/// Creates UDP client and connects it to specified address/port.
4445
/// If listen is true, a thread is launched where client can receive
45-
/// responses rom the server.
46+
/// responses from the server.
4647

4748
~UDPClient() override;
4849
/// Destroys UDPClient.

Net/include/Poco/Net/UDPServer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class UDPServerImpl: public Poco::Runnable
3737
/// MultipleSocketPoller for more information.
3838
{
3939
public:
40+
41+
using ServerParams = UDPServerParams;
42+
4043
UDPServerImpl(typename UDPHandlerImpl<S>::List& handlers, const Poco::Net::SocketAddress& sa):
4144
_poller(handlers, sa),
4245
_thread("UDPServer"),
@@ -47,7 +50,7 @@ class UDPServerImpl: public Poco::Runnable
4750
_thread.start(*this);
4851
}
4952

50-
UDPServerImpl(typename UDPHandlerImpl<S>::List& handlers, const UDPServerParams& params):
53+
UDPServerImpl(typename UDPHandlerImpl<S>::List& handlers, const ServerParams& params):
5154
_poller(handlers, params),
5255
_thread("UDPServer"),
5356
_stop(false)

Net/include/Poco/Net/UDPServerParams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Net_API UDPServerParams
3333
public:
3434
UDPServerParams(const Poco::Net::SocketAddress& sa,
3535
int nSockets = 10,
36-
Poco::Timespan timeout = 250000,
36+
const Poco::Timespan& timeout = 250000,
3737
std::size_t handlerBufListSize = 1000,
3838
bool notifySender = false,
3939
int backlogThreshold = 10);

Net/src/UDPServerParams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Net {
2121

2222
UDPServerParams::UDPServerParams(const Poco::Net::SocketAddress& sa,
2323
int nSockets,
24-
Poco::Timespan timeout,
24+
const Timespan& timeout,
2525
std::size_t handlerBufListSize,
2626
bool notifySender,
2727
int backlogThreshold): _sa(sa),

Net/testsuite/src/UDPServerTest.cpp

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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()
173180
void 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
}

Net/testsuite/src/UDPServerTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class UDPServerTest: public CppUnit::TestCase
2727
void testUDPMultiSocket();
2828
void testUDPSingleSocketMultipleHandlers();
2929
void testUDPMultiSocketMultipleHandlers();
30+
void testUDPMultiSocketMultipleHandlersLessSockets();
31+
void testUDPMultiSocketMultipleHandlersMoreSockets();
32+
void testUDPMultiSocketMultipleHandlersMultipleClients();
3033

3134
void setUp();
3235
void tearDown();

0 commit comments

Comments
 (0)