@@ -14,9 +14,12 @@ TEST_CASE("create destroy", "[active_poller]")
14
14
}
15
15
16
16
static_assert (!std::is_copy_constructible<zmq::active_poller_t >::value,
17
- " active_active_poller_t should not be copy-constructible" );
17
+ " active_poller_t should not be copy-constructible" );
18
18
static_assert (!std::is_copy_assignable<zmq::active_poller_t >::value,
19
- " active_active_poller_t should not be copy-assignable" );
19
+ " active_poller_t should not be copy-assignable" );
20
+
21
+ static const zmq::active_poller_t ::handler_type no_op_handler =
22
+ [](zmq::event_flags) {};
20
23
21
24
TEST_CASE (" move construct empty" , " [active_poller]" )
22
25
{
@@ -47,9 +50,7 @@ TEST_CASE("move construct non empty", "[active_poller]")
47
50
zmq::socket_t socket{context, zmq::socket_type::router};
48
51
49
52
zmq::active_poller_t a;
50
- a.add (socket, zmq::event_flags::pollin, [](zmq::event_flags)
51
- {
52
- });
53
+ a.add (socket, zmq::event_flags::pollin, [](zmq::event_flags) {});
53
54
CHECK_FALSE (a.empty ());
54
55
CHECK (1u == a.size ());
55
56
zmq::active_poller_t b = std::move (a);
@@ -65,9 +66,7 @@ TEST_CASE("move assign non empty", "[active_poller]")
65
66
zmq::socket_t socket{context, zmq::socket_type::router};
66
67
67
68
zmq::active_poller_t a;
68
- a.add (socket, zmq::event_flags::pollin, [](zmq::event_flags)
69
- {
70
- });
69
+ a.add (socket, zmq::event_flags::pollin, no_op_handler);
71
70
CHECK_FALSE (a.empty ());
72
71
CHECK (1u == a.size ());
73
72
zmq::active_poller_t b;
@@ -79,12 +78,22 @@ TEST_CASE("move assign non empty", "[active_poller]")
79
78
}
80
79
81
80
TEST_CASE (" add handler" , " [active_poller]" )
81
+ {
82
+ zmq::context_t context;
83
+ zmq::socket_t socket{context, zmq::socket_type::router};
84
+ zmq::active_poller_t active_poller;
85
+ CHECK_NOTHROW (
86
+ active_poller.add (socket, zmq::event_flags::pollin, no_op_handler));
87
+ }
88
+
89
+ TEST_CASE (" add null handler fails" , " [active_poller]" )
82
90
{
83
91
zmq::context_t context;
84
92
zmq::socket_t socket{context, zmq::socket_type::router};
85
93
zmq::active_poller_t active_poller;
86
94
zmq::active_poller_t ::handler_type handler;
87
- CHECK_NOTHROW (active_poller.add (socket, zmq::event_flags::pollin, handler));
95
+ CHECK_THROWS_AS (active_poller.add (socket, zmq::event_flags::pollin, handler),
96
+ const std::invalid_argument &);
88
97
}
89
98
90
99
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0)
@@ -94,52 +103,54 @@ TEST_CASE("add handler invalid events type", "[active_poller]")
94
103
zmq::context_t context;
95
104
zmq::socket_t socket{context, zmq::socket_type::router};
96
105
zmq::active_poller_t active_poller;
97
- zmq::active_poller_t ::handler_type handler;
98
106
short invalid_events_type = 2 << 10 ;
99
107
CHECK_THROWS_AS (
100
- active_poller.add (socket, static_cast <zmq::event_flags>(invalid_events_type),
101
- handler), const zmq::error_t &);
108
+ active_poller.add (socket, static_cast <zmq::event_flags>(invalid_events_type),
109
+ no_op_handler),
110
+ const zmq::error_t &);
102
111
CHECK (active_poller.empty ());
103
112
CHECK (0u == active_poller.size ());
104
113
}
105
114
#endif
106
115
107
116
TEST_CASE (" add handler twice throws" , " [active_poller]" )
108
117
{
109
- zmq::context_t context;
110
- zmq::socket_t socket{context, zmq::socket_type::router};
118
+ common_server_client_setup s;
119
+
120
+ CHECK (s.client .send (zmq::message_t {}, zmq::send_flags::none));
121
+
111
122
zmq::active_poller_t active_poller;
112
- zmq::active_poller_t ::handler_type handler;
113
- active_poller.add (socket, zmq::event_flags::pollin, handler);
114
- // / \todo the actual error code should be checked
115
- CHECK_THROWS_AS (active_poller.add (socket, zmq::event_flags::pollin, handler),
116
- const zmq::error_t &);
123
+ bool message_received = false ;
124
+ active_poller.add (
125
+ s.server , zmq::event_flags::pollin,
126
+ [&message_received](zmq::event_flags) { message_received = true ; });
127
+ CHECK_THROWS_ZMQ_ERROR (
128
+ EINVAL, active_poller.add (s.server , zmq::event_flags::pollin, no_op_handler));
129
+ CHECK (1 == active_poller.wait (std::chrono::milliseconds{-1 }));
130
+ CHECK (message_received); // handler unmodified
117
131
}
118
132
119
133
TEST_CASE (" wait with no handlers throws" , " [active_poller]" )
120
134
{
121
135
zmq::active_poller_t active_poller;
122
- // / \todo the actual error code should be checked
123
- CHECK_THROWS_AS (active_poller.wait (std::chrono::milliseconds{10 }),
124
- const zmq::error_t &);
136
+ CHECK_THROWS_ZMQ_ERROR (EFAULT,
137
+ active_poller.wait (std::chrono::milliseconds{10 }));
125
138
}
126
139
127
140
TEST_CASE (" remove unregistered throws" , " [active_poller]" )
128
141
{
129
142
zmq::context_t context;
130
143
zmq::socket_t socket{context, zmq::socket_type::router};
131
144
zmq::active_poller_t active_poller;
132
- // / \todo the actual error code should be checked
133
- CHECK_THROWS_AS (active_poller.remove (socket), const zmq::error_t &);
145
+ CHECK_THROWS_ZMQ_ERROR (EINVAL, active_poller.remove (socket));
134
146
}
135
147
136
148
TEST_CASE (" remove registered empty" , " [active_poller]" )
137
149
{
138
150
zmq::context_t context;
139
151
zmq::socket_t socket{context, zmq::socket_type::router};
140
152
zmq::active_poller_t active_poller;
141
- active_poller.add (socket, zmq::event_flags::pollin,
142
- zmq::active_poller_t ::handler_type{});
153
+ active_poller.add (socket, zmq::event_flags::pollin, no_op_handler);
143
154
CHECK_NOTHROW (active_poller.remove (socket));
144
155
}
145
156
@@ -148,18 +159,15 @@ TEST_CASE("remove registered non empty", "[active_poller]")
148
159
zmq::context_t context;
149
160
zmq::socket_t socket{context, zmq::socket_type::router};
150
161
zmq::active_poller_t active_poller;
151
- active_poller.add (socket, zmq::event_flags::pollin, [](zmq::event_flags)
152
- {
153
- });
162
+ active_poller.add (socket, zmq::event_flags::pollin, no_op_handler);
154
163
CHECK_NOTHROW (active_poller.remove (socket));
155
164
}
156
165
157
166
namespace
158
167
{
159
168
struct server_client_setup : common_server_client_setup
160
169
{
161
- zmq::active_poller_t ::handler_type handler = [&](zmq::event_flags e)
162
- {
170
+ zmq::active_poller_t ::handler_type handler = [&](zmq::event_flags e) {
163
171
events = e;
164
172
};
165
173
@@ -178,12 +186,11 @@ TEST_CASE("poll basic", "[active_poller]")
178
186
179
187
zmq::active_poller_t active_poller;
180
188
bool message_received = false ;
181
- zmq::active_poller_t ::handler_type handler = [&message_received
182
- ](zmq::event_flags events)
183
- {
184
- CHECK (zmq::event_flags::none != (events & zmq::event_flags::pollin));
185
- message_received = true ;
186
- };
189
+ zmq::active_poller_t ::handler_type handler =
190
+ [&message_received](zmq::event_flags events) {
191
+ CHECK (zmq::event_flags::none != (events & zmq::event_flags::pollin));
192
+ message_received = true ;
193
+ };
187
194
CHECK_NOTHROW (active_poller.add (s.server , zmq::event_flags::pollin, handler));
188
195
CHECK (1 == active_poller.wait (std::chrono::milliseconds{-1 }));
189
196
CHECK (message_received);
@@ -200,8 +207,7 @@ TEST_CASE("client server", "[active_poller]")
200
207
// Setup active_poller
201
208
zmq::active_poller_t active_poller;
202
209
zmq::event_flags events;
203
- zmq::active_poller_t ::handler_type handler = [&](zmq::event_flags e)
204
- {
210
+ zmq::active_poller_t ::handler_type handler = [&](zmq::event_flags e) {
205
211
if (zmq::event_flags::none != (e & zmq::event_flags::pollin)) {
206
212
zmq::message_t zmq_msg;
207
213
CHECK_NOTHROW (s.server .recv (zmq_msg)); // get message
@@ -224,9 +230,8 @@ TEST_CASE("client server", "[active_poller]")
224
230
225
231
// Re-add server socket with pollout flag
226
232
CHECK_NOTHROW (active_poller.remove (s.server ));
227
- CHECK_NOTHROW (
228
- active_poller.add (s.server , zmq::event_flags::pollin | zmq::event_flags::
229
- pollout, handler));
233
+ CHECK_NOTHROW (active_poller.add (
234
+ s.server , zmq::event_flags::pollin | zmq::event_flags::pollout, handler));
230
235
CHECK (1 == active_poller.wait (std::chrono::milliseconds{-1 }));
231
236
CHECK (events == zmq::event_flags::pollout);
232
237
}
@@ -237,10 +242,8 @@ TEST_CASE("add invalid socket throws", "[active_poller]")
237
242
zmq::active_poller_t active_poller;
238
243
zmq::socket_t a{context, zmq::socket_type::router};
239
244
zmq::socket_t b{std::move (a)};
240
- CHECK_THROWS_AS (
241
- active_poller.add (a, zmq::event_flags::pollin, zmq::active_poller_t ::
242
- handler_type{}),
243
- const zmq::error_t &);
245
+ CHECK_THROWS_AS (active_poller.add (a, zmq::event_flags::pollin, no_op_handler),
246
+ const zmq::error_t &);
244
247
}
245
248
246
249
TEST_CASE (" remove invalid socket throws" , " [active_poller]" )
@@ -249,12 +252,11 @@ TEST_CASE("remove invalid socket throws", "[active_poller]")
249
252
zmq::socket_t socket{context, zmq::socket_type::router};
250
253
zmq::active_poller_t active_poller;
251
254
CHECK_NOTHROW (
252
- active_poller.add (socket, zmq::event_flags::pollin, zmq::active_poller_t ::
253
- handler_type{}));
255
+ active_poller.add (socket, zmq::event_flags::pollin, no_op_handler));
254
256
CHECK (1u == active_poller.size ());
255
257
std::vector<zmq::socket_t > sockets;
256
258
sockets.emplace_back (std::move (socket));
257
- CHECK_THROWS_AS (active_poller.remove (socket), const zmq::error_t &);
259
+ CHECK_THROWS_AS (active_poller.remove (socket), const zmq::error_t &);
258
260
CHECK (1u == active_poller.size ());
259
261
}
260
262
@@ -263,8 +265,8 @@ TEST_CASE("wait on added empty handler", "[active_poller]")
263
265
server_client_setup s;
264
266
CHECK_NOTHROW (s.client .send (zmq::message_t {hi_str}, zmq::send_flags::none));
265
267
zmq::active_poller_t active_poller;
266
- zmq:: active_poller_t ::handler_type handler;
267
- CHECK_NOTHROW ( active_poller.add (s.server , zmq::event_flags::pollin, handler ));
268
+ CHECK_NOTHROW (
269
+ active_poller.add (s.server , zmq::event_flags::pollin, no_op_handler ));
268
270
CHECK_NOTHROW (active_poller.wait (std::chrono::milliseconds{-1 }));
269
271
}
270
272
@@ -274,7 +276,7 @@ TEST_CASE("modify empty throws", "[active_poller]")
274
276
zmq::socket_t socket{context, zmq::socket_type::push};
275
277
zmq::active_poller_t active_poller;
276
278
CHECK_THROWS_AS (active_poller.modify (socket, zmq::event_flags::pollin),
277
- const zmq::error_t &);
279
+ const zmq::error_t &);
278
280
}
279
281
280
282
TEST_CASE (" modify invalid socket throws" , " [active_poller]" )
@@ -284,7 +286,7 @@ TEST_CASE("modify invalid socket throws", "[active_poller]")
284
286
zmq::socket_t b{std::move (a)};
285
287
zmq::active_poller_t active_poller;
286
288
CHECK_THROWS_AS (active_poller.modify (a, zmq::event_flags::pollin),
287
- const zmq::error_t &);
289
+ const zmq::error_t &);
288
290
}
289
291
290
292
TEST_CASE (" modify not added throws" , " [active_poller]" )
@@ -293,24 +295,19 @@ TEST_CASE("modify not added throws", "[active_poller]")
293
295
zmq::socket_t a{context, zmq::socket_type::push};
294
296
zmq::socket_t b{context, zmq::socket_type::push};
295
297
zmq::active_poller_t active_poller;
296
- CHECK_NOTHROW (
297
- active_poller.add (a, zmq::event_flags::pollin, zmq::active_poller_t ::
298
- handler_type{}));
298
+ CHECK_NOTHROW (active_poller.add (a, zmq::event_flags::pollin, no_op_handler));
299
299
CHECK_THROWS_AS (active_poller.modify (b, zmq::event_flags::pollin),
300
- const zmq::error_t &);
300
+ const zmq::error_t &);
301
301
}
302
302
303
303
TEST_CASE (" modify simple" , " [active_poller]" )
304
304
{
305
305
zmq::context_t context;
306
306
zmq::socket_t a{context, zmq::socket_type::push};
307
307
zmq::active_poller_t active_poller;
308
+ CHECK_NOTHROW (active_poller.add (a, zmq::event_flags::pollin, no_op_handler));
308
309
CHECK_NOTHROW (
309
- active_poller.add (a, zmq::event_flags::pollin, zmq::active_poller_t ::
310
- handler_type{}));
311
- CHECK_NOTHROW (
312
- active_poller.modify (a, zmq::event_flags::pollin | zmq::event_flags::pollout
313
- ));
310
+ active_poller.modify (a, zmq::event_flags::pollin | zmq::event_flags::pollout));
314
311
}
315
312
316
313
TEST_CASE (" poll client server" , " [active_poller]" )
@@ -330,9 +327,8 @@ TEST_CASE("poll client server", "[active_poller]")
330
327
CHECK (s.events == zmq::event_flags::pollin);
331
328
332
329
// Modify server socket with pollout flag
333
- CHECK_NOTHROW (
334
- active_poller.modify (s.server , zmq::event_flags::pollin | zmq::event_flags::
335
- pollout));
330
+ CHECK_NOTHROW (active_poller.modify (s.server , zmq::event_flags::pollin
331
+ | zmq::event_flags::pollout));
336
332
CHECK (1 == active_poller.wait (std::chrono::milliseconds{500 }));
337
333
CHECK (s.events == (zmq::event_flags::pollin | zmq::event_flags::pollout));
338
334
}
@@ -346,9 +342,8 @@ TEST_CASE("wait one return", "[active_poller]")
346
342
347
343
// Setup active_poller
348
344
zmq::active_poller_t active_poller;
349
- CHECK_NOTHROW (
350
- active_poller.add (s.server , zmq::event_flags::pollin, [&count](zmq::
351
- event_flags) { ++count; }));
345
+ CHECK_NOTHROW (active_poller.add (s.server , zmq::event_flags::pollin,
346
+ [&count](zmq::event_flags) { ++count; }));
352
347
353
348
// client sends message
354
349
CHECK_NOTHROW (s.client .send (zmq::message_t {hi_str}, zmq::send_flags::none));
@@ -363,12 +358,11 @@ TEST_CASE("wait on move constructed active_poller", "[active_poller]")
363
358
server_client_setup s;
364
359
CHECK_NOTHROW (s.client .send (zmq::message_t {hi_str}, zmq::send_flags::none));
365
360
zmq::active_poller_t a;
366
- zmq::active_poller_t ::handler_type handler;
367
- CHECK_NOTHROW (a.add (s.server , zmq::event_flags::pollin, handler));
361
+ CHECK_NOTHROW (a.add (s.server , zmq::event_flags::pollin, no_op_handler));
368
362
zmq::active_poller_t b{std::move (a)};
369
363
CHECK (1u == b.size ());
370
- // / \todo the actual error code should be checked
371
- CHECK_THROWS_AS ( a.wait (std::chrono::milliseconds{10 }), const zmq:: error_t & );
364
+ CHECK ( 0u == a. size ());
365
+ CHECK_THROWS_ZMQ_ERROR (EFAULT, a.wait (std::chrono::milliseconds{10 }));
372
366
CHECK (b.wait (std::chrono::milliseconds{-1 }));
373
367
}
374
368
@@ -377,13 +371,12 @@ TEST_CASE("wait on move assigned active_poller", "[active_poller]")
377
371
server_client_setup s;
378
372
CHECK_NOTHROW (s.client .send (zmq::message_t {hi_str}, zmq::send_flags::none));
379
373
zmq::active_poller_t a;
380
- zmq::active_poller_t ::handler_type handler;
381
- CHECK_NOTHROW (a.add (s.server , zmq::event_flags::pollin, handler));
374
+ CHECK_NOTHROW (a.add (s.server , zmq::event_flags::pollin, no_op_handler));
382
375
zmq::active_poller_t b;
383
376
b = {std::move (a)};
384
377
CHECK (1u == b.size ());
385
- // / \todo the actual error code should be checked
386
- CHECK_THROWS_AS ( a.wait (std::chrono::milliseconds{10 }), const zmq:: error_t & );
378
+ CHECK ( 0u == a. size ());
379
+ CHECK_THROWS_ZMQ_ERROR (EFAULT, a.wait (std::chrono::milliseconds{10 }));
387
380
CHECK (b.wait (std::chrono::milliseconds{-1 }));
388
381
}
389
382
@@ -394,9 +387,8 @@ TEST_CASE("received on move constructed active_poller", "[active_poller]")
394
387
int count = 0 ;
395
388
// Setup active_poller a
396
389
zmq::active_poller_t a;
397
- CHECK_NOTHROW (
398
- a.add (s.server , zmq::event_flags::pollin, [&count](zmq::event_flags) { ++
399
- count; }));
390
+ CHECK_NOTHROW (a.add (s.server , zmq::event_flags::pollin,
391
+ [&count](zmq::event_flags) { ++count; }));
400
392
// client sends message
401
393
CHECK_NOTHROW (s.client .send (zmq::message_t {hi_str}, zmq::send_flags::none));
402
394
// wait for message and verify it is received
@@ -425,13 +417,13 @@ TEST_CASE("remove from handler", "[active_poller]")
425
417
zmq::active_poller_t active_poller;
426
418
int count = 0 ;
427
419
for (size_t i = 0 ; i < ITER_NO; ++i) {
428
- CHECK_NOTHROW (
429
- active_poller. add ( setup_list[i].server , zmq::event_flags::pollin, [&, i](
430
- zmq::event_flags events) {
431
- CHECK (events == zmq::event_flags::pollin);
432
- active_poller.remove (setup_list[ITER_NO - i - 1 ].server );
433
- CHECK ((ITER_NO - i - 1 ) == active_poller.size ());
434
- }));
420
+ CHECK_NOTHROW (active_poller. add (
421
+ setup_list[i].server , zmq::event_flags::pollin,
422
+ [&, i]( zmq::event_flags events) {
423
+ CHECK (events == zmq::event_flags::pollin);
424
+ active_poller.remove (setup_list[ITER_NO - i - 1 ].server );
425
+ CHECK ((ITER_NO - i - 1 ) == active_poller.size ());
426
+ }));
435
427
++count;
436
428
}
437
429
CHECK (ITER_NO == active_poller.size ());
0 commit comments