mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-13 10:52:57 +01:00
Merge pull request #249 from sigiesec/add-monitor-tests
Add first tests for monitor_t and fix monitor_t::abort
This commit is contained in:
commit
97d2cb2498
@ -23,11 +23,13 @@ add_executable(
|
||||
poller.cpp
|
||||
active_poller.cpp
|
||||
multipart.cpp
|
||||
monitor.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
unit_tests
|
||||
PRIVATE gtest_main
|
||||
PRIVATE gtest
|
||||
PRIVATE gmock_main
|
||||
PRIVATE cppzmq
|
||||
)
|
||||
|
||||
|
82
tests/monitor.cpp
Normal file
82
tests/monitor.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <zmq.hpp>
|
||||
|
||||
#ifdef ZMQ_CPP11
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
class mock_monitor_t : public zmq::monitor_t
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD2(on_event_connect_delayed, void(const zmq_event_t &, const char *));
|
||||
MOCK_METHOD2(on_event_connected, void(const zmq_event_t &, const char *));
|
||||
};
|
||||
|
||||
TEST(monitor, create_destroy)
|
||||
{
|
||||
zmq::monitor_t monitor;
|
||||
}
|
||||
|
||||
TEST(monitor, init_check)
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t bind_socket(ctx, ZMQ_DEALER);
|
||||
|
||||
bind_socket.bind("tcp://127.0.0.1:*");
|
||||
char endpoint[255];
|
||||
size_t endpoint_len = sizeof(endpoint);
|
||||
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
|
||||
|
||||
zmq::socket_t connect_socket(ctx, ZMQ_DEALER);
|
||||
|
||||
mock_monitor_t monitor;
|
||||
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
||||
.Times(testing::AtLeast(1));
|
||||
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
||||
.Times(testing::AtLeast(1));
|
||||
|
||||
monitor.init(connect_socket, "inproc://foo");
|
||||
|
||||
ASSERT_FALSE(monitor.check_event(0));
|
||||
connect_socket.connect(endpoint);
|
||||
|
||||
while (monitor.check_event(100)) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ZMQ_CPP11
|
||||
TEST(monitor, init_abort)
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t bind_socket(ctx, zmq::socket_type::dealer);
|
||||
|
||||
bind_socket.bind("tcp://127.0.0.1:*");
|
||||
char endpoint[255];
|
||||
size_t endpoint_len = sizeof(endpoint);
|
||||
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
|
||||
|
||||
zmq::socket_t connect_socket(ctx, zmq::socket_type::dealer);
|
||||
|
||||
mock_monitor_t monitor;
|
||||
monitor.init(connect_socket, "inproc://foo");
|
||||
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
||||
.Times(testing::AtLeast(1));
|
||||
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
||||
.Times(testing::AtLeast(1));
|
||||
|
||||
auto thread = std::thread([&monitor] {
|
||||
while (monitor.check_event(-1)) {
|
||||
}
|
||||
});
|
||||
|
||||
connect_socket.connect(endpoint);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
// TODO instead of sleeping an arbitrary amount of time, we should better
|
||||
// wait until the expectations have met. How can this be done with
|
||||
// googlemock?
|
||||
|
||||
monitor.abort();
|
||||
thread.join();
|
||||
}
|
||||
#endif
|
6
zmq.hpp
6
zmq.hpp
@ -849,7 +849,7 @@ class monitor_t
|
||||
#ifdef ZMQ_EVENT_MONITOR_STOPPED
|
||||
if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
|
||||
zmq_msg_close(&eventMsg);
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -923,11 +923,7 @@ class monitor_t
|
||||
if (socketPtr)
|
||||
zmq_socket_monitor(socketPtr, NULL, 0);
|
||||
|
||||
if (monitor_socket)
|
||||
zmq_close(monitor_socket);
|
||||
|
||||
socketPtr = NULL;
|
||||
monitor_socket = NULL;
|
||||
}
|
||||
#endif
|
||||
virtual void on_monitor_started() {}
|
||||
|
Loading…
Reference in New Issue
Block a user