Problem: project files do not follow clang-format

This commit is contained in:
Pawel Kurdybacha 2018-05-11 20:29:15 +01:00
parent 7732903177
commit 5031278f18
9 changed files with 1319 additions and 1252 deletions

View File

@ -1,8 +1,7 @@
#include <zmq.hpp> #include <zmq.hpp>
int main(int argc, char** argv) int main (int argc, char **argv)
{ {
zmq::context_t context; zmq::context_t context;
return 0; return 0;
} }

View File

@ -7,83 +7,85 @@
#include <array> #include <array>
#include <memory> #include <memory>
TEST(active_poller, create_destroy) TEST (active_poller, create_destroy)
{ {
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_TRUE(active_poller.empty ()); ASSERT_TRUE (active_poller.empty ());
} }
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value, "active_active_poller_t should not be copy-constructible"); 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"); "active_active_poller_t should not be copy-constructible");
static_assert (!std::is_copy_assignable<zmq::active_poller_t>::value,
"active_active_poller_t should not be copy-assignable");
TEST(active_poller, move_construct_empty) TEST (active_poller, move_construct_empty)
{ {
zmq::active_poller_t a; zmq::active_poller_t a;
ASSERT_TRUE(a.empty ()); ASSERT_TRUE (a.empty ());
zmq::active_poller_t b = std::move (a); zmq::active_poller_t b = std::move (a);
ASSERT_TRUE(b.empty ()); ASSERT_TRUE (b.empty ());
ASSERT_EQ(0u, a.size ()); ASSERT_EQ (0u, a.size ());
ASSERT_EQ(0u, b.size ()); ASSERT_EQ (0u, b.size ());
} }
TEST(active_poller, move_assign_empty) TEST (active_poller, move_assign_empty)
{ {
zmq::active_poller_t a; zmq::active_poller_t a;
ASSERT_TRUE(a.empty()); ASSERT_TRUE (a.empty ());
zmq::active_poller_t b; zmq::active_poller_t b;
ASSERT_TRUE(b.empty()); ASSERT_TRUE (b.empty ());
b = std::move(a); b = std::move (a);
ASSERT_EQ(0u, a.size ()); ASSERT_EQ (0u, a.size ());
ASSERT_EQ(0u, b.size ()); ASSERT_EQ (0u, b.size ());
ASSERT_TRUE(a.empty()); ASSERT_TRUE (a.empty ());
ASSERT_TRUE(b.empty()); ASSERT_TRUE (b.empty ());
} }
TEST(active_poller, move_construct_non_empty) TEST (active_poller, move_construct_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t a; zmq::active_poller_t a;
a.add(socket, ZMQ_POLLIN, [](short) {}); a.add (socket, ZMQ_POLLIN, [](short) {});
ASSERT_FALSE(a.empty ()); ASSERT_FALSE (a.empty ());
ASSERT_EQ(1u, a.size ()); ASSERT_EQ (1u, a.size ());
zmq::active_poller_t b = std::move (a); zmq::active_poller_t b = std::move (a);
ASSERT_TRUE(a.empty ()); ASSERT_TRUE (a.empty ());
ASSERT_EQ(0u, a.size ()); ASSERT_EQ (0u, a.size ());
ASSERT_FALSE(b.empty ()); ASSERT_FALSE (b.empty ());
ASSERT_EQ(1u, b.size ()); ASSERT_EQ (1u, b.size ());
} }
TEST(active_poller, move_assign_non_empty) TEST (active_poller, move_assign_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t a; zmq::active_poller_t a;
a.add(socket, ZMQ_POLLIN, [](short) {}); a.add (socket, ZMQ_POLLIN, [](short) {});
ASSERT_FALSE(a.empty()); ASSERT_FALSE (a.empty ());
ASSERT_EQ(1u, a.size ()); ASSERT_EQ (1u, a.size ());
zmq::active_poller_t b; zmq::active_poller_t b;
b = std::move(a); b = std::move (a);
ASSERT_TRUE(a.empty ()); ASSERT_TRUE (a.empty ());
ASSERT_EQ(0u, a.size ()); ASSERT_EQ (0u, a.size ());
ASSERT_FALSE(b.empty ()); ASSERT_FALSE (b.empty ());
ASSERT_EQ(1u, b.size ()); ASSERT_EQ (1u, b.size ());
} }
TEST(active_poller, add_handler) TEST (active_poller, add_handler)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler; zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW(active_poller.add(socket, ZMQ_POLLIN, handler)); ASSERT_NO_THROW (active_poller.add (socket, ZMQ_POLLIN, handler));
} }
TEST(active_poller, add_handler_invalid_events_type) TEST (active_poller, add_handler_invalid_events_type)
{ {
/// \todo is it good that this is accepted? should probably already be /// \todo is it good that this is accepted? should probably already be
/// checked by zmq_poller_add/modify in libzmq: /// checked by zmq_poller_add/modify in libzmq:
/// https://github.com/zeromq/libzmq/issues/3088 /// https://github.com/zeromq/libzmq/issues/3088
zmq::context_t context; zmq::context_t context;
@ -91,86 +93,88 @@ TEST(active_poller, add_handler_invalid_events_type)
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler; zmq::active_poller_t::handler_t handler;
short invalid_events_type = 2 << 10; short invalid_events_type = 2 << 10;
ASSERT_NO_THROW(active_poller.add(socket, invalid_events_type, handler)); ASSERT_NO_THROW (active_poller.add (socket, invalid_events_type, handler));
ASSERT_FALSE(active_poller.empty ()); ASSERT_FALSE (active_poller.empty ());
ASSERT_EQ(1u, active_poller.size ()); ASSERT_EQ (1u, active_poller.size ());
} }
TEST(active_poller, add_handler_twice_throws) TEST (active_poller, add_handler_twice_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler; zmq::active_poller_t::handler_t handler;
active_poller.add(socket, ZMQ_POLLIN, handler); active_poller.add (socket, ZMQ_POLLIN, handler);
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(active_poller.add(socket, ZMQ_POLLIN, handler), zmq::error_t); ASSERT_THROW (active_poller.add (socket, ZMQ_POLLIN, handler),
zmq::error_t);
} }
TEST(active_poller, wait_with_no_handlers_throws) TEST (active_poller, wait_with_no_handlers_throws)
{ {
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(active_poller.wait(std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (active_poller.wait (std::chrono::milliseconds{10}),
zmq::error_t);
} }
TEST(active_poller, remove_unregistered_throws) TEST (active_poller, remove_unregistered_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(active_poller.remove(socket), zmq::error_t); ASSERT_THROW (active_poller.remove (socket), zmq::error_t);
} }
TEST(active_poller, remove_registered_empty) TEST (active_poller, remove_registered_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
active_poller.add(socket, ZMQ_POLLIN, zmq::active_poller_t::handler_t{}); active_poller.add (socket, ZMQ_POLLIN, zmq::active_poller_t::handler_t{});
ASSERT_NO_THROW(active_poller.remove(socket)); ASSERT_NO_THROW (active_poller.remove (socket));
} }
TEST(active_poller, remove_registered_non_empty) TEST (active_poller, remove_registered_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
active_poller.add(socket, ZMQ_POLLIN, [](short) {}); active_poller.add (socket, ZMQ_POLLIN, [](short) {});
ASSERT_NO_THROW(active_poller.remove(socket)); ASSERT_NO_THROW (active_poller.remove (socket));
} }
namespace { namespace
{
struct server_client_setup : common_server_client_setup struct server_client_setup : common_server_client_setup
{ {
zmq::active_poller_t::handler_t handler = [&](short e) { zmq::active_poller_t::handler_t handler = [&](short e) { events = e; };
events = e;
};
short events = 0; short events = 0;
}; };
} }
TEST(active_poller, poll_basic) TEST (active_poller, poll_basic)
{ {
server_client_setup s; server_client_setup s;
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
bool message_received = false; bool message_received = false;
zmq::active_poller_t::handler_t handler = [&message_received](short events) { zmq::active_poller_t::handler_t handler =
ASSERT_TRUE(0 != (events & ZMQ_POLLIN)); [&message_received](short events) {
message_received = true; ASSERT_TRUE (0 != (events & ZMQ_POLLIN));
}; message_received = true;
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, handler)); };
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1})); ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, handler));
ASSERT_TRUE(message_received); ASSERT_EQ (1, active_poller.wait (std::chrono::milliseconds{-1}));
ASSERT_TRUE (message_received);
} }
/// \todo this contains multiple test cases that should be split up /// \todo this contains multiple test cases that should be split up
TEST(active_poller, client_server) TEST (active_poller, client_server)
{ {
const std::string send_msg = "Hi"; const std::string send_msg = "Hi";
@ -183,47 +187,49 @@ TEST(active_poller, client_server)
zmq::active_poller_t::handler_t handler = [&](short e) { zmq::active_poller_t::handler_t handler = [&](short e) {
if (0 != (e & ZMQ_POLLIN)) { if (0 != (e & ZMQ_POLLIN)) {
zmq::message_t zmq_msg; zmq::message_t zmq_msg;
ASSERT_NO_THROW(s.server.recv(&zmq_msg)); // get message ASSERT_NO_THROW (s.server.recv (&zmq_msg)); // get message
std::string recv_msg(zmq_msg.data<char>(), std::string recv_msg (zmq_msg.data<char> (), zmq_msg.size ());
zmq_msg.size()); ASSERT_EQ (send_msg, recv_msg);
ASSERT_EQ(send_msg, recv_msg);
} else if (0 != (e & ~ZMQ_POLLOUT)) { } else if (0 != (e & ~ZMQ_POLLOUT)) {
ASSERT_TRUE(false) << "Unexpected event type " << events; ASSERT_TRUE (false) << "Unexpected event type " << events;
} }
events = e; events = e;
}; };
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, handler)); ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, handler));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send(send_msg)); ASSERT_NO_THROW (s.client.send (send_msg));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1})); ASSERT_EQ (1, active_poller.wait (std::chrono::milliseconds{-1}));
ASSERT_EQ(events, ZMQ_POLLIN); ASSERT_EQ (events, ZMQ_POLLIN);
// Re-add server socket with pollout flag // Re-add server socket with pollout flag
ASSERT_NO_THROW(active_poller.remove(s.server)); ASSERT_NO_THROW (active_poller.remove (s.server));
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN | ZMQ_POLLOUT, handler)); ASSERT_NO_THROW (
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1})); active_poller.add (s.server, ZMQ_POLLIN | ZMQ_POLLOUT, handler));
ASSERT_EQ(events, ZMQ_POLLOUT); ASSERT_EQ (1, active_poller.wait (std::chrono::milliseconds{-1}));
ASSERT_EQ (events, ZMQ_POLLOUT);
} }
TEST(active_poller, add_invalid_socket_throws) TEST (active_poller, add_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
zmq::socket_t a {context, zmq::socket_type::router}; zmq::socket_t a{context, zmq::socket_type::router};
zmq::socket_t b {std::move (a)}; zmq::socket_t b{std::move (a)};
ASSERT_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {}), ASSERT_THROW (
zmq::error_t); active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t{}),
zmq::error_t);
} }
TEST(active_poller, remove_invalid_socket_throws) TEST (active_poller, remove_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (socket, ZMQ_POLLIN, zmq::active_poller_t::handler_t {})); ASSERT_NO_THROW (active_poller.add (socket, ZMQ_POLLIN,
zmq::active_poller_t::handler_t{}));
ASSERT_EQ (1u, active_poller.size ()); ASSERT_EQ (1u, active_poller.size ());
std::vector<zmq::socket_t> sockets; std::vector<zmq::socket_t> sockets;
sockets.emplace_back (std::move (socket)); sockets.emplace_back (std::move (socket));
@ -231,75 +237,77 @@ TEST(active_poller, remove_invalid_socket_throws)
ASSERT_EQ (1u, active_poller.size ()); ASSERT_EQ (1u, active_poller.size ());
} }
TEST(active_poller, wait_on_added_empty_handler) TEST (active_poller, wait_on_added_empty_handler)
{ {
server_client_setup s; server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler; zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, handler)); ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, handler));
ASSERT_NO_THROW (active_poller.wait (std::chrono::milliseconds {-1})); ASSERT_NO_THROW (active_poller.wait (std::chrono::milliseconds{-1}));
} }
TEST(active_poller, modify_empty_throws) TEST (active_poller, modify_empty_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::push}; zmq::socket_t socket{context, zmq::socket_type::push};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_THROW (active_poller.modify (socket, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (active_poller.modify (socket, ZMQ_POLLIN), zmq::error_t);
} }
TEST(active_poller, modify_invalid_socket_throws) TEST (active_poller, modify_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b {std::move (a)}; zmq::socket_t b{std::move (a)};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_THROW (active_poller.modify (a, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (active_poller.modify (a, ZMQ_POLLIN), zmq::error_t);
} }
TEST(active_poller, modify_not_added_throws) TEST (active_poller, modify_not_added_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b {context, zmq::socket_type::push}; zmq::socket_t b{context, zmq::socket_type::push};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {})); ASSERT_NO_THROW (
active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t{}));
ASSERT_THROW (active_poller.modify (b, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (active_poller.modify (b, ZMQ_POLLIN), zmq::error_t);
} }
TEST(active_poller, modify_simple) TEST (active_poller, modify_simple)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {})); ASSERT_NO_THROW (
ASSERT_NO_THROW (active_poller.modify (a, ZMQ_POLLIN|ZMQ_POLLOUT)); active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t{}));
ASSERT_NO_THROW (active_poller.modify (a, ZMQ_POLLIN | ZMQ_POLLOUT));
} }
TEST(active_poller, poll_client_server) TEST (active_poller, poll_client_server)
{ {
// Setup server and client // Setup server and client
server_client_setup s; server_client_setup s;
// Setup active_poller // Setup active_poller
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, s.handler)); ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, s.handler));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify events // wait for message and verify events
ASSERT_NO_THROW(active_poller.wait(std::chrono::milliseconds{500})); ASSERT_NO_THROW (active_poller.wait (std::chrono::milliseconds{500}));
ASSERT_EQ(s.events, ZMQ_POLLIN); ASSERT_EQ (s.events, ZMQ_POLLIN);
// Modify server socket with pollout flag // Modify server socket with pollout flag
ASSERT_NO_THROW(active_poller.modify(s.server, ZMQ_POLLIN | ZMQ_POLLOUT)); ASSERT_NO_THROW (active_poller.modify (s.server, ZMQ_POLLIN | ZMQ_POLLOUT));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{500})); ASSERT_EQ (1, active_poller.wait (std::chrono::milliseconds{500}));
ASSERT_EQ(s.events, ZMQ_POLLIN | ZMQ_POLLOUT); ASSERT_EQ (s.events, ZMQ_POLLIN | ZMQ_POLLOUT);
} }
TEST(active_poller, wait_one_return) TEST (active_poller, wait_one_return)
{ {
// Setup server and client // Setup server and client
server_client_setup s; server_client_setup s;
@ -308,33 +316,32 @@ TEST(active_poller, wait_one_return)
// Setup active_poller // Setup active_poller
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, [&count](short) { ASSERT_NO_THROW (
++count; active_poller.add (s.server, ZMQ_POLLIN, [&count](short) { ++count; }));
}));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify events // wait for message and verify events
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{500})); ASSERT_EQ (1, active_poller.wait (std::chrono::milliseconds{500}));
ASSERT_EQ(1u, count); ASSERT_EQ (1u, count);
} }
TEST(active_poller, wait_on_move_constructed_active_poller) TEST (active_poller, wait_on_move_constructed_active_poller)
{ {
server_client_setup s; server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::active_poller_t a; zmq::active_poller_t a;
zmq::active_poller_t::handler_t handler; zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler)); ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler));
zmq::active_poller_t b {std::move (a)}; zmq::active_poller_t b{std::move (a)};
ASSERT_EQ(1u, b.size ()); ASSERT_EQ (1u, b.size ());
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(a.wait(std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (a.wait (std::chrono::milliseconds{10}), zmq::error_t);
ASSERT_TRUE (b.wait (std::chrono::milliseconds {-1})); ASSERT_TRUE (b.wait (std::chrono::milliseconds{-1}));
} }
TEST(active_poller, wait_on_move_assigned_active_poller) TEST (active_poller, wait_on_move_assigned_active_poller)
{ {
server_client_setup s; server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
@ -343,38 +350,37 @@ TEST(active_poller, wait_on_move_assigned_active_poller)
ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler)); ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler));
zmq::active_poller_t b; zmq::active_poller_t b;
b = {std::move (a)}; b = {std::move (a)};
ASSERT_EQ(1u, b.size ()); ASSERT_EQ (1u, b.size ());
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(a.wait(std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (a.wait (std::chrono::milliseconds{10}), zmq::error_t);
ASSERT_TRUE (b.wait (std::chrono::milliseconds {-1})); ASSERT_TRUE (b.wait (std::chrono::milliseconds{-1}));
} }
TEST(active_poller, received_on_move_constructed_active_poller) TEST (active_poller, received_on_move_constructed_active_poller)
{ {
// Setup server and client // Setup server and client
server_client_setup s; server_client_setup s;
int count = 0; int count = 0;
// Setup active_poller a // Setup active_poller a
zmq::active_poller_t a; zmq::active_poller_t a;
ASSERT_NO_THROW(a.add(s.server, ZMQ_POLLIN, [&count](short) { ASSERT_NO_THROW (
++count; a.add (s.server, ZMQ_POLLIN, [&count](short) { ++count; }));
}));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify it is received // wait for message and verify it is received
ASSERT_EQ(1, a.wait(std::chrono::milliseconds{500})); ASSERT_EQ (1, a.wait (std::chrono::milliseconds{500}));
ASSERT_EQ(1u, count); ASSERT_EQ (1u, count);
// Move construct active_poller b // Move construct active_poller b
zmq::active_poller_t b{std::move(a)}; zmq::active_poller_t b{std::move (a)};
// client sends message again // client sends message again
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify it is received // wait for message and verify it is received
ASSERT_EQ(1, b.wait(std::chrono::milliseconds{500})); ASSERT_EQ (1, b.wait (std::chrono::milliseconds{500}));
ASSERT_EQ(2u, count); ASSERT_EQ (2u, count);
} }
TEST(active_poller, remove_from_handler) TEST (active_poller, remove_from_handler)
{ {
constexpr auto ITER_NO = 10; constexpr auto ITER_NO = 10;
@ -387,28 +393,29 @@ TEST(active_poller, remove_from_handler)
zmq::active_poller_t active_poller; zmq::active_poller_t active_poller;
int count = 0; int count = 0;
for (auto i = 0; i < ITER_NO; ++i) { for (auto i = 0; i < ITER_NO; ++i) {
ASSERT_NO_THROW(active_poller.add(setup_list[i].server, ZMQ_POLLIN, [&,i](short events) { ASSERT_NO_THROW (active_poller.add (
ASSERT_EQ(events, ZMQ_POLLIN); setup_list[i].server, ZMQ_POLLIN, [&, i](short events) {
active_poller.remove(setup_list[ITER_NO-i-1].server); ASSERT_EQ (events, ZMQ_POLLIN);
ASSERT_EQ(ITER_NO-i-1, active_poller.size()); active_poller.remove (setup_list[ITER_NO - i - 1].server);
})); ASSERT_EQ (ITER_NO - i - 1, active_poller.size ());
}));
++count; ++count;
} }
ASSERT_EQ(ITER_NO, active_poller.size()); ASSERT_EQ (ITER_NO, active_poller.size ());
// Clients send messages // Clients send messages
for (auto & s : setup_list) { for (auto &s : setup_list) {
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
} }
// Wait for all servers to receive a message // Wait for all servers to receive a message
for (auto & s : setup_list) { for (auto &s : setup_list) {
zmq::pollitem_t items [] = { { s.server, 0, ZMQ_POLLIN, 0 } }; zmq::pollitem_t items[] = {{s.server, 0, ZMQ_POLLIN, 0}};
zmq::poll (&items [0], 1); zmq::poll (&items[0], 1);
} }
// Fire all handlers in one wait // Fire all handlers in one wait
ASSERT_EQ(ITER_NO, active_poller.wait (std::chrono::milliseconds{-1})); ASSERT_EQ (ITER_NO, active_poller.wait (std::chrono::milliseconds{-1}));
ASSERT_EQ(ITER_NO, count); ASSERT_EQ (ITER_NO, count);
} }
#endif #endif

View File

@ -1,15 +1,15 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <zmq.hpp> #include <zmq.hpp>
TEST(context, create_default_destroy) TEST (context, create_default_destroy)
{ {
zmq::context_t context; zmq::context_t context;
} }
TEST(context, create_close) TEST (context, create_close)
{ {
zmq::context_t context; zmq::context_t context;
context.close(); context.close ();
ASSERT_EQ(NULL, (void*)context); ASSERT_EQ (NULL, (void *) context);
} }

View File

@ -1,7 +1,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
int main(int argc, char** argv) int main (int argc, char **argv)
{ {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS ();
} }

View File

@ -2,8 +2,10 @@
#include <zmq.hpp> #include <zmq.hpp>
#if defined(ZMQ_CPP11) #if defined(ZMQ_CPP11)
static_assert(!std::is_copy_constructible<zmq::message_t>::value, "message_t should not be copy-constructible"); static_assert (!std::is_copy_constructible<zmq::message_t>::value,
static_assert(!std::is_copy_assignable<zmq::message_t>::value, "message_t should not be copy-assignable"); "message_t should not be copy-constructible");
static_assert (!std::is_copy_assignable<zmq::message_t>::value,
"message_t should not be copy-assignable");
#endif #endif
TEST (message, constructor_default) TEST (message, constructor_default)
@ -12,7 +14,7 @@ TEST (message, constructor_default)
ASSERT_EQ (0u, message.size ()); ASSERT_EQ (0u, message.size ());
} }
const char* const data = "Hi"; const char *const data = "Hi";
TEST (message, constructor_iterators) TEST (message, constructor_iterators)
{ {
@ -30,7 +32,8 @@ TEST (message, constructor_pointer_size)
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2)); ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
} }
TEST (message, constructor_char_array) { TEST (message, constructor_char_array)
{
const zmq::message_t hi_msg (data, strlen (data)); const zmq::message_t hi_msg (data, strlen (data));
ASSERT_EQ (2u, hi_msg.size ()); ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2)); ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
@ -49,7 +52,7 @@ TEST (message, constructor_container)
#ifdef ZMQ_HAS_RVALUE_REFS #ifdef ZMQ_HAS_RVALUE_REFS
TEST (message, constructor_move) TEST (message, constructor_move)
{ {
zmq::message_t hi_msg (zmq::message_t(data, strlen (data))); zmq::message_t hi_msg (zmq::message_t (data, strlen (data)));
} }
TEST (message, assign_move_empty_before) TEST (message, assign_move_empty_before)
@ -63,50 +66,55 @@ TEST (message, assign_move_empty_before)
TEST (message, assign_move_empty_after) TEST (message, assign_move_empty_after)
{ {
zmq::message_t hi_msg (data, strlen (data)); zmq::message_t hi_msg (data, strlen (data));
hi_msg = zmq::message_t(); hi_msg = zmq::message_t ();
ASSERT_EQ (0u, hi_msg.size ()); ASSERT_EQ (0u, hi_msg.size ());
} }
TEST (message, assign_move_empty_before_and_after) TEST (message, assign_move_empty_before_and_after)
{ {
zmq::message_t hi_msg; zmq::message_t hi_msg;
hi_msg = zmq::message_t(); hi_msg = zmq::message_t ();
ASSERT_EQ (0u, hi_msg.size ()); ASSERT_EQ (0u, hi_msg.size ());
} }
#endif #endif
TEST (message, equality_self) { TEST (message, equality_self)
{
const zmq::message_t hi_msg (data, strlen (data)); const zmq::message_t hi_msg (data, strlen (data));
ASSERT_EQ (hi_msg, hi_msg); ASSERT_EQ (hi_msg, hi_msg);
} }
TEST (message, equality_equal) { TEST (message, equality_equal)
{
const zmq::message_t hi_msg_a (data, strlen (data)); const zmq::message_t hi_msg_a (data, strlen (data));
const zmq::message_t hi_msg_b (data, strlen (data)); const zmq::message_t hi_msg_b (data, strlen (data));
ASSERT_EQ (hi_msg_a, hi_msg_b); ASSERT_EQ (hi_msg_a, hi_msg_b);
} }
TEST (message, equality_equal_empty) { TEST (message, equality_equal_empty)
{
const zmq::message_t msg_a; const zmq::message_t msg_a;
const zmq::message_t msg_b; const zmq::message_t msg_b;
ASSERT_EQ (msg_a, msg_b); ASSERT_EQ (msg_a, msg_b);
} }
TEST (message, equality_non_equal) { TEST (message, equality_non_equal)
{
const zmq::message_t msg_a ("Hi", 2); const zmq::message_t msg_a ("Hi", 2);
const zmq::message_t msg_b ("Hello", 5); const zmq::message_t msg_b ("Hello", 5);
ASSERT_NE (msg_a, msg_b); ASSERT_NE (msg_a, msg_b);
} }
TEST (message, equality_non_equal_rhs_empty) { TEST (message, equality_non_equal_rhs_empty)
{
const zmq::message_t msg_a ("Hi", 2); const zmq::message_t msg_a ("Hi", 2);
const zmq::message_t msg_b; const zmq::message_t msg_b;
ASSERT_NE (msg_a, msg_b); ASSERT_NE (msg_a, msg_b);
} }
TEST (message, equality_non_equal_lhs_empty) { TEST (message, equality_non_equal_lhs_empty)
{
const zmq::message_t msg_a; const zmq::message_t msg_a;
const zmq::message_t msg_b ("Hi", 2); const zmq::message_t msg_b ("Hi", 2);
ASSERT_NE (msg_a, msg_b); ASSERT_NE (msg_a, msg_b);
} }

View File

@ -4,168 +4,170 @@
#ifdef ZMQ_HAS_RVALUE_REFS #ifdef ZMQ_HAS_RVALUE_REFS
/// \todo split this up into separate test cases /// \todo split this up into separate test cases
/// ///
TEST(multipart, legacy_test) TEST (multipart, legacy_test)
{ {
using namespace zmq; using namespace zmq;
bool ok = true; (void) ok; bool ok = true;
float num = 0; (void) num; (void) ok;
float num = 0;
(void) num;
std::string str = ""; std::string str = "";
message_t msg; message_t msg;
// Create two PAIR sockets and connect over inproc // Create two PAIR sockets and connect over inproc
context_t context(1); context_t context (1);
socket_t output(context, ZMQ_PAIR); socket_t output (context, ZMQ_PAIR);
socket_t input(context, ZMQ_PAIR); socket_t input (context, ZMQ_PAIR);
output.bind("inproc://multipart.test"); output.bind ("inproc://multipart.test");
input.connect("inproc://multipart.test"); input.connect ("inproc://multipart.test");
// Test send and receive of single-frame message // Test send and receive of single-frame message
multipart_t multipart; multipart_t multipart;
assert(multipart.empty()); assert (multipart.empty ());
multipart.push(message_t("Hello", 5)); multipart.push (message_t ("Hello", 5));
assert(multipart.size() == 1); assert (multipart.size () == 1);
ok = multipart.send(output); ok = multipart.send (output);
assert(multipart.empty()); assert (multipart.empty ());
assert(ok); assert (ok);
ok = multipart.recv(input); ok = multipart.recv (input);
assert(multipart.size() == 1); assert (multipart.size () == 1);
assert(ok); assert (ok);
msg = multipart.pop(); msg = multipart.pop ();
assert(multipart.empty()); assert (multipart.empty ());
assert(std::string(msg.data<char>(), msg.size()) == "Hello"); assert (std::string (msg.data<char> (), msg.size ()) == "Hello");
// Test send and receive of multi-frame message // Test send and receive of multi-frame message
multipart.addstr("A"); multipart.addstr ("A");
multipart.addstr("BB"); multipart.addstr ("BB");
multipart.addstr("CCC"); multipart.addstr ("CCC");
assert(multipart.size() == 3); assert (multipart.size () == 3);
multipart_t copy = multipart.clone(); multipart_t copy = multipart.clone ();
assert(copy.size() == 3); assert (copy.size () == 3);
ok = copy.send(output); ok = copy.send (output);
assert(copy.empty()); assert (copy.empty ());
assert(ok); assert (ok);
ok = copy.recv(input); ok = copy.recv (input);
assert(copy.size() == 3); assert (copy.size () == 3);
assert(ok); assert (ok);
assert(copy.equal(&multipart)); assert (copy.equal (&multipart));
multipart.clear(); multipart.clear ();
assert(multipart.empty()); assert (multipart.empty ());
// Test message frame manipulation // Test message frame manipulation
multipart.add(message_t("Frame5", 6)); multipart.add (message_t ("Frame5", 6));
multipart.addstr("Frame6"); multipart.addstr ("Frame6");
multipart.addstr("Frame7"); multipart.addstr ("Frame7");
multipart.addtyp(8.0f); multipart.addtyp (8.0f);
multipart.addmem("Frame9", 6); multipart.addmem ("Frame9", 6);
multipart.push(message_t("Frame4", 6)); multipart.push (message_t ("Frame4", 6));
multipart.pushstr("Frame3"); multipart.pushstr ("Frame3");
multipart.pushstr("Frame2"); multipart.pushstr ("Frame2");
multipart.pushtyp(1.0f); multipart.pushtyp (1.0f);
multipart.pushmem("Frame0", 6); multipart.pushmem ("Frame0", 6);
assert(multipart.size() == 10); assert (multipart.size () == 10);
msg = multipart.remove(); msg = multipart.remove ();
assert(multipart.size() == 9); assert (multipart.size () == 9);
assert(std::string(msg.data<char>(), msg.size()) == "Frame9"); assert (std::string (msg.data<char> (), msg.size ()) == "Frame9");
msg = multipart.pop(); msg = multipart.pop ();
assert(multipart.size() == 8); assert (multipart.size () == 8);
assert(std::string(msg.data<char>(), msg.size()) == "Frame0"); assert (std::string (msg.data<char> (), msg.size ()) == "Frame0");
num = multipart.poptyp<float>(); num = multipart.poptyp<float> ();
assert(multipart.size() == 7); assert (multipart.size () == 7);
assert(num == 1.0f); assert (num == 1.0f);
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 6); assert (multipart.size () == 6);
assert(str == "Frame2"); assert (str == "Frame2");
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 5); assert (multipart.size () == 5);
assert(str == "Frame3"); assert (str == "Frame3");
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 4); assert (multipart.size () == 4);
assert(str == "Frame4"); assert (str == "Frame4");
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 3); assert (multipart.size () == 3);
assert(str == "Frame5"); assert (str == "Frame5");
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 2); assert (multipart.size () == 2);
assert(str == "Frame6"); assert (str == "Frame6");
str = multipart.popstr(); str = multipart.popstr ();
assert(multipart.size() == 1); assert (multipart.size () == 1);
assert(str == "Frame7"); assert (str == "Frame7");
num = multipart.poptyp<float>(); num = multipart.poptyp<float> ();
assert(multipart.empty()); assert (multipart.empty ());
assert(num == 8.0f); assert (num == 8.0f);
// Test message constructors and concatenation // Test message constructors and concatenation
multipart_t head("One", 3); multipart_t head ("One", 3);
head.addstr("Two"); head.addstr ("Two");
assert(head.size() == 2); assert (head.size () == 2);
multipart_t tail(std::string("One-hundred")); multipart_t tail (std::string ("One-hundred"));
tail.pushstr("Ninety-nine"); tail.pushstr ("Ninety-nine");
assert(tail.size() == 2); assert (tail.size () == 2);
multipart_t tmp(message_t("Fifty", 5)); multipart_t tmp (message_t ("Fifty", 5));
assert(tmp.size() == 1); assert (tmp.size () == 1);
multipart_t mid = multipart_t::create(49.0f); multipart_t mid = multipart_t::create (49.0f);
mid.append(std::move(tmp)); mid.append (std::move (tmp));
assert(mid.size() == 2); assert (mid.size () == 2);
assert(tmp.empty()); assert (tmp.empty ());
multipart_t merged(std::move(mid)); multipart_t merged (std::move (mid));
merged.prepend(std::move(head)); merged.prepend (std::move (head));
merged.append(std::move(tail)); merged.append (std::move (tail));
assert(merged.size() == 6); assert (merged.size () == 6);
assert(head.empty()); assert (head.empty ());
assert(tail.empty()); assert (tail.empty ());
ok = merged.send(output); ok = merged.send (output);
assert(merged.empty()); assert (merged.empty ());
assert(ok); assert (ok);
multipart_t received(input); multipart_t received (input);
assert(received.size() == 6); assert (received.size () == 6);
str = received.popstr(); str = received.popstr ();
assert(received.size() == 5); assert (received.size () == 5);
assert(str == "One"); assert (str == "One");
str = received.popstr(); str = received.popstr ();
assert(received.size() == 4); assert (received.size () == 4);
assert(str == "Two"); assert (str == "Two");
num = received.poptyp<float>(); num = received.poptyp<float> ();
assert(received.size() == 3); assert (received.size () == 3);
assert(num == 49.0f); assert (num == 49.0f);
str = received.popstr(); str = received.popstr ();
assert(received.size() == 2); assert (received.size () == 2);
assert(str == "Fifty"); assert (str == "Fifty");
str = received.popstr(); str = received.popstr ();
assert(received.size() == 1); assert (received.size () == 1);
assert(str == "Ninety-nine"); assert (str == "Ninety-nine");
str = received.popstr(); str = received.popstr ();
assert(received.empty()); assert (received.empty ());
assert(str == "One-hundred"); assert (str == "One-hundred");
} }
#endif #endif

View File

@ -5,151 +5,153 @@
#include <array> #include <array>
#include <memory> #include <memory>
TEST(poller, create_destroy) TEST (poller, create_destroy)
{ {
zmq::poller_t<> poller; zmq::poller_t<> poller;
} }
static_assert(!std::is_copy_constructible<zmq::poller_t<>>::value, "poller_t should not be copy-constructible"); static_assert (!std::is_copy_constructible<zmq::poller_t<>>::value,
static_assert(!std::is_copy_assignable<zmq::poller_t<>>::value, "poller_t should not be copy-assignable"); "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) TEST (poller, move_construct_empty)
{ {
zmq::poller_t<> a; zmq::poller_t<> a;
zmq::poller_t<> b = std::move (a); zmq::poller_t<> b = std::move (a);
} }
TEST(poller, move_assign_empty) TEST (poller, move_assign_empty)
{ {
zmq::poller_t<> a; zmq::poller_t<> a;
zmq::poller_t<> b; zmq::poller_t<> b;
b = std::move(a); b = std::move (a);
} }
TEST(poller, move_construct_non_empty) TEST (poller, move_construct_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> a; zmq::poller_t<> a;
a.add(socket, ZMQ_POLLIN, nullptr); a.add (socket, ZMQ_POLLIN, nullptr);
zmq::poller_t<> b = std::move (a); zmq::poller_t<> b = std::move (a);
} }
TEST(poller, move_assign_non_empty) TEST (poller, move_assign_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> a; zmq::poller_t<> a;
a.add(socket, ZMQ_POLLIN, nullptr); a.add (socket, ZMQ_POLLIN, nullptr);
zmq::poller_t<> b; zmq::poller_t<> b;
b = std::move(a); b = std::move (a);
} }
TEST(poller, add_nullptr) TEST (poller, add_nullptr)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW(poller.add(socket, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, nullptr));
} }
TEST(poller, add_non_nullptr) TEST (poller, add_non_nullptr)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
int i; int i;
ASSERT_NO_THROW(poller.add(socket, ZMQ_POLLIN, &i)); ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, &i));
} }
TEST(poller, add_handler_invalid_events_type) TEST (poller, add_handler_invalid_events_type)
{ {
/// \todo is it good that this is accepted? should probably already be /// \todo is it good that this is accepted? should probably already be
/// checked by zmq_poller_add/modify in libzmq: /// checked by zmq_poller_add/modify in libzmq:
/// https://github.com/zeromq/libzmq/issues/3088 /// https://github.com/zeromq/libzmq/issues/3088
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
short invalid_events_type = 2 << 10; short invalid_events_type = 2 << 10;
ASSERT_NO_THROW(poller.add(socket, invalid_events_type, nullptr)); ASSERT_NO_THROW (poller.add (socket, invalid_events_type, nullptr));
} }
TEST(poller, add_handler_twice_throws) TEST (poller, add_handler_twice_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
poller.add(socket, ZMQ_POLLIN, nullptr); poller.add (socket, ZMQ_POLLIN, nullptr);
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(poller.add(socket, ZMQ_POLLIN, nullptr), zmq::error_t); ASSERT_THROW (poller.add (socket, ZMQ_POLLIN, nullptr), zmq::error_t);
} }
TEST(poller, wait_with_no_handlers_throws) TEST (poller, wait_with_no_handlers_throws)
{ {
zmq::poller_t<> poller; zmq::poller_t<> poller;
std::vector<zmq_poller_event_t> events; std::vector<zmq_poller_event_t> events;
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(poller.wait_all(events, std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (poller.wait_all (events, std::chrono::milliseconds{10}),
zmq::error_t);
} }
TEST(poller, remove_unregistered_throws) TEST (poller, remove_unregistered_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(poller.remove(socket), zmq::error_t); ASSERT_THROW (poller.remove (socket), zmq::error_t);
} }
TEST(poller, remove_registered_empty) TEST (poller, remove_registered_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
poller.add(socket, ZMQ_POLLIN, nullptr); poller.add (socket, ZMQ_POLLIN, nullptr);
ASSERT_NO_THROW(poller.remove(socket)); ASSERT_NO_THROW (poller.remove (socket));
} }
TEST(poller, remove_registered_non_empty) TEST (poller, remove_registered_non_empty)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
poller.add(socket, ZMQ_POLLIN, nullptr); poller.add (socket, ZMQ_POLLIN, nullptr);
ASSERT_NO_THROW(poller.remove(socket)); ASSERT_NO_THROW (poller.remove (socket));
} }
TEST(poller, poll_basic) TEST (poller, poll_basic)
{ {
common_server_client_setup s; common_server_client_setup s;
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::poller_t<int> poller; zmq::poller_t<int> poller;
std::vector<zmq_poller_event_t> events{1}; std::vector<zmq_poller_event_t> events{1};
int i = 0; int i = 0;
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, &i)); ASSERT_NO_THROW (poller.add (s.server, ZMQ_POLLIN, &i));
ASSERT_EQ(1, poller.wait_all(events, std::chrono::milliseconds{-1})); ASSERT_EQ (1, poller.wait_all (events, std::chrono::milliseconds{-1}));
ASSERT_EQ(s.server, events[0].socket); ASSERT_EQ (s.server, events[0].socket);
ASSERT_EQ(&i, events[0].user_data); ASSERT_EQ (&i, events[0].user_data);
} }
TEST(poller, add_invalid_socket_throws) TEST (poller, add_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::poller_t<> poller; zmq::poller_t<> poller;
zmq::socket_t a {context, zmq::socket_type::router}; zmq::socket_t a{context, zmq::socket_type::router};
zmq::socket_t b {std::move (a)}; zmq::socket_t b{std::move (a)};
ASSERT_THROW (poller.add (a, ZMQ_POLLIN, nullptr), ASSERT_THROW (poller.add (a, ZMQ_POLLIN, nullptr), zmq::error_t);
zmq::error_t);
} }
TEST(poller, remove_invalid_socket_throws) TEST (poller, remove_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::router}; zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, nullptr));
std::vector<zmq::socket_t> sockets; std::vector<zmq::socket_t> sockets;
@ -158,96 +160,97 @@ TEST(poller, remove_invalid_socket_throws)
ASSERT_NO_THROW (poller.remove (sockets[0])); ASSERT_NO_THROW (poller.remove (sockets[0]));
} }
TEST(poller, modify_empty_throws) TEST (poller, modify_empty_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::push}; zmq::socket_t socket{context, zmq::socket_type::push};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_THROW (poller.modify (socket, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (poller.modify (socket, ZMQ_POLLIN), zmq::error_t);
} }
TEST(poller, modify_invalid_socket_throws) TEST (poller, modify_invalid_socket_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b {std::move (a)}; zmq::socket_t b{std::move (a)};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_THROW (poller.modify (a, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (poller.modify (a, ZMQ_POLLIN), zmq::error_t);
} }
TEST(poller, modify_not_added_throws) TEST (poller, modify_not_added_throws)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::socket_t b {context, zmq::socket_type::push}; zmq::socket_t b{context, zmq::socket_type::push};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, nullptr));
ASSERT_THROW (poller.modify (b, ZMQ_POLLIN), zmq::error_t); ASSERT_THROW (poller.modify (b, ZMQ_POLLIN), zmq::error_t);
} }
TEST(poller, modify_simple) TEST (poller, modify_simple)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push}; zmq::socket_t a{context, zmq::socket_type::push};
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, nullptr));
ASSERT_NO_THROW (poller.modify (a, ZMQ_POLLIN|ZMQ_POLLOUT)); ASSERT_NO_THROW (poller.modify (a, ZMQ_POLLIN | ZMQ_POLLOUT));
} }
TEST(poller, poll_client_server) TEST (poller, poll_client_server)
{ {
// Setup server and client // Setup server and client
common_server_client_setup s; common_server_client_setup s;
// Setup poller // Setup poller
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, s.server)); ASSERT_NO_THROW (poller.add (s.server, ZMQ_POLLIN, s.server));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify events // wait for message and verify events
std::vector<zmq_poller_event_t> events(1); std::vector<zmq_poller_event_t> events (1);
ASSERT_EQ(1, poller.wait_all(events, std::chrono::milliseconds{500})); ASSERT_EQ (1, poller.wait_all (events, std::chrono::milliseconds{500}));
ASSERT_EQ(ZMQ_POLLIN, events[0].events); ASSERT_EQ (ZMQ_POLLIN, events[0].events);
// Modify server socket with pollout flag // Modify server socket with pollout flag
ASSERT_NO_THROW(poller.modify(s.server, ZMQ_POLLIN | ZMQ_POLLOUT)); ASSERT_NO_THROW (poller.modify (s.server, ZMQ_POLLIN | ZMQ_POLLOUT));
ASSERT_EQ(1, poller.wait_all(events, std::chrono::milliseconds{500})); ASSERT_EQ (1, poller.wait_all (events, std::chrono::milliseconds{500}));
ASSERT_EQ(ZMQ_POLLIN | ZMQ_POLLOUT, events[0].events); ASSERT_EQ (ZMQ_POLLIN | ZMQ_POLLOUT, events[0].events);
} }
TEST(poller, wait_one_return) TEST (poller, wait_one_return)
{ {
// Setup server and client // Setup server and client
common_server_client_setup s; common_server_client_setup s;
// Setup poller // Setup poller
zmq::poller_t<> poller; zmq::poller_t<> poller;
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (poller.add (s.server, ZMQ_POLLIN, nullptr));
// client sends message // client sends message
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
// wait for message and verify events // wait for message and verify events
std::vector<zmq_poller_event_t> events(1); std::vector<zmq_poller_event_t> events (1);
ASSERT_EQ(1, poller.wait_all(events, std::chrono::milliseconds{500})); ASSERT_EQ (1, poller.wait_all (events, std::chrono::milliseconds{500}));
} }
TEST(poller, wait_on_move_constructed_poller) TEST (poller, wait_on_move_constructed_poller)
{ {
common_server_client_setup s; common_server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::poller_t<> a; zmq::poller_t<> a;
ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, nullptr));
zmq::poller_t<> b {std::move (a)}; zmq::poller_t<> b{std::move (a)};
std::vector<zmq_poller_event_t> events(1); std::vector<zmq_poller_event_t> events (1);
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
ASSERT_THROW(a.wait_all (events, std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (a.wait_all (events, std::chrono::milliseconds{10}),
ASSERT_EQ (1, b.wait_all (events, std::chrono::milliseconds {-1})); zmq::error_t);
ASSERT_EQ (1, b.wait_all (events, std::chrono::milliseconds{-1}));
} }
TEST(poller, wait_on_move_assigned_poller) TEST (poller, wait_on_move_assigned_poller)
{ {
common_server_client_setup s; common_server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
@ -256,12 +259,13 @@ TEST(poller, wait_on_move_assigned_poller)
zmq::poller_t<> b; zmq::poller_t<> b;
b = {std::move (a)}; b = {std::move (a)};
/// \todo the actual error code should be checked /// \todo the actual error code should be checked
std::vector<zmq_poller_event_t> events(1); std::vector<zmq_poller_event_t> events (1);
ASSERT_THROW(a.wait_all (events, std::chrono::milliseconds{10}), zmq::error_t); ASSERT_THROW (a.wait_all (events, std::chrono::milliseconds{10}),
ASSERT_EQ (1, b.wait_all (events, std::chrono::milliseconds {-1})); zmq::error_t);
ASSERT_EQ (1, b.wait_all (events, std::chrono::milliseconds{-1}));
} }
TEST(poller, remove_from_handler) TEST (poller, remove_from_handler)
{ {
constexpr auto ITER_NO = 10; constexpr auto ITER_NO = 10;
@ -273,22 +277,24 @@ TEST(poller, remove_from_handler)
// Setup poller // Setup poller
zmq::poller_t<> poller; zmq::poller_t<> poller;
for (auto i = 0; i < ITER_NO; ++i) { for (auto i = 0; i < ITER_NO; ++i) {
ASSERT_NO_THROW(poller.add(setup_list[i].server, ZMQ_POLLIN, nullptr)); ASSERT_NO_THROW (
poller.add (setup_list[i].server, ZMQ_POLLIN, nullptr));
} }
// Clients send messages // Clients send messages
for (auto & s : setup_list) { for (auto &s : setup_list) {
ASSERT_NO_THROW(s.client.send("Hi")); ASSERT_NO_THROW (s.client.send ("Hi"));
} }
// Wait for all servers to receive a message // Wait for all servers to receive a message
for (auto & s : setup_list) { for (auto &s : setup_list) {
zmq::pollitem_t items [] = { { s.server, 0, ZMQ_POLLIN, 0 } }; zmq::pollitem_t items[] = {{s.server, 0, ZMQ_POLLIN, 0}};
zmq::poll (&items [0], 1); zmq::poll (&items[0], 1);
} }
// Fire all handlers in one wait // Fire all handlers in one wait
std::vector<zmq_poller_event_t> events(ITER_NO); std::vector<zmq_poller_event_t> events (ITER_NO);
ASSERT_EQ(ITER_NO, poller.wait_all (events, std::chrono::milliseconds{-1})); ASSERT_EQ (ITER_NO,
poller.wait_all (events, std::chrono::milliseconds{-1}));
} }
#endif #endif

View File

@ -1,16 +1,16 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <zmq.hpp> #include <zmq.hpp>
TEST(socket, create_destroy) TEST (socket, create_destroy)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket(context, ZMQ_ROUTER); zmq::socket_t socket (context, ZMQ_ROUTER);
} }
#ifdef ZMQ_CPP11 #ifdef ZMQ_CPP11
TEST(socket, create_by_enum_destroy) TEST (socket, create_by_enum_destroy)
{ {
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket(context, zmq::socket_type::router); zmq::socket_t socket (context, zmq::socket_type::router);
} }
#endif #endif

1777
zmq.hpp

File diff suppressed because it is too large Load Diff