2018-04-03 16:30:58 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <zmq.hpp>
|
|
|
|
|
|
|
|
#if defined(ZMQ_CPP11) && 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-04-13 18:38:51 +02:00
|
|
|
TEST(poller, create_destroy)
|
|
|
|
{
|
2018-04-07 23:56:53 +02:00
|
|
|
zmq::poller_t poller;
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:35:58 +02:00
|
|
|
static_assert(!std::is_copy_constructible<zmq::poller_t>::value, "poller_t should not be copy-constructible");
|
|
|
|
static_assert(!std::is_copy_assignable<zmq::poller_t>::value, "poller_t should not be copy-assignable");
|
|
|
|
|
|
|
|
TEST(poller, move_construct_empty)
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-04-13 18:35:41 +02:00
|
|
|
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
|
|
|
|
zmq::poller_t b = std::move(*a);
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
a.reset ();
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:35:58 +02:00
|
|
|
TEST(poller, move_assign_empty)
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-04-13 18:35:41 +02:00
|
|
|
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
|
|
|
|
zmq::poller_t b;
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
b = std::move(*a);
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
a.reset ();
|
2018-04-10 18:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, move_construct_non_empty)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
|
|
|
|
a->add(socket, ZMQ_POLLIN, [](short) {});
|
|
|
|
zmq::poller_t b = std::move(*a);
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
a.reset ();
|
2018-04-10 18:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, move_assign_non_empty)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
|
|
|
|
a->add(socket, ZMQ_POLLIN, [](short) {});
|
|
|
|
zmq::poller_t b;
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
b = std::move(*a);
|
2018-04-10 18:35:58 +02:00
|
|
|
|
2018-04-13 18:35:41 +02:00
|
|
|
a.reset ();
|
2018-04-07 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, add_handler)
|
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};
|
|
|
|
zmq::poller_t poller;
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::poller_t::handler_t handler;
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_NO_THROW(poller.add(socket, ZMQ_POLLIN, handler));
|
2018-04-03 16:30:58 +02:00
|
|
|
}
|
2018-04-07 23:56:53 +02:00
|
|
|
|
|
|
|
TEST(poller, add_handler_invalid_events_type)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
zmq::poller_t poller;
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::poller_t::handler_t handler;
|
2018-04-07 23:56:53 +02:00
|
|
|
short invalid_events_type = 2 << 10;
|
|
|
|
ASSERT_NO_THROW(poller.add(socket, invalid_events_type, handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, add_handler_twice_throws)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
zmq::poller_t poller;
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::poller_t::handler_t handler;
|
2018-04-07 23:56:53 +02:00
|
|
|
poller.add(socket, ZMQ_POLLIN, handler);
|
2018-04-13 18:35:41 +02:00
|
|
|
/// \todo the actual error code should be checked
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_THROW(poller.add(socket, ZMQ_POLLIN, handler), zmq::error_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, wait_with_no_handlers_throws)
|
|
|
|
{
|
|
|
|
zmq::poller_t poller;
|
2018-04-13 18:35:41 +02:00
|
|
|
/// \todo the actual error code should be checked
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_THROW(poller.wait(std::chrono::milliseconds{10}), zmq::error_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, remove_unregistered_throws)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
zmq::poller_t poller;
|
2018-04-13 18:35:41 +02:00
|
|
|
/// \todo the actual error code should be checked
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_THROW(poller.remove(socket), zmq::error_t);
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:35:58 +02:00
|
|
|
TEST(poller, remove_registered_empty)
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
|
|
zmq::poller_t poller;
|
2018-04-10 18:35:58 +02:00
|
|
|
poller.add(socket, ZMQ_POLLIN, zmq::poller_t::handler_t{});
|
|
|
|
ASSERT_NO_THROW(poller.remove(socket));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, remove_registered_non_empty)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket{context, zmq::socket_type::router};
|
2018-04-13 18:35:41 +02:00
|
|
|
zmq::poller_t poller;
|
2018-04-10 18:35:58 +02:00
|
|
|
poller.add(socket, ZMQ_POLLIN, [](short) {});
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_NO_THROW(poller.remove(socket));
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:22:31 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class loopback_ip4_binder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
loopback_ip4_binder(zmq::socket_t &socket) { bind(socket); }
|
|
|
|
std::string endpoint() { return endpoint_; }
|
|
|
|
private:
|
|
|
|
// Helper function used in constructor
|
2018-04-10 18:35:58 +02:00
|
|
|
// as Gtest allows ASSERT_* only in void returning functions
|
2018-04-09 09:22:31 +02:00
|
|
|
// and constructor/destructor are not.
|
|
|
|
void bind(zmq::socket_t &socket)
|
|
|
|
{
|
|
|
|
ASSERT_NO_THROW(socket.bind("tcp://127.0.0.1:*"));
|
|
|
|
std::array<char, 100> endpoint{};
|
|
|
|
size_t endpoint_size = endpoint.size();
|
|
|
|
ASSERT_NO_THROW(socket.getsockopt(ZMQ_LAST_ENDPOINT, endpoint.data(),
|
|
|
|
&endpoint_size));
|
|
|
|
ASSERT_TRUE(endpoint_size < endpoint.size());
|
|
|
|
endpoint_ = std::string{endpoint.data()};
|
|
|
|
}
|
|
|
|
std::string endpoint_;
|
|
|
|
};
|
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
struct server_client_setup
|
2018-04-07 23:56:53 +02:00
|
|
|
{
|
2018-04-24 22:16:01 +02:00
|
|
|
server_client_setup ()
|
|
|
|
{
|
|
|
|
init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
endpoint = loopback_ip4_binder {server}.endpoint ();
|
|
|
|
ASSERT_NO_THROW (client.connect (endpoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::poller_t::handler_t handler = [&](short e) {
|
|
|
|
events = e;
|
|
|
|
};
|
|
|
|
|
2018-04-07 23:56:53 +02:00
|
|
|
zmq::context_t context;
|
2018-04-24 22:16:01 +02:00
|
|
|
zmq::socket_t server {context, zmq::socket_type::router};
|
|
|
|
zmq::socket_t client {context, zmq::socket_type::dealer};
|
|
|
|
std::string endpoint;
|
|
|
|
short events = 0;
|
|
|
|
};
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
} //namespace
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
TEST(poller, poll_basic)
|
|
|
|
{
|
|
|
|
server_client_setup s;
|
2018-04-07 23:56:53 +02:00
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW(s.client.send("Hi"));
|
2018-04-07 23:56:53 +02:00
|
|
|
|
|
|
|
zmq::poller_t poller;
|
|
|
|
bool message_received = false;
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::poller_t::handler_t handler = [&message_received](short events) {
|
|
|
|
ASSERT_TRUE(0 != (events & ZMQ_POLLIN));
|
2018-04-07 23:56:53 +02:00
|
|
|
message_received = true;
|
|
|
|
};
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, handler));
|
2018-04-07 23:56:53 +02:00
|
|
|
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{-1}));
|
|
|
|
ASSERT_TRUE(message_received);
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:35:58 +02:00
|
|
|
/// \todo this contains multiple test cases that should be split up
|
2018-04-09 09:22:31 +02:00
|
|
|
TEST(poller, client_server)
|
|
|
|
{
|
2018-04-10 18:35:58 +02:00
|
|
|
const std::string send_msg = "Hi";
|
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
|
|
|
|
|
|
|
// Setup poller
|
|
|
|
zmq::poller_t poller;
|
2018-04-24 22:16:01 +02:00
|
|
|
short events;
|
|
|
|
zmq::poller_t::handler_t handler = [&](short e) {
|
|
|
|
if (0 != (e & ZMQ_POLLIN)) {
|
2018-04-09 09:22:31 +02:00
|
|
|
zmq::message_t zmq_msg;
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW(s.server.recv(&zmq_msg)); // skip msg id
|
|
|
|
ASSERT_NO_THROW(s.server.recv(&zmq_msg)); // get message
|
2018-04-09 09:22:31 +02:00
|
|
|
std::string recv_msg(zmq_msg.data<char>(),
|
|
|
|
zmq_msg.size());
|
|
|
|
ASSERT_EQ(send_msg, recv_msg);
|
2018-04-24 22:16:01 +02:00
|
|
|
} else if (0 != (e & ~ZMQ_POLLOUT)) {
|
2018-04-09 09:22:31 +02:00
|
|
|
ASSERT_TRUE(false) << "Unexpected event type " << events;
|
|
|
|
}
|
2018-04-24 22:16:01 +02:00
|
|
|
events = e;
|
2018-04-09 09:22:31 +02:00
|
|
|
};
|
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, handler));
|
|
|
|
|
|
|
|
// client sends message
|
|
|
|
ASSERT_NO_THROW(s.client.send(send_msg));
|
2018-04-09 09:22:31 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{-1}));
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_EQ(events, ZMQ_POLLIN);
|
2018-04-09 09:22:31 +02:00
|
|
|
|
|
|
|
// Re-add server socket with pollout flag
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW(poller.remove(s.server));
|
|
|
|
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN | ZMQ_POLLOUT, handler));
|
2018-04-09 09:22:31 +02:00
|
|
|
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{-1}));
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_EQ(events, ZMQ_POLLOUT);
|
2018-04-09 09:22:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 08:00:41 +02:00
|
|
|
TEST(poller, poller_add_invalid_socket_throws)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::poller_t poller;
|
|
|
|
zmq::socket_t a {context, zmq::socket_type::router};
|
|
|
|
zmq::socket_t b {std::move (a)};
|
|
|
|
ASSERT_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}),
|
|
|
|
zmq::error_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, poller_remove_invalid_socket_throws)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t socket {context, zmq::socket_type::router};
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
|
|
|
|
std::vector<zmq::socket_t> sockets;
|
|
|
|
sockets.emplace_back (std::move (socket));
|
|
|
|
ASSERT_THROW (poller.remove (socket), zmq::error_t);
|
|
|
|
}
|
|
|
|
|
2018-04-20 07:16:04 +02:00
|
|
|
TEST(poller, wait_on_added_empty_handler)
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
server_client_setup s;
|
2018-04-26 20:50:14 +02:00
|
|
|
ASSERT_NO_THROW (s.client.send ("Hi"));
|
2018-04-24 22:16:01 +02:00
|
|
|
zmq::poller_t poller;
|
2018-04-26 20:50:14 +02:00
|
|
|
zmq::poller_t::handler_t handler;
|
|
|
|
ASSERT_NO_THROW (poller.add (s.server, ZMQ_POLLIN, handler));
|
|
|
|
ASSERT_NO_THROW (poller.wait (std::chrono::milliseconds {-1}));
|
2018-04-24 22:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, modify_empty_throws)
|
2018-04-20 07:16:04 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
2018-04-24 22:16:01 +02:00
|
|
|
zmq::socket_t socket {context, zmq::socket_type::push};
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_THROW (poller.modify (socket, ZMQ_POLLIN), zmq::error_t);
|
|
|
|
}
|
2018-04-20 07:16:04 +02:00
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
TEST(poller, modify_invalid_socket_throws)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t a {context, zmq::socket_type::push};
|
|
|
|
zmq::socket_t b {std::move (a)};
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_THROW (poller.modify (a, ZMQ_POLLIN), zmq::error_t);
|
|
|
|
}
|
2018-04-20 07:16:04 +02:00
|
|
|
|
2018-04-24 22:21:55 +02:00
|
|
|
TEST(poller, modify_not_added_throws)
|
2018-04-24 22:16:01 +02:00
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t a {context, zmq::socket_type::push};
|
|
|
|
zmq::socket_t b {context, zmq::socket_type::push};
|
2018-04-20 07:16:04 +02:00
|
|
|
zmq::poller_t poller;
|
2018-04-24 22:16:01 +02:00
|
|
|
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
|
|
|
|
ASSERT_THROW (poller.modify (b, ZMQ_POLLIN), zmq::error_t);
|
2018-04-20 07:16:04 +02:00
|
|
|
}
|
2018-04-24 22:16:01 +02:00
|
|
|
|
2018-04-24 22:21:55 +02:00
|
|
|
TEST(poller, modify_simple)
|
|
|
|
{
|
|
|
|
zmq::context_t context;
|
|
|
|
zmq::socket_t a {context, zmq::socket_type::push};
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
|
|
|
|
ASSERT_NO_THROW (poller.modify (a, ZMQ_POLLIN|ZMQ_POLLOUT));
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:16:01 +02:00
|
|
|
TEST(poller, poll_client_server)
|
|
|
|
{
|
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
|
|
|
|
|
|
|
// Setup poller
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, s.handler));
|
|
|
|
|
|
|
|
// client sends message
|
|
|
|
ASSERT_NO_THROW(s.client.send("Hi"));
|
|
|
|
|
|
|
|
// wait for message and verify events
|
|
|
|
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{500}));
|
|
|
|
ASSERT_TRUE(s.events == ZMQ_POLLIN);
|
|
|
|
ASSERT_EQ(s.events, ZMQ_POLLIN);
|
|
|
|
|
|
|
|
// Modify server socket with pollout flag
|
|
|
|
ASSERT_NO_THROW(poller.modify(s.server, ZMQ_POLLIN | ZMQ_POLLOUT));
|
|
|
|
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{500}));
|
|
|
|
ASSERT_EQ(s.events, ZMQ_POLLIN | ZMQ_POLLOUT);
|
|
|
|
}
|
|
|
|
|
2018-05-03 22:10:05 +02:00
|
|
|
TEST(poller, wait_one_return)
|
|
|
|
{
|
|
|
|
// Setup server and client
|
|
|
|
server_client_setup s;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
// Setup poller
|
|
|
|
zmq::poller_t poller;
|
|
|
|
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, [&count](short) {
|
|
|
|
++count;
|
|
|
|
}));
|
|
|
|
|
|
|
|
// client sends message
|
|
|
|
ASSERT_NO_THROW(s.client.send("Hi"));
|
|
|
|
|
|
|
|
// wait for message and verify events
|
|
|
|
int result = poller.wait(std::chrono::milliseconds{500});
|
|
|
|
ASSERT_EQ(count, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(poller, remove_from_handler)
|
|
|
|
{
|
|
|
|
constexpr auto ITER_NO = 10;
|
|
|
|
|
|
|
|
// Setup servers and clients
|
|
|
|
std::vector<server_client_setup> setup_list;
|
|
|
|
for (auto i = 0; i < ITER_NO; ++i)
|
|
|
|
setup_list.emplace_back (server_client_setup{});
|
|
|
|
|
|
|
|
// Setup poller
|
|
|
|
zmq::poller_t poller;
|
|
|
|
for (auto i = 0; i < ITER_NO; ++i) {
|
|
|
|
ASSERT_NO_THROW(poller.add(setup_list[i].server, ZMQ_POLLIN, [&,i](short events) {
|
|
|
|
ASSERT_EQ(events, ZMQ_POLLIN);
|
|
|
|
poller.remove(setup_list[ITER_NO - i -1].server);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
// Clients send messages
|
|
|
|
for (auto & s : setup_list) {
|
|
|
|
ASSERT_NO_THROW(s.client.send("Hi"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
int count = poller.wait (std::chrono::milliseconds{-1});
|
|
|
|
ASSERT_EQ(count, ITER_NO);
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:30:58 +02:00
|
|
|
#endif
|