|
6 | 6 |
|
7 | 7 | #include <array>
|
8 | 8 | #include <memory>
|
| 9 | +#include <cstring> |
| 10 | + |
| 11 | +#if !defined(_WIN32) |
| 12 | + #include <unistd.h> |
| 13 | +#endif // !_WIN32 |
9 | 14 |
|
10 | 15 | TEST_CASE("create destroy", "[active_poller]")
|
11 | 16 | {
|
@@ -86,6 +91,85 @@ TEST_CASE("add handler", "[active_poller]")
|
86 | 91 | active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
|
87 | 92 | }
|
88 | 93 |
|
| 94 | +TEST_CASE("add fd handler", "[active_poller]") |
| 95 | +{ |
| 96 | + int fd = 1; |
| 97 | + zmq::active_poller_t active_poller; |
| 98 | + CHECK_NOTHROW( |
| 99 | + active_poller.add(fd, zmq::event_flags::pollin, no_op_handler)); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_CASE("remove fd handler", "[active_poller]") |
| 103 | +{ |
| 104 | + int fd = 1; |
| 105 | + zmq::active_poller_t active_poller; |
| 106 | + CHECK_NOTHROW( |
| 107 | + active_poller.add(fd, zmq::event_flags::pollin, no_op_handler)); |
| 108 | + CHECK_NOTHROW( |
| 109 | + active_poller.remove(fd)); |
| 110 | + CHECK_THROWS_ZMQ_ERROR(EINVAL, active_poller.remove(100)); |
| 111 | +} |
| 112 | + |
| 113 | +#if !defined(_WIN32) |
| 114 | +// On Windows, these functions can only be used with WinSock sockets. |
| 115 | + |
| 116 | +TEST_CASE("mixed socket and fd handlers", "[active_poller]") |
| 117 | +{ |
| 118 | + int pipefd[2]; |
| 119 | + ::pipe(pipefd); |
| 120 | + |
| 121 | + zmq::context_t context; |
| 122 | + constexpr char inprocSocketAddress[] = "inproc://mixed-handlers"; |
| 123 | + zmq::socket_t socket_rcv{context, zmq::socket_type::pair}; |
| 124 | + zmq::socket_t socket_snd{context, zmq::socket_type::pair}; |
| 125 | + socket_rcv.bind(inprocSocketAddress); |
| 126 | + socket_snd.connect(inprocSocketAddress); |
| 127 | + |
| 128 | + unsigned eventsFd = 0; |
| 129 | + unsigned eventsSocket = 0; |
| 130 | + |
| 131 | + constexpr char messageText[] = "message"; |
| 132 | + constexpr size_t messageSize = sizeof(messageText); |
| 133 | + |
| 134 | + zmq::active_poller_t active_poller; |
| 135 | + CHECK_NOTHROW( |
| 136 | + active_poller.add(pipefd[0], zmq::event_flags::pollin, [&](zmq::event_flags flags) { |
| 137 | + if (flags == zmq::event_flags::pollin) |
| 138 | + { |
| 139 | + char buffer[256]; |
| 140 | + CHECK(messageSize == ::read(pipefd[0], buffer, messageSize)); |
| 141 | + CHECK(0 == std::strcmp(buffer, messageText)); |
| 142 | + ++eventsFd; |
| 143 | + } |
| 144 | + })); |
| 145 | + CHECK_NOTHROW( |
| 146 | + active_poller.add(socket_rcv, zmq::event_flags::pollin, [&](zmq::event_flags flags) { |
| 147 | + if (flags == zmq::event_flags::pollin) |
| 148 | + { |
| 149 | + zmq::message_t msg; |
| 150 | + CHECK(socket_rcv.recv(msg, zmq::recv_flags::dontwait).has_value()); |
| 151 | + CHECK(messageSize == msg.size()); |
| 152 | + CHECK(0 == std::strcmp(messageText, msg.data<const char>())); |
| 153 | + ++eventsSocket; |
| 154 | + } |
| 155 | + })); |
| 156 | + |
| 157 | + // send/rcv socket pair |
| 158 | + zmq::message_t msg{messageText, messageSize}; |
| 159 | + socket_snd.send(msg, zmq::send_flags::dontwait); |
| 160 | + CHECK(1 == active_poller.wait(std::chrono::milliseconds{100})); |
| 161 | + CHECK(0 == eventsFd); |
| 162 | + CHECK(1 == eventsSocket); |
| 163 | + |
| 164 | + // send/rcv pipe |
| 165 | + ::write(pipefd[1], messageText, messageSize); |
| 166 | + CHECK(1 == active_poller.wait(std::chrono::milliseconds{100})); |
| 167 | + CHECK(1 == eventsFd); |
| 168 | + CHECK(1 == eventsSocket); |
| 169 | +} |
| 170 | + |
| 171 | +#endif // !_WIN32 |
| 172 | + |
89 | 173 | TEST_CASE("add null handler fails", "[active_poller]")
|
90 | 174 | {
|
91 | 175 | zmq::context_t context;
|
|
0 commit comments