5 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL) && defined(ZMQ_BUILD_DRAFT_API)
15 TEST_CASE(
"create destroy",
"[active_poller]")
17 zmq::active_poller_t active_poller;
18 CHECK(active_poller.empty());
22 "active_poller_t should not be copy-constructible");
24 "active_poller_t should not be copy-assignable");
26 static const zmq::active_poller_t::handler_type no_op_handler =
27 [](zmq::event_flags) {};
29 TEST_CASE(
"move construct empty",
"[active_poller]")
31 zmq::active_poller_t
a;
33 zmq::active_poller_t
b = std::move(
a);
39 TEST_CASE(
"move assign empty",
"[active_poller]")
41 zmq::active_poller_t
a;
43 zmq::active_poller_t
b;
52 TEST_CASE(
"move construct non empty",
"[active_poller]")
57 zmq::active_poller_t
a;
58 a.add(socket, zmq::event_flags::pollin, [](zmq::event_flags) {});
59 CHECK_FALSE(
a.empty());
61 zmq::active_poller_t
b = std::move(
a);
64 CHECK_FALSE(
b.empty());
68 TEST_CASE(
"move assign non empty",
"[active_poller]")
73 zmq::active_poller_t
a;
74 a.add(socket, zmq::event_flags::pollin, no_op_handler);
75 CHECK_FALSE(
a.empty());
77 zmq::active_poller_t
b;
81 CHECK_FALSE(
b.empty());
85 TEST_CASE(
"add handler",
"[active_poller]")
89 zmq::active_poller_t active_poller;
91 active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
94 TEST_CASE(
"add fd handler",
"[active_poller]")
97 zmq::active_poller_t active_poller;
99 active_poller.add(fd, zmq::event_flags::pollin, no_op_handler));
102 TEST_CASE(
"remove fd handler",
"[active_poller]")
105 zmq::active_poller_t active_poller;
107 active_poller.add(fd, zmq::event_flags::pollin, no_op_handler));
109 active_poller.remove(fd));
116 TEST_CASE(
"mixed socket and fd handlers",
"[active_poller]")
122 constexpr
char inprocSocketAddress[] =
"inproc://mixed-handlers";
125 socket_rcv.bind(inprocSocketAddress);
126 socket_snd.connect(inprocSocketAddress);
128 unsigned eventsFd = 0;
129 unsigned eventsSocket = 0;
131 constexpr
char messageText[] =
"message";
132 constexpr
size_t messageSize =
sizeof(messageText);
134 zmq::active_poller_t active_poller;
136 active_poller.add(pipefd[0], zmq::event_flags::pollin, [&](zmq::event_flags
flags) {
137 if (flags == zmq::event_flags::pollin)
140 CHECK(messageSize == ::read(pipefd[0], buffer, messageSize));
141 CHECK(0 == std::strcmp(buffer, messageText));
146 active_poller.add(socket_rcv, zmq::event_flags::pollin, [&](zmq::event_flags
flags) {
147 if (flags == zmq::event_flags::pollin)
150 CHECK(socket_rcv.recv(msg, zmq::recv_flags::dontwait).has_value());
151 CHECK(messageSize == msg.size());
152 CHECK(0 == std::strcmp(messageText, msg.data<const char>()));
159 socket_snd.send(msg, zmq::send_flags::dontwait);
160 CHECK(1 == active_poller.wait(std::chrono::milliseconds{100}));
161 CHECK(0 == eventsFd);
162 CHECK(1 == eventsSocket);
165 ::write(pipefd[1], messageText, messageSize);
166 CHECK(1 == active_poller.wait(std::chrono::milliseconds{100}));
167 CHECK(1 == eventsFd);
168 CHECK(1 == eventsSocket);
173 TEST_CASE(
"add null handler fails",
"[active_poller]")
177 zmq::active_poller_t active_poller;
178 zmq::active_poller_t::handler_type
handler;
179 CHECK_THROWS_AS(active_poller.add(socket, zmq::event_flags::pollin,
handler),
180 std::invalid_argument);
183 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0)
185 TEST_CASE(
"add handler invalid events type",
"[active_poller]")
189 zmq::active_poller_t active_poller;
190 short invalid_events_type = 2 << 10;
192 active_poller.add(socket,
static_cast<zmq::event_flags
>(invalid_events_type),
195 CHECK(active_poller.empty());
196 CHECK(0u == active_poller.size());
200 TEST_CASE(
"add handler twice throws",
"[active_poller]")
202 common_server_client_setup
s;
206 zmq::active_poller_t active_poller;
207 bool message_received =
false;
209 s.server, zmq::event_flags::pollin,
210 [&message_received](zmq::event_flags) { message_received = true; });
212 EINVAL, active_poller.add(
s.server, zmq::event_flags::pollin, no_op_handler));
213 CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
214 CHECK(message_received);
217 TEST_CASE(
"wait with no handlers throws",
"[active_poller]")
219 zmq::active_poller_t active_poller;
221 active_poller.wait(std::chrono::milliseconds{10}));
224 TEST_CASE(
"remove unregistered throws",
"[active_poller]")
228 zmq::active_poller_t active_poller;
232 TEST_CASE(
"remove registered empty",
"[active_poller]")
236 zmq::active_poller_t active_poller;
237 active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
238 CHECK_NOTHROW(active_poller.remove(socket));
241 TEST_CASE(
"remove registered non empty",
"[active_poller]")
245 zmq::active_poller_t active_poller;
246 active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
247 CHECK_NOTHROW(active_poller.remove(socket));
252 struct server_client_setup : common_server_client_setup
254 zmq::active_poller_t::handler_type
handler = [&](zmq::event_flags e) {
258 zmq::event_flags events = zmq::event_flags::none;
265 TEST_CASE(
"poll basic",
"[active_poller]")
267 server_client_setup
s;
269 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
271 zmq::active_poller_t active_poller;
272 bool message_received =
false;
273 zmq::active_poller_t::handler_type
handler =
274 [&message_received](zmq::event_flags events) {
275 CHECK(zmq::event_flags::none != (events & zmq::event_flags::pollin));
276 message_received =
true;
278 CHECK_NOTHROW(active_poller.add(
s.server, zmq::event_flags::pollin,
handler));
279 CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
280 CHECK(message_received);
284 TEST_CASE(
"client server",
"[active_poller]")
289 server_client_setup
s;
292 zmq::active_poller_t active_poller;
293 zmq::event_flags events;
294 zmq::active_poller_t::handler_type
handler = [&](zmq::event_flags e) {
295 if (zmq::event_flags::none != (e & zmq::event_flags::pollin)) {
297 CHECK_NOTHROW(
s.server.recv(zmq_msg));
299 CHECK(send_msg == recv_msg);
300 }
else if (zmq::event_flags::none != (e & ~zmq::event_flags::pollout)) {
301 INFO(
"Unexpected event type " <<
static_cast<short>(events));
307 CHECK_NOTHROW(active_poller.add(
s.server, zmq::event_flags::pollin,
handler));
310 CHECK_NOTHROW(
s.client.send(
zmq::message_t{send_msg}, zmq::send_flags::none));
312 CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
313 CHECK(events == zmq::event_flags::pollin);
316 CHECK_NOTHROW(active_poller.remove(
s.server));
317 CHECK_NOTHROW(active_poller.add(
318 s.server, zmq::event_flags::pollin | zmq::event_flags::pollout,
handler));
319 CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
320 CHECK(events == zmq::event_flags::pollout);
323 TEST_CASE(
"add invalid socket throws",
"[active_poller]")
326 zmq::active_poller_t active_poller;
329 CHECK_THROWS_AS(active_poller.add(
a, zmq::event_flags::pollin, no_op_handler),
333 TEST_CASE(
"remove invalid socket throws",
"[active_poller]")
337 zmq::active_poller_t active_poller;
339 active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
340 CHECK(1u == active_poller.size());
341 std::vector<zmq::socket_t> sockets;
342 sockets.emplace_back(std::move(socket));
343 CHECK_THROWS_AS(active_poller.remove(socket),
zmq::error_t);
344 CHECK(1u == active_poller.size());
347 TEST_CASE(
"wait on added empty handler",
"[active_poller]")
349 server_client_setup
s;
350 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
351 zmq::active_poller_t active_poller;
353 active_poller.add(
s.server, zmq::event_flags::pollin, no_op_handler));
354 CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{-1}));
357 TEST_CASE(
"modify empty throws",
"[active_poller]")
361 zmq::active_poller_t active_poller;
362 CHECK_THROWS_AS(active_poller.modify(socket, zmq::event_flags::pollin),
366 TEST_CASE(
"modify invalid socket throws",
"[active_poller]")
371 zmq::active_poller_t active_poller;
372 CHECK_THROWS_AS(active_poller.modify(
a, zmq::event_flags::pollin),
376 TEST_CASE(
"modify not added throws",
"[active_poller]")
381 zmq::active_poller_t active_poller;
382 CHECK_NOTHROW(active_poller.add(
a, zmq::event_flags::pollin, no_op_handler));
383 CHECK_THROWS_AS(active_poller.modify(
b, zmq::event_flags::pollin),
387 TEST_CASE(
"modify simple",
"[active_poller]")
391 zmq::active_poller_t active_poller;
392 CHECK_NOTHROW(active_poller.add(
a, zmq::event_flags::pollin, no_op_handler));
394 active_poller.modify(
a, zmq::event_flags::pollin | zmq::event_flags::pollout));
397 TEST_CASE(
"poll client server",
"[active_poller]")
400 server_client_setup
s;
403 zmq::active_poller_t active_poller;
404 CHECK_NOTHROW(active_poller.add(
s.server, zmq::event_flags::pollin,
s.handler));
407 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
410 CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{500}));
411 CHECK(
s.events == zmq::event_flags::pollin);
414 CHECK_NOTHROW(active_poller.modify(
s.server, zmq::event_flags::pollin
415 | zmq::event_flags::pollout));
416 CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
417 CHECK(
s.events == (zmq::event_flags::pollin | zmq::event_flags::pollout));
420 TEST_CASE(
"wait one return",
"[active_poller]")
423 server_client_setup
s;
428 zmq::active_poller_t active_poller;
429 CHECK_NOTHROW(active_poller.add(
s.server, zmq::event_flags::pollin,
430 [&
count](zmq::event_flags) { ++count; }));
433 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
436 CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
440 TEST_CASE(
"wait on move constructed active_poller",
"[active_poller]")
442 server_client_setup
s;
443 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
444 zmq::active_poller_t
a;
445 CHECK_NOTHROW(
a.add(
s.server, zmq::event_flags::pollin, no_op_handler));
446 zmq::active_poller_t
b{std::move(
a)};
450 CHECK(
b.wait(std::chrono::milliseconds{-1}));
453 TEST_CASE(
"wait on move assigned active_poller",
"[active_poller]")
455 server_client_setup
s;
456 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
457 zmq::active_poller_t
a;
458 CHECK_NOTHROW(
a.add(
s.server, zmq::event_flags::pollin, no_op_handler));
459 zmq::active_poller_t
b;
464 CHECK(
b.wait(std::chrono::milliseconds{-1}));
467 TEST_CASE(
"received on move constructed active_poller",
"[active_poller]")
470 server_client_setup
s;
473 zmq::active_poller_t
a;
474 CHECK_NOTHROW(
a.add(
s.server, zmq::event_flags::pollin,
475 [&
count](zmq::event_flags) { ++count; }));
477 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
479 CHECK(1 ==
a.wait(std::chrono::milliseconds{500}));
482 zmq::active_poller_t
b{std::move(
a)};
484 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
486 CHECK(1 ==
b.wait(std::chrono::milliseconds{500}));
491 TEST_CASE(
"remove from handler",
"[active_poller]")
493 constexpr
size_t ITER_NO = 10;
496 std::vector<server_client_setup> setup_list;
497 for (
size_t i = 0;
i < ITER_NO; ++
i)
498 setup_list.emplace_back(server_client_setup{});
501 zmq::active_poller_t active_poller;
503 for (
size_t i = 0;
i < ITER_NO; ++
i) {
504 CHECK_NOTHROW(active_poller.add(
505 setup_list[
i].server, zmq::event_flags::pollin,
506 [&,
i](zmq::event_flags events) {
507 CHECK(events == zmq::event_flags::pollin);
508 active_poller.remove(setup_list[ITER_NO - i - 1].server);
509 CHECK((ITER_NO - i - 1) == active_poller.size());
513 CHECK(ITER_NO == active_poller.size());
515 for (
auto &s : setup_list) {
516 CHECK_NOTHROW(
s.client.send(
zmq::message_t{hi_str}, zmq::send_flags::none));
520 for (
auto &s : setup_list) {
526 CHECK(ITER_NO == active_poller.wait(std::chrono::milliseconds{-1}));