2018-05-11 10:56:41 +02:00
|
|
|
#include <zmq_addon.hpp>
|
2018-04-03 16:30:58 +02:00
|
|
|
|
2018-05-11 11:43:41 +02:00
|
|
|
#include "testutil.hpp"
|
|
|
|
|
2021-07-14 00:56:19 +02:00
|
|
|
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL) && defined(ZMQ_BUILD_DRAFT_API)
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-04-09 09:22:31 +02:00
|
|
|
#include <array>
|
2018-04-17 10:29:47 +02:00
|
|
|
#include <memory>
|
2018-04-09 09:22:31 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("create destroy", "[active_poller]")
|
2018-04-13 18:38:51 +02:00
|
|
|
{
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(active_poller.empty());
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value,
|
2020-04-19 16:01:43 +02:00
|
|
|
"active_poller_t should not be copy-constructible");
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value,
|
2020-04-19 16:01:43 +02:00
|
|
|
"active_poller_t should not be copy-assignable");
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2020-05-25 00:34:20 +02:00
|
|
|
static const zmq::active_poller_t::handler_type no_op_handler =
|
|
|
|
[](zmq::event_flags) {};
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("move construct empty", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-05-11 11:09:09 +02:00
|
|
|
zmq::active_poller_t a;
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(a.empty());
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::active_poller_t b = std::move(a);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(b.empty());
|
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK(0u == b.size());
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("move assign empty", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-05-11 11:09:09 +02:00
|
|
|
zmq::active_poller_t a;
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(a.empty());
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t b;
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(b.empty());
|
2018-05-12 18:28:28 +02:00
|
|
|
b = std::move(a);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK(0u == b.size());
|
|
|
|
CHECK(a.empty());
|
|
|
|
CHECK(b.empty());
|
2018-04-10 18:35:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("move construct non empty", "[active_poller]")
|
2018-04-10 18:35:58 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
|
2018-05-11 11:09:09 +02:00
|
|
|
zmq::active_poller_t a;
|
2020-05-24 23:53:56 +02:00
|
|
|
a.add(socket, zmq::event_flags::pollin, [](zmq::event_flags) {});
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_FALSE(a.empty());
|
|
|
|
CHECK(1u == a.size());
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::active_poller_t b = std::move(a);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(a.empty());
|
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK_FALSE(b.empty());
|
|
|
|
CHECK(1u == b.size());
|
2018-04-10 18:35:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("move assign non empty", "[active_poller]")
|
2018-04-10 18:35:58 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
|
2018-05-11 11:09:09 +02:00
|
|
|
zmq::active_poller_t a;
|
2020-05-25 00:34:20 +02:00
|
|
|
a.add(socket, zmq::event_flags::pollin, no_op_handler);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_FALSE(a.empty());
|
|
|
|
CHECK(1u == a.size());
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t b;
|
2018-05-12 18:28:28 +02:00
|
|
|
b = std::move(a);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(a.empty());
|
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK_FALSE(b.empty());
|
|
|
|
CHECK(1u == b.size());
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("add handler", "[active_poller]")
|
2020-05-25 00:34:20 +02:00
|
|
|
{
|
|
|
|
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]")
|
2018-04-03 16:30:58 +02:00
|
|
|
{
|
2018-04-07 23:56:53 +02:00
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2019-05-15 08:35:12 +02:00
|
|
|
zmq::active_poller_t::handler_type handler;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_THROWS_AS(active_poller.add(socket, zmq::event_flags::pollin, handler),
|
2021-01-02 10:36:02 +01:00
|
|
|
std::invalid_argument);
|
2018-04-03 16:30:58 +02:00
|
|
|
}
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2019-02-04 14:53:03 +01:00
|
|
|
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0)
|
|
|
|
// this behaviour was added by https://github.com/zeromq/libzmq/pull/3100
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("add handler invalid events type", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2018-04-07 23:56:53 +02:00
|
|
|
short invalid_events_type = 2 << 10;
|
2020-01-26 10:09:20 +01:00
|
|
|
CHECK_THROWS_AS(
|
2020-05-24 23:53:56 +02:00
|
|
|
active_poller.add(socket, static_cast<zmq::event_flags>(invalid_events_type),
|
2020-05-25 00:34:20 +02:00
|
|
|
no_op_handler),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2019-02-04 14:53:03 +01:00
|
|
|
CHECK(active_poller.empty());
|
|
|
|
CHECK(0u == active_poller.size());
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
2019-02-04 14:53:03 +01:00
|
|
|
#endif
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("add handler twice throws", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2020-04-19 16:01:43 +02:00
|
|
|
common_server_client_setup s;
|
|
|
|
|
|
|
|
CHECK(s.client.send(zmq::message_t{}, zmq::send_flags::none));
|
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-04-19 16:01:43 +02:00
|
|
|
bool message_received = false;
|
|
|
|
active_poller.add(
|
|
|
|
s.server, zmq::event_flags::pollin,
|
|
|
|
[&message_received](zmq::event_flags) { message_received = true; });
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_THROWS_ZMQ_ERROR(
|
2020-04-19 16:01:43 +02:00
|
|
|
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
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("wait with no handlers throws", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_THROWS_ZMQ_ERROR(EFAULT,
|
|
|
|
active_poller.wait(std::chrono::milliseconds{10}));
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("remove unregistered throws", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:16:55 +02:00
|
|
|
CHECK_THROWS_ZMQ_ERROR(EINVAL, active_poller.remove(socket));
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("remove registered empty", "[active_poller]")
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(active_poller.remove(socket));
|
2018-04-10 18:35:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("remove registered non empty", "[active_poller]")
|
2018-04-10 18:35:58 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(active_poller.remove(socket));
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 21:29:15 +02:00
|
|
|
namespace
|
|
|
|
{
|
2018-05-11 11:43:41 +02:00
|
|
|
struct server_client_setup : common_server_client_setup
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2020-05-24 23:53:56 +02:00
|
|
|
zmq::active_poller_t::handler_type handler = [&](zmq::event_flags e) {
|
2020-01-26 10:09:20 +01:00
|
|
|
events = e;
|
|
|
|
};
|
2018-04-24 22:16:01 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
zmq::event_flags events = zmq::event_flags::none;
|
2018-04-24 22:16:01 +02:00
|
|
|
};
|
2020-05-16 15:22:52 +02:00
|
|
|
|
|
|
|
const std::string hi_str = "Hi";
|
|
|
|
|
2018-05-11 11:43:41 +02:00
|
|
|
}
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("poll basic", "[active_poller]")
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
server_client_setup s;
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2018-04-07 23:56:53 +02:00
|
|
|
bool message_received = false;
|
2020-05-24 23:53:56 +02:00
|
|
|
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;
|
|
|
|
};
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, handler));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
|
|
|
|
CHECK(message_received);
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:35:58 +02:00
|
|
|
/// \todo this contains multiple test cases that should be split up
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("client server", "[active_poller]")
|
2018-04-09 09:22:31 +02:00
|
|
|
{
|
2020-05-16 15:22:52 +02:00
|
|
|
const std::string send_msg = hi_str;
|
2018-04-09 09:22:31 +02:00
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
2018-04-09 09:22:31 +02:00
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
// Setup active_poller
|
|
|
|
zmq::active_poller_t active_poller;
|
2019-05-15 08:35:12 +02:00
|
|
|
zmq::event_flags events;
|
2020-05-24 23:53:56 +02:00
|
|
|
zmq::active_poller_t::handler_type handler = [&](zmq::event_flags e) {
|
2019-05-15 08:35:12 +02:00
|
|
|
if (zmq::event_flags::none != (e & zmq::event_flags::pollin)) {
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::message_t zmq_msg;
|
2019-04-22 23:15:51 +02:00
|
|
|
CHECK_NOTHROW(s.server.recv(zmq_msg)); // get message
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string recv_msg(zmq_msg.data<char>(), zmq_msg.size());
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(send_msg == recv_msg);
|
2019-05-15 08:35:12 +02:00
|
|
|
} else if (zmq::event_flags::none != (e & ~zmq::event_flags::pollout)) {
|
|
|
|
INFO("Unexpected event type " << static_cast<short>(events));
|
2018-09-23 18:14:25 +02:00
|
|
|
REQUIRE(false);
|
2018-04-09 09:22:31 +02:00
|
|
|
}
|
2018-04-24 22:16:01 +02:00
|
|
|
events = e;
|
2018-04-09 09:22:31 +02:00
|
|
|
};
|
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, handler));
|
2018-04-24 22:16:01 +02:00
|
|
|
|
|
|
|
// client sends message
|
2019-04-22 23:15:51 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{send_msg}, zmq::send_flags::none));
|
2018-04-09 09:22:31 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK(events == zmq::event_flags::pollin);
|
2018-04-09 09:22:31 +02:00
|
|
|
|
|
|
|
// Re-add server socket with pollout flag
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(active_poller.remove(s.server));
|
2020-05-24 23:53:56 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(
|
|
|
|
s.server, zmq::event_flags::pollin | zmq::event_flags::pollout, handler));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK(events == zmq::event_flags::pollout);
|
2018-04-09 09:22:31 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("add invalid socket throws", "[active_poller]")
|
2018-04-18 08:00:41 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t a{context, zmq::socket_type::router};
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::socket_t b{std::move(a)};
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_THROWS_AS(active_poller.add(a, zmq::event_flags::pollin, no_op_handler),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2018-04-18 08:00:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("remove invalid socket throws", "[active_poller]")
|
2018-04-18 08:00:41 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(
|
|
|
|
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1u == active_poller.size());
|
2018-04-18 08:00:41 +02:00
|
|
|
std::vector<zmq::socket_t> sockets;
|
2018-05-12 18:28:28 +02:00
|
|
|
sockets.emplace_back(std::move(socket));
|
2021-01-02 10:36:02 +01:00
|
|
|
CHECK_THROWS_AS(active_poller.remove(socket), zmq::error_t);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1u == active_poller.size());
|
2018-04-18 08:00:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("wait on added empty handler", "[active_poller]")
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
server_client_setup s;
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(
|
|
|
|
active_poller.add(s.server, zmq::event_flags::pollin, no_op_handler));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{-1}));
|
2018-04-24 22:16:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("modify empty throws", "[active_poller]")
|
2018-04-20 07:16:04 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t socket{context, zmq::socket_type::push};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-01-26 10:09:20 +01:00
|
|
|
CHECK_THROWS_AS(active_poller.modify(socket, zmq::event_flags::pollin),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2018-04-24 22:16:01 +02:00
|
|
|
}
|
2018-04-20 07:16:04 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("modify invalid socket throws", "[active_poller]")
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t a{context, zmq::socket_type::push};
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::socket_t b{std::move(a)};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-01-26 10:09:20 +01:00
|
|
|
CHECK_THROWS_AS(active_poller.modify(a, zmq::event_flags::pollin),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2018-04-24 22:16:01 +02:00
|
|
|
}
|
2018-04-20 07:16:04 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("modify not added throws", "[active_poller]")
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t a{context, zmq::socket_type::push};
|
|
|
|
zmq::socket_t b{context, zmq::socket_type::push};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(a, zmq::event_flags::pollin, no_op_handler));
|
2020-01-26 10:09:20 +01:00
|
|
|
CHECK_THROWS_AS(active_poller.modify(b, zmq::event_flags::pollin),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2018-04-20 07:16:04 +02:00
|
|
|
}
|
2018-04-24 22:16:01 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("modify simple", "[active_poller]")
|
2018-04-24 22:21:55 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-05-11 21:29:15 +02:00
|
|
|
zmq::socket_t a{context, zmq::socket_type::push};
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(a, zmq::event_flags::pollin, no_op_handler));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(
|
2020-05-24 23:53:56 +02:00
|
|
|
active_poller.modify(a, zmq::event_flags::pollin | zmq::event_flags::pollout));
|
2018-04-24 22:21:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("poll client server", "[active_poller]")
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
// Setup active_poller
|
|
|
|
zmq::active_poller_t active_poller;
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, s.handler));
|
2018-04-24 22:16:01 +02:00
|
|
|
|
|
|
|
// client sends message
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-04-24 22:16:01 +02:00
|
|
|
|
|
|
|
// wait for message and verify events
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{500}));
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK(s.events == zmq::event_flags::pollin);
|
2018-04-24 22:16:01 +02:00
|
|
|
|
|
|
|
// Modify server socket with pollout flag
|
2020-05-24 23:53:56 +02:00
|
|
|
CHECK_NOTHROW(active_poller.modify(s.server, zmq::event_flags::pollin
|
|
|
|
| zmq::event_flags::pollout));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
|
2019-05-15 08:35:12 +02:00
|
|
|
CHECK(s.events == (zmq::event_flags::pollin | zmq::event_flags::pollout));
|
2018-04-24 22:16:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("wait one return", "[active_poller]")
|
2018-05-03 22:10:05 +02:00
|
|
|
{
|
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
// Setup active_poller
|
|
|
|
zmq::active_poller_t active_poller;
|
2020-05-24 23:53:56 +02:00
|
|
|
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin,
|
|
|
|
[&count](zmq::event_flags) { ++count; }));
|
2018-05-03 22:10:05 +02:00
|
|
|
|
|
|
|
// client sends message
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-03 22:10:05 +02:00
|
|
|
|
|
|
|
// wait for message and verify events
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{500}));
|
|
|
|
CHECK(1u == count);
|
2018-05-03 22:10:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("wait on move constructed active_poller", "[active_poller]")
|
2018-05-09 06:57:44 +02:00
|
|
|
{
|
|
|
|
server_client_setup s;
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t a;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, no_op_handler));
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::active_poller_t b{std::move(a)};
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1u == b.size());
|
2020-05-25 00:16:55 +02:00
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK_THROWS_ZMQ_ERROR(EFAULT, a.wait(std::chrono::milliseconds{10}));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(b.wait(std::chrono::milliseconds{-1}));
|
2018-05-09 06:57:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("wait on move assigned active_poller", "[active_poller]")
|
2018-05-09 06:57:44 +02:00
|
|
|
{
|
|
|
|
server_client_setup s;
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t a;
|
2020-05-25 00:34:20 +02:00
|
|
|
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, no_op_handler));
|
2018-05-11 10:56:41 +02:00
|
|
|
zmq::active_poller_t b;
|
2018-05-12 18:28:28 +02:00
|
|
|
b = {std::move(a)};
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1u == b.size());
|
2020-05-25 00:16:55 +02:00
|
|
|
CHECK(0u == a.size());
|
|
|
|
CHECK_THROWS_ZMQ_ERROR(EFAULT, a.wait(std::chrono::milliseconds{10}));
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(b.wait(std::chrono::milliseconds{-1}));
|
2018-05-09 06:57:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("received on move constructed active_poller", "[active_poller]")
|
2018-05-09 06:57:44 +02:00
|
|
|
{
|
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
|
|
|
int count = 0;
|
2018-05-11 10:56:41 +02:00
|
|
|
// Setup active_poller a
|
|
|
|
zmq::active_poller_t a;
|
2020-05-24 23:53:56 +02:00
|
|
|
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin,
|
|
|
|
[&count](zmq::event_flags) { ++count; }));
|
2018-05-09 06:57:44 +02:00
|
|
|
// client sends message
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-09 06:57:44 +02:00
|
|
|
// wait for message and verify it is received
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == a.wait(std::chrono::milliseconds{500}));
|
|
|
|
CHECK(1u == count);
|
2018-05-11 10:56:41 +02:00
|
|
|
// Move construct active_poller b
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::active_poller_t b{std::move(a)};
|
2018-05-09 06:57:44 +02:00
|
|
|
// client sends message again
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-09 06:57:44 +02:00
|
|
|
// wait for message and verify it is received
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(1 == b.wait(std::chrono::milliseconds{500}));
|
|
|
|
CHECK(2u == count);
|
2018-05-09 06:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("remove from handler", "[active_poller]")
|
2018-05-03 22:10:05 +02:00
|
|
|
{
|
2019-04-14 23:34:15 +02:00
|
|
|
constexpr size_t ITER_NO = 10;
|
2018-05-03 22:10:05 +02:00
|
|
|
|
|
|
|
// Setup servers and clients
|
|
|
|
std::vector<server_client_setup> setup_list;
|
2019-04-14 23:34:15 +02:00
|
|
|
for (size_t i = 0; i < ITER_NO; ++i)
|
2018-05-12 18:28:28 +02:00
|
|
|
setup_list.emplace_back(server_client_setup{});
|
2018-05-03 22:10:05 +02:00
|
|
|
|
2018-05-11 10:56:41 +02:00
|
|
|
// Setup active_poller
|
|
|
|
zmq::active_poller_t active_poller;
|
2018-05-11 09:41:36 +02:00
|
|
|
int count = 0;
|
2019-04-14 23:34:15 +02:00
|
|
|
for (size_t i = 0; i < ITER_NO; ++i) {
|
2020-05-24 23:53:56 +02:00
|
|
|
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());
|
|
|
|
}));
|
2018-05-11 09:41:36 +02:00
|
|
|
++count;
|
2018-05-03 22:10:05 +02:00
|
|
|
}
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(ITER_NO == active_poller.size());
|
2018-05-03 22:10:05 +02:00
|
|
|
// Clients send messages
|
2018-05-11 21:29:15 +02:00
|
|
|
for (auto &s : setup_list) {
|
2020-05-16 15:22:52 +02:00
|
|
|
CHECK_NOTHROW(s.client.send(zmq::message_t{hi_str}, zmq::send_flags::none));
|
2018-05-03 22:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all servers to receive a message
|
2018-05-11 21:29:15 +02:00
|
|
|
for (auto &s : setup_list) {
|
|
|
|
zmq::pollitem_t items[] = {{s.server, 0, ZMQ_POLLIN, 0}};
|
2018-05-12 18:28:28 +02:00
|
|
|
zmq::poll(&items[0], 1);
|
2018-05-03 22:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fire all handlers in one wait
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(ITER_NO == active_poller.wait(std::chrono::milliseconds{-1}));
|
|
|
|
CHECK(ITER_NO == count);
|
2018-05-03 22:10:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:30:58 +02:00
|
|
|
#endif
|