forked from zeromq/cppzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_poller.cpp
446 lines (383 loc) · 14.5 KB
/
active_poller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <zmq_addon.hpp>
#include "testutil.hpp"
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL) && defined(ZMQ_BUILD_DRAFT_API)
#include <array>
#include <memory>
TEST_CASE("create destroy", "[active_poller]")
{
zmq::active_poller_t active_poller;
CHECK(active_poller.empty());
}
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value,
"active_poller_t should not be copy-constructible");
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value,
"active_poller_t should not be copy-assignable");
static const zmq::active_poller_t::handler_type no_op_handler =
[](zmq::event_flags) {};
TEST_CASE("move construct empty", "[active_poller]")
{
zmq::active_poller_t a;
CHECK(a.empty());
zmq::active_poller_t b = std::move(a);
CHECK(b.empty());
CHECK(0u == a.size());
CHECK(0u == b.size());
}
TEST_CASE("move assign empty", "[active_poller]")
{
zmq::active_poller_t a;
CHECK(a.empty());
zmq::active_poller_t b;
CHECK(b.empty());
b = std::move(a);
CHECK(0u == a.size());
CHECK(0u == b.size());
CHECK(a.empty());
CHECK(b.empty());
}
TEST_CASE("move construct non empty", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t a;
a.add(socket, zmq::event_flags::pollin, [](zmq::event_flags) {});
CHECK_FALSE(a.empty());
CHECK(1u == a.size());
zmq::active_poller_t b = std::move(a);
CHECK(a.empty());
CHECK(0u == a.size());
CHECK_FALSE(b.empty());
CHECK(1u == b.size());
}
TEST_CASE("move assign non empty", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t a;
a.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_FALSE(a.empty());
CHECK(1u == a.size());
zmq::active_poller_t b;
b = std::move(a);
CHECK(a.empty());
CHECK(0u == a.size());
CHECK_FALSE(b.empty());
CHECK(1u == b.size());
}
TEST_CASE("add handler", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
}
TEST_CASE("add null handler fails", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_type handler;
CHECK_THROWS_AS(active_poller.add(socket, zmq::event_flags::pollin, handler),
std::invalid_argument);
}
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0)
// this behaviour was added by https://github.com/zeromq/libzmq/pull/3100
TEST_CASE("add handler invalid events type", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
short invalid_events_type = 2 << 10;
CHECK_THROWS_AS(
active_poller.add(socket, static_cast<zmq::event_flags>(invalid_events_type),
no_op_handler),
zmq::error_t);
CHECK(active_poller.empty());
CHECK(0u == active_poller.size());
}
#endif
TEST_CASE("add handler twice throws", "[active_poller]")
{
common_server_client_setup s;
CHECK(s.client.send(zmq::message_t{}, zmq::send_flags::none));
zmq::active_poller_t active_poller;
bool message_received = false;
active_poller.add(
s.server, zmq::event_flags::pollin,
[&message_received](zmq::event_flags) { message_received = true; });
CHECK_THROWS_ZMQ_ERROR(
EINVAL, active_poller.add(s.server, zmq::event_flags::pollin, no_op_handler));
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
CHECK(message_received); // handler unmodified
}
TEST_CASE("wait with no handlers throws", "[active_poller]")
{
zmq::active_poller_t active_poller;
CHECK_THROWS_ZMQ_ERROR(EFAULT,
active_poller.wait(std::chrono::milliseconds{10}));
}
TEST_CASE("remove unregistered throws", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
CHECK_THROWS_ZMQ_ERROR(EINVAL, active_poller.remove(socket));
}
TEST_CASE("remove registered empty", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_NOTHROW(active_poller.remove(socket));
}
TEST_CASE("remove registered non empty", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_NOTHROW(active_poller.remove(socket));
}
namespace
{
struct server_client_setup : common_server_client_setup
{
zmq::active_poller_t::handler_type handler = [&](zmq::event_flags e) {
events = e;
};
zmq::event_flags events = zmq::event_flags::none;
};
const std::string hi_str = "Hi";
}
TEST_CASE("poll basic", "[active_poller]")
{
server_client_setup s;
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
zmq::active_poller_t active_poller;
bool message_received = false;
zmq::active_poller_t::handler_type handler =
[&message_received](zmq::event_flags events) {
CHECK(zmq::event_flags::none != (events & zmq::event_flags::pollin));
message_received = true;
};
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, handler));
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
CHECK(message_received);
}
/// \todo this contains multiple test cases that should be split up
TEST_CASE("client server", "[active_poller]")
{
const std::string send_msg = hi_str;
// Setup server and client
server_client_setup s;
// Setup active_poller
zmq::active_poller_t active_poller;
zmq::event_flags events;
zmq::active_poller_t::handler_type handler = [&](zmq::event_flags e) {
if (zmq::event_flags::none != (e & zmq::event_flags::pollin)) {
zmq::message_t zmq_msg;
CHECK_NOTHROW(s.server.recv(zmq_msg)); // get message
std::string recv_msg(zmq_msg.data<char>(), zmq_msg.size());
CHECK(send_msg == recv_msg);
} else if (zmq::event_flags::none != (e & ~zmq::event_flags::pollout)) {
INFO("Unexpected event type " << static_cast<short>(events));
REQUIRE(false);
}
events = e;
};
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, handler));
// client sends message
CHECK_NOTHROW(s.client.send(zmq::message_t{send_msg}, zmq::send_flags::none));
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
CHECK(events == zmq::event_flags::pollin);
// Re-add server socket with pollout flag
CHECK_NOTHROW(active_poller.remove(s.server));
CHECK_NOTHROW(active_poller.add(
s.server, zmq::event_flags::pollin | zmq::event_flags::pollout, handler));
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
CHECK(events == zmq::event_flags::pollout);
}
TEST_CASE("add invalid socket throws", "[active_poller]")
{
zmq::context_t context;
zmq::active_poller_t active_poller;
zmq::socket_t a{context, zmq::socket_type::router};
zmq::socket_t b{std::move(a)};
CHECK_THROWS_AS(active_poller.add(a, zmq::event_flags::pollin, no_op_handler),
zmq::error_t);
}
TEST_CASE("remove invalid socket throws", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
CHECK(1u == active_poller.size());
std::vector<zmq::socket_t> sockets;
sockets.emplace_back(std::move(socket));
CHECK_THROWS_AS(active_poller.remove(socket), zmq::error_t);
CHECK(1u == active_poller.size());
}
TEST_CASE("wait on added empty handler", "[active_poller]")
{
server_client_setup s;
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(s.server, zmq::event_flags::pollin, no_op_handler));
CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{-1}));
}
TEST_CASE("modify empty throws", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::push};
zmq::active_poller_t active_poller;
CHECK_THROWS_AS(active_poller.modify(socket, zmq::event_flags::pollin),
zmq::error_t);
}
TEST_CASE("modify invalid socket throws", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b{std::move(a)};
zmq::active_poller_t active_poller;
CHECK_THROWS_AS(active_poller.modify(a, zmq::event_flags::pollin),
zmq::error_t);
}
TEST_CASE("modify not added throws", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b{context, zmq::socket_type::push};
zmq::active_poller_t active_poller;
CHECK_NOTHROW(active_poller.add(a, zmq::event_flags::pollin, no_op_handler));
CHECK_THROWS_AS(active_poller.modify(b, zmq::event_flags::pollin),
zmq::error_t);
}
TEST_CASE("modify simple", "[active_poller]")
{
zmq::context_t context;
zmq::socket_t a{context, zmq::socket_type::push};
zmq::active_poller_t active_poller;
CHECK_NOTHROW(active_poller.add(a, zmq::event_flags::pollin, no_op_handler));
CHECK_NOTHROW(
active_poller.modify(a, zmq::event_flags::pollin | zmq::event_flags::pollout));
}
TEST_CASE("poll client server", "[active_poller]")
{
// Setup server and client
server_client_setup s;
// Setup active_poller
zmq::active_poller_t active_poller;
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, s.handler));
// client sends message
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
// wait for message and verify events
CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{500}));
CHECK(s.events == zmq::event_flags::pollin);
// Modify server socket with pollout flag
CHECK_NOTHROW(active_poller.modify(s.server, zmq::event_flags::pollin
| zmq::event_flags::pollout));
CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
CHECK(s.events == (zmq::event_flags::pollin | zmq::event_flags::pollout));
}
TEST_CASE("wait one return", "[active_poller]")
{
// Setup server and client
server_client_setup s;
int count = 0;
// Setup active_poller
zmq::active_poller_t active_poller;
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin,
[&count](zmq::event_flags) { ++count; }));
// client sends message
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
// wait for message and verify events
CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
CHECK(1u == count);
}
TEST_CASE("wait on move constructed active_poller", "[active_poller]")
{
server_client_setup s;
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
zmq::active_poller_t a;
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, no_op_handler));
zmq::active_poller_t b{std::move(a)};
CHECK(1u == b.size());
CHECK(0u == a.size());
CHECK_THROWS_ZMQ_ERROR(EFAULT, a.wait(std::chrono::milliseconds{10}));
CHECK(b.wait(std::chrono::milliseconds{-1}));
}
TEST_CASE("wait on move assigned active_poller", "[active_poller]")
{
server_client_setup s;
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
zmq::active_poller_t a;
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, no_op_handler));
zmq::active_poller_t b;
b = {std::move(a)};
CHECK(1u == b.size());
CHECK(0u == a.size());
CHECK_THROWS_ZMQ_ERROR(EFAULT, a.wait(std::chrono::milliseconds{10}));
CHECK(b.wait(std::chrono::milliseconds{-1}));
}
TEST_CASE("received on move constructed active_poller", "[active_poller]")
{
// Setup server and client
server_client_setup s;
int count = 0;
// Setup active_poller a
zmq::active_poller_t a;
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin,
[&count](zmq::event_flags) { ++count; }));
// client sends message
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
// wait for message and verify it is received
CHECK(1 == a.wait(std::chrono::milliseconds{500}));
CHECK(1u == count);
// Move construct active_poller b
zmq::active_poller_t b{std::move(a)};
// client sends message again
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
// wait for message and verify it is received
CHECK(1 == b.wait(std::chrono::milliseconds{500}));
CHECK(2u == count);
}
TEST_CASE("remove from handler", "[active_poller]")
{
constexpr size_t ITER_NO = 10;
// Setup servers and clients
std::vector<server_client_setup> setup_list;
for (size_t i = 0; i < ITER_NO; ++i)
setup_list.emplace_back(server_client_setup{});
// Setup active_poller
zmq::active_poller_t active_poller;
int count = 0;
for (size_t i = 0; i < ITER_NO; ++i) {
CHECK_NOTHROW(active_poller.add(
setup_list[i].server, zmq::event_flags::pollin,
[&, i](zmq::event_flags events) {
CHECK(events == zmq::event_flags::pollin);
active_poller.remove(setup_list[ITER_NO - i - 1].server);
CHECK((ITER_NO - i - 1) == active_poller.size());
}));
++count;
}
CHECK(ITER_NO == active_poller.size());
// Clients send messages
for (auto &s : setup_list) {
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
}
// Wait for all servers to receive a message
for (auto &s : setup_list) {
zmq::pollitem_t items[] = {{s.server, 0, ZMQ_POLLIN, 0}};
zmq::poll(&items[0], 1);
}
// Fire all handlers in one wait
CHECK(ITER_NO == active_poller.wait(std::chrono::milliseconds{-1}));
CHECK(ITER_NO == count);
}
#endif