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
910class 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)
2123TEST (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
4950TEST (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 ();
0 commit comments