1
- #include < gtest/gtest.h>
2
- #include < gmock/gmock.h>
3
- #include < zmq.hpp>
1
+ #include " testutil.hpp"
4
2
3
+ #include < gmock/gmock.h>
5
4
#ifdef ZMQ_CPP11
6
5
#include < thread>
6
+ #include < mutex>
7
+ #include < condition_variable>
7
8
#endif
8
9
9
10
class mock_monitor_t : public zmq ::monitor_t
@@ -18,63 +19,65 @@ TEST(monitor, create_destroy)
18
19
zmq::monitor_t monitor;
19
20
}
20
21
22
+ #if defined(ZMQ_CPP11)
21
23
TEST (monitor, init_check)
22
24
{
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;
30
27
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
+ };
32
33
33
- mock_monitor_t monitor;
34
34
EXPECT_CALL (monitor, on_event_connect_delayed (testing::_, testing::_))
35
- .Times (testing::AtLeast (1 ));
35
+ .Times (1 )
36
+ .WillOnce (testing::Invoke (count_event));
36
37
EXPECT_CALL (monitor, on_event_connected (testing::_, testing::_))
37
- .Times (testing::AtLeast (1 ));
38
+ .Times (1 )
39
+ .WillOnce (testing::Invoke (count_event));
38
40
39
- monitor.init (connect_socket , " inproc://foo" );
41
+ monitor.init (s. client , " inproc://foo" );
40
42
41
43
ASSERT_FALSE (monitor.check_event (0 ));
42
- connect_socket. connect (endpoint );
44
+ s. init ( );
43
45
44
- while (monitor.check_event (100 )) {
46
+ while (monitor.check_event (100 ) && event_count < expected_event_count ) {
45
47
}
46
48
}
47
49
48
- #ifdef ZMQ_CPP11
49
50
TEST (monitor, init_abort)
50
51
{
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" );
58
55
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 };
60
59
61
- mock_monitor_t monitor;
62
- monitor.init (connect_socket, " inproc://foo" );
63
60
EXPECT_CALL (monitor, on_event_connect_delayed (testing::_, testing::_))
64
- .Times (testing::AtLeast ( 1 ) );
61
+ .Times (1 );
65
62
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
+ }));
67
69
68
70
auto thread = std::thread ([&monitor] {
69
71
while (monitor.check_event (-1 )) {
70
72
}
71
73
});
72
74
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
+ }
78
81
79
82
monitor.abort ();
80
83
thread.join ();
0 commit comments