Skip to content

Commit

Permalink
unit test for active_poller and poller_t support for file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanschim committed Jul 19, 2023
1 parent b5ce0ad commit dd67d56
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/active_poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#include <array>
#include <memory>
#include <cstring>

#if !defined(_WIN32)
#include <unistd.h>
#endif // !_WIN32

TEST_CASE("create destroy", "[active_poller]")
{
Expand Down Expand Up @@ -86,6 +91,85 @@ TEST_CASE("add handler", "[active_poller]")
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
}

TEST_CASE("add fd handler", "[active_poller]")
{
int fd = 1;
zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(fd, zmq::event_flags::pollin, no_op_handler));
}

TEST_CASE("remove fd handler", "[active_poller]")
{
int fd = 1;
zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(fd, zmq::event_flags::pollin, no_op_handler));
CHECK_NOTHROW(
active_poller.remove(fd));
CHECK_THROWS_ZMQ_ERROR(EINVAL, active_poller.remove(100));
}

#if !defined(_WIN32)
// On Windows, these functions can only be used with WinSock sockets.

TEST_CASE("mixed socket and fd handlers", "[active_poller]")
{
int pipefd[2];
::pipe(pipefd);

zmq::context_t context;
constexpr char inprocSocketAddress[] = "inproc://mixed-handlers";
zmq::socket_t socket_rcv{context, zmq::socket_type::pair};
zmq::socket_t socket_snd{context, zmq::socket_type::pair};
socket_rcv.bind(inprocSocketAddress);
socket_snd.connect(inprocSocketAddress);

unsigned eventsFd = 0;
unsigned eventsSocket = 0;

constexpr char messageText[] = "message";
constexpr size_t messageSize = sizeof(messageText);

zmq::active_poller_t active_poller;
CHECK_NOTHROW(
active_poller.add(pipefd[0], zmq::event_flags::pollin, [&](zmq::event_flags flags) {
if (flags == zmq::event_flags::pollin)
{
char buffer[256];
CHECK(messageSize == ::read(pipefd[0], buffer, messageSize));
CHECK(0 == std::strcmp(buffer, messageText));
++eventsFd;
}
}));
CHECK_NOTHROW(
active_poller.add(socket_rcv, zmq::event_flags::pollin, [&](zmq::event_flags flags) {
if (flags == zmq::event_flags::pollin)
{
zmq::message_t msg;
CHECK(socket_rcv.recv(msg, zmq::recv_flags::dontwait).has_value());
CHECK(messageSize == msg.size());
CHECK(0 == std::strcmp(messageText, msg.data<const char>()));
++eventsSocket;
}
}));

// send/rcv socket pair
zmq::message_t msg{messageText, messageSize};
socket_snd.send(msg, zmq::send_flags::dontwait);
CHECK(1 == active_poller.wait(std::chrono::milliseconds{100}));
CHECK(0 == eventsFd);
CHECK(1 == eventsSocket);

// send/rcv pipe
::write(pipefd[1], messageText, messageSize);
CHECK(1 == active_poller.wait(std::chrono::milliseconds{100}));
CHECK(1 == eventsFd);
CHECK(1 == eventsSocket);
}

#endif // !_WIN32

TEST_CASE("add null handler fails", "[active_poller]")
{
zmq::context_t context;
Expand Down
1 change: 1 addition & 0 deletions zmq_addon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <limits>
#include <functional>
#include <unordered_map>
#include <variant>
#endif

namespace zmq
Expand Down

0 comments on commit dd67d56

Please sign in to comment.