Skip to content

Commit 73f171a

Browse files
authored
Merge pull request #255 from kurdybacha/issue_fix
Problem: #209 and monitor_t tests not event driven
2 parents 837c0c9 + 57454df commit 73f171a

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

cppzmqConfig.cmake.in

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ if(NOT ZeroMQ_FOUND)
3030
endif()
3131

3232
if(NOT TARGET @PROJECT_NAME@)
33-
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
34-
35-
get_target_property(@PROJECT_NAME@_INCLUDE_DIR cppzmq INTERFACE_INCLUDE_DIRECTORIES)
36-
get_target_property(@PROJECT_NAME@_LIBRARY libzmq LOCATION)
37-
get_target_property(@PROJECT_NAME@_STATIC_LIBRARY libzmq-static LOCATION)
33+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
34+
get_target_property(@PROJECT_NAME@_INCLUDE_DIR cppzmq INTERFACE_INCLUDE_DIRECTORIES)
3835
endif()
36+

tests/monitor.cpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <gtest/gtest.h>
2-
#include <gmock/gmock.h>
3-
#include <zmq.hpp>
1+
#include "testutil.hpp"
42

3+
#include <gmock/gmock.h>
54
#ifdef ZMQ_CPP11
65
#include <thread>
6+
#include <mutex>
7+
#include <condition_variable>
78
#endif
89

910
class mock_monitor_t : public zmq::monitor_t
@@ -18,63 +19,65 @@ TEST(monitor, create_destroy)
1819
zmq::monitor_t monitor;
1920
}
2021

22+
#if defined(ZMQ_CPP11)
2123
TEST(monitor, init_check)
2224
{
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);
25+
common_server_client_setup s{false};
26+
mock_monitor_t monitor;
3027

31-
zmq::socket_t connect_socket(ctx, ZMQ_DEALER);
28+
const int expected_event_count = 2;
29+
int event_count = 0;
30+
auto count_event = [&event_count](const zmq_event_t &, const char *) {
31+
++event_count;
32+
};
3233

33-
mock_monitor_t monitor;
3434
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
35-
.Times(testing::AtLeast(1));
35+
.Times(1)
36+
.WillOnce(testing::Invoke(count_event));
3637
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
37-
.Times(testing::AtLeast(1));
38+
.Times(1)
39+
.WillOnce(testing::Invoke(count_event));
3840

39-
monitor.init(connect_socket, "inproc://foo");
41+
monitor.init(s.client, "inproc://foo");
4042

4143
ASSERT_FALSE(monitor.check_event(0));
42-
connect_socket.connect(endpoint);
44+
s.init();
4345

44-
while (monitor.check_event(100)) {
46+
while (monitor.check_event(100) && event_count < expected_event_count) {
4547
}
4648
}
4749

48-
#ifdef ZMQ_CPP11
4950
TEST(monitor, init_abort)
5051
{
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);
52+
common_server_client_setup s(false);
53+
mock_monitor_t monitor;
54+
monitor.init(s.client, "inproc://foo");
5855

59-
zmq::socket_t connect_socket(ctx, zmq::socket_type::dealer);
56+
std::mutex mutex;
57+
std::condition_variable cond_var;
58+
bool done{false};
6059

61-
mock_monitor_t monitor;
62-
monitor.init(connect_socket, "inproc://foo");
6360
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
64-
.Times(testing::AtLeast(1));
61+
.Times(1);
6562
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
66-
.Times(testing::AtLeast(1));
63+
.Times(1)
64+
.WillOnce(testing::Invoke([&](const zmq_event_t &, const char *) {
65+
std::lock_guard<std::mutex> lock(mutex);
66+
done = true;
67+
cond_var.notify_one();
68+
}));
6769

6870
auto thread = std::thread([&monitor] {
6971
while (monitor.check_event(-1)) {
7072
}
7173
});
7274

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?
75+
s.init();
76+
{
77+
std::unique_lock<std::mutex> lock(mutex);
78+
EXPECT_TRUE(cond_var.wait_for(lock, std::chrono::seconds(1),
79+
[&done] { return done; }));
80+
}
7881

7982
monitor.abort();
8083
thread.join();

tests/testutil.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <gtest/gtest.h>
44
#include <zmq.hpp>
55

6-
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
6+
#if defined(ZMQ_CPP11)
77
#include <array>
88

99
class loopback_ip4_binder
@@ -31,7 +31,11 @@ class loopback_ip4_binder
3131

3232
struct common_server_client_setup
3333
{
34-
common_server_client_setup() { init(); }
34+
common_server_client_setup(bool initialize = true)
35+
{
36+
if (initialize)
37+
init();
38+
}
3539

3640
void init()
3741
{
@@ -40,8 +44,8 @@ struct common_server_client_setup
4044
}
4145

4246
zmq::context_t context;
43-
zmq::socket_t server{context, zmq::socket_type::server};
44-
zmq::socket_t client{context, zmq::socket_type::client};
47+
zmq::socket_t server{context, zmq::socket_type::pair};
48+
zmq::socket_t client{context, zmq::socket_type::pair};
4549
std::string endpoint;
4650
};
4751
#endif

0 commit comments

Comments
 (0)