Skip to content

Commit 97d2cb2

Browse files
authored
Merge pull request #249 from sigiesec/add-monitor-tests
Add first tests for monitor_t and fix monitor_t::abort
2 parents e0b5629 + 58ffef7 commit 97d2cb2

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ add_executable(
2323
poller.cpp
2424
active_poller.cpp
2525
multipart.cpp
26+
monitor.cpp
2627
)
2728

2829
target_link_libraries(
2930
unit_tests
30-
PRIVATE gtest_main
31+
PRIVATE gtest
32+
PRIVATE gmock_main
3133
PRIVATE cppzmq
3234
)
3335

tests/monitor.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <gtest/gtest.h>
2+
#include <gmock/gmock.h>
3+
#include <zmq.hpp>
4+
5+
#ifdef ZMQ_CPP11
6+
#include <thread>
7+
#endif
8+
9+
class mock_monitor_t : public zmq::monitor_t
10+
{
11+
public:
12+
MOCK_METHOD2(on_event_connect_delayed, void(const zmq_event_t &, const char *));
13+
MOCK_METHOD2(on_event_connected, void(const zmq_event_t &, const char *));
14+
};
15+
16+
TEST(monitor, create_destroy)
17+
{
18+
zmq::monitor_t monitor;
19+
}
20+
21+
TEST(monitor, init_check)
22+
{
23+
zmq::context_t ctx;
24+
zmq::socket_t bind_socket(ctx, ZMQ_DEALER);
25+
26+
bind_socket.bind("tcp://127.0.0.1:*");
27+
char endpoint[255];
28+
size_t endpoint_len = sizeof(endpoint);
29+
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
30+
31+
zmq::socket_t connect_socket(ctx, ZMQ_DEALER);
32+
33+
mock_monitor_t monitor;
34+
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
35+
.Times(testing::AtLeast(1));
36+
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
37+
.Times(testing::AtLeast(1));
38+
39+
monitor.init(connect_socket, "inproc://foo");
40+
41+
ASSERT_FALSE(monitor.check_event(0));
42+
connect_socket.connect(endpoint);
43+
44+
while (monitor.check_event(100)) {
45+
}
46+
}
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)