Skip to content

Commit

Permalink
Add requirement that the handler is non-null in active_poller
Browse files Browse the repository at this point in the history
  • Loading branch information
gummif committed May 24, 2020
1 parent e9c5546 commit 8b64a34
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
57 changes: 32 additions & 25 deletions tests/active_poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value,
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value,
"active_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;
Expand Down Expand Up @@ -63,7 +66,7 @@ TEST_CASE("move assign non empty", "[active_poller]")
zmq::socket_t socket{context, zmq::socket_type::router};

zmq::active_poller_t a;
a.add(socket, zmq::event_flags::pollin, [](zmq::event_flags) {});
a.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_FALSE(a.empty());
CHECK(1u == a.size());
zmq::active_poller_t b;
Expand All @@ -75,12 +78,22 @@ TEST_CASE("move assign non empty", "[active_poller]")
}

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_NOTHROW(active_poller.add(socket, zmq::event_flags::pollin, handler));
CHECK_THROWS_AS(active_poller.add(socket, zmq::event_flags::pollin, handler),
const std::invalid_argument &);
}

#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0)
Expand All @@ -90,11 +103,10 @@ 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;
zmq::active_poller_t::handler_type handler;
short invalid_events_type = 2 << 10;
CHECK_THROWS_AS(
active_poller.add(socket, static_cast<zmq::event_flags>(invalid_events_type),
handler),
no_op_handler),
const zmq::error_t &);
CHECK(active_poller.empty());
CHECK(0u == active_poller.size());
Expand All @@ -106,15 +118,16 @@ TEST_CASE("add handler twice throws", "[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;
active_poller.add(socket, zmq::event_flags::pollin, handler);
CHECK_THROWS_ZMQ_ERROR(EINVAL, active_poller.add(socket, zmq::event_flags::pollin, handler));
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_THROWS_ZMQ_ERROR(
EINVAL, active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
}

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}));
CHECK_THROWS_ZMQ_ERROR(EFAULT,
active_poller.wait(std::chrono::milliseconds{10}));
}

TEST_CASE("remove unregistered throws", "[active_poller]")
Expand All @@ -130,8 +143,7 @@ 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,
zmq::active_poller_t::handler_type{});
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_NOTHROW(active_poller.remove(socket));
}

Expand All @@ -140,7 +152,7 @@ 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, [](zmq::event_flags) {});
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
CHECK_NOTHROW(active_poller.remove(socket));
}

Expand Down Expand Up @@ -223,8 +235,7 @@ TEST_CASE("add invalid socket throws", "[active_poller]")
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,
zmq::active_poller_t::handler_type{}),
CHECK_THROWS_AS(active_poller.add(a, zmq::event_flags::pollin, no_op_handler),
const zmq::error_t &);
}

Expand All @@ -233,8 +244,8 @@ 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,
zmq::active_poller_t::handler_type{}));
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));
Expand All @@ -247,8 +258,8 @@ 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;
zmq::active_poller_t::handler_type handler;
CHECK_NOTHROW(active_poller.add(s.server, zmq::event_flags::pollin, handler));
CHECK_NOTHROW(
active_poller.add(s.server, zmq::event_flags::pollin, no_op_handler));
CHECK_NOTHROW(active_poller.wait(std::chrono::milliseconds{-1}));
}

Expand Down Expand Up @@ -277,8 +288,7 @@ TEST_CASE("modify not added throws", "[active_poller]")
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,
zmq::active_poller_t::handler_type{}));
CHECK_NOTHROW(active_poller.add(a, zmq::event_flags::pollin, no_op_handler));
CHECK_THROWS_AS(active_poller.modify(b, zmq::event_flags::pollin),
const zmq::error_t &);
}
Expand All @@ -288,8 +298,7 @@ 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,
zmq::active_poller_t::handler_type{}));
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));
}
Expand Down Expand Up @@ -342,8 +351,7 @@ 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;
zmq::active_poller_t::handler_type handler;
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, handler));
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());
Expand All @@ -356,8 +364,7 @@ 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;
zmq::active_poller_t::handler_type handler;
CHECK_NOTHROW(a.add(s.server, zmq::event_flags::pollin, handler));
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());
Expand Down
2 changes: 2 additions & 0 deletions zmq_addon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ class active_poller_t

void add(zmq::socket_ref socket, event_flags events, handler_type handler)
{
if (!handler)
throw std::invalid_argument("null handler in active_poller_t::add");
auto it = decltype(handlers)::iterator{};
auto inserted = bool{};
std::tie(it, inserted) = handlers.emplace(
Expand Down

0 comments on commit 8b64a34

Please sign in to comment.