Skip to content

Commit 58ffef7

Browse files
committed
Problem: no tests for monitor_t::abort
Solution: add a test and fix implementation of monitor_t::abort to make it usable
1 parent ec63fb3 commit 58ffef7

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

tests/monitor.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <gmock/gmock.h>
33
#include <zmq.hpp>
44

5+
#ifdef ZMQ_CPP11
6+
#include <thread>
7+
#endif
8+
59
class mock_monitor_t : public zmq::monitor_t
610
{
711
public:
@@ -40,3 +44,39 @@ TEST(monitor, init_check)
4044
while (monitor.check_event(100)) {
4145
}
4246
}
47+
48+
#ifdef ZMQ_CPP11
49+
TEST(monitor, init_abort)
50+
{
51+
zmq::context_t ctx;
52+
zmq::socket_t bind_socket(ctx, zmq::socket_type::dealer);
53+
54+
bind_socket.bind("tcp://127.0.0.1:*");
55+
char endpoint[255];
56+
size_t endpoint_len = sizeof(endpoint);
57+
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
58+
59+
zmq::socket_t connect_socket(ctx, zmq::socket_type::dealer);
60+
61+
mock_monitor_t monitor;
62+
monitor.init(connect_socket, "inproc://foo");
63+
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
64+
.Times(testing::AtLeast(1));
65+
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
66+
.Times(testing::AtLeast(1));
67+
68+
auto thread = std::thread([&monitor] {
69+
while (monitor.check_event(-1)) {
70+
}
71+
});
72+
73+
connect_socket.connect(endpoint);
74+
std::this_thread::sleep_for(std::chrono::milliseconds(250));
75+
// TODO instead of sleeping an arbitrary amount of time, we should better
76+
// wait until the expectations have met. How can this be done with
77+
// googlemock?
78+
79+
monitor.abort();
80+
thread.join();
81+
}
82+
#endif

zmq.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ class monitor_t
849849
#ifdef ZMQ_EVENT_MONITOR_STOPPED
850850
if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
851851
zmq_msg_close(&eventMsg);
852-
return true;
852+
return false;
853853
}
854854

855855
#endif
@@ -923,11 +923,7 @@ class monitor_t
923923
if (socketPtr)
924924
zmq_socket_monitor(socketPtr, NULL, 0);
925925

926-
if (monitor_socket)
927-
zmq_close(monitor_socket);
928-
929926
socketPtr = NULL;
930-
monitor_socket = NULL;
931927
}
932928
#endif
933929
virtual void on_monitor_started() {}

0 commit comments

Comments
 (0)