-
Notifications
You must be signed in to change notification settings - Fork 797
Expand file tree
/
Copy pathmonitor.cpp
More file actions
190 lines (161 loc) · 4.48 KB
/
monitor.cpp
File metadata and controls
190 lines (161 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "testutil.hpp"
#ifdef ZMQ_CPP11
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
class mock_monitor_t : public zmq::monitor_t
{
public:
void on_event_connect_delayed(const zmq_event_t &, const char *) ZMQ_OVERRIDE
{
++connect_delayed;
++total;
}
void on_event_connected(const zmq_event_t &, const char *) ZMQ_OVERRIDE
{
++connected;
++total;
}
int total{0};
int connect_delayed{0};
int connected{0};
};
#endif
TEST_CASE("monitor create destroy", "[monitor]")
{
zmq::monitor_t monitor;
}
#if defined(ZMQ_CPP11)
TEST_CASE("monitor move construct", "[monitor]")
{
zmq::context_t ctx;
zmq::socket_t sock(ctx, ZMQ_DEALER);
SECTION("move ctor empty") {
zmq::monitor_t monitor1;
zmq::monitor_t monitor2 = std::move(monitor1);
}
SECTION("move ctor init") {
zmq::monitor_t monitor1;
monitor1.init(sock, "inproc://monitor-client");
zmq::monitor_t monitor2 = std::move(monitor1);
}
}
TEST_CASE("monitor move assign", "[monitor]")
{
zmq::context_t ctx;
zmq::socket_t sock(ctx, ZMQ_DEALER);
SECTION("move assign empty") {
zmq::monitor_t monitor1;
zmq::monitor_t monitor2;
monitor1 = std::move(monitor2);
}
SECTION("move assign init") {
zmq::monitor_t monitor1;
monitor1.init(sock, "inproc://monitor-client");
zmq::monitor_t monitor2;
monitor2 = std::move(monitor1);
}
SECTION("move assign init both") {
zmq::monitor_t monitor1;
monitor1.init(sock, "inproc://monitor-client");
zmq::monitor_t monitor2;
zmq::socket_t sock2(ctx, ZMQ_DEALER);
monitor2.init(sock2, "inproc://monitor-client2");
monitor2 = std::move(monitor1);
}
}
TEST_CASE("monitor init check event count", "[monitor]")
{
common_server_client_setup s{false};
mock_monitor_t monitor;
const int expected_event_count = 2;
monitor.init(s.client, "inproc://foo");
CHECK_FALSE(monitor.check_event(0));
s.init();
while (monitor.check_event(100) && monitor.total < expected_event_count) {
}
CHECK(monitor.connect_delayed == 1);
CHECK(monitor.connected == 1);
CHECK(monitor.total == expected_event_count);
}
TEST_CASE("monitor init get event count", "[monitor]")
{
common_server_client_setup s{ false };
zmq::monitor_t monitor;
const int expected_event_count = 2;
monitor.init(s.client, "inproc://foo");
int total{ 0 };
int connect_delayed{ 0 };
int connected{ 0 };
auto lbd_count_event = [&](const zmq_event_t& event) {
switch (event.event)
{
case ZMQ_EVENT_CONNECT_DELAYED:
connect_delayed++;
total++;
break;
case ZMQ_EVENT_CONNECTED:
connected++;
total++;
break;
}
};
zmq_event_t eventMsg;
std::string address;
CHECK_FALSE(monitor.get_event(eventMsg, address, zmq::recv_flags::dontwait));
s.init();
while (total < expected_event_count)
{
if (!monitor.get_event(eventMsg, address))
continue;
lbd_count_event(eventMsg);
}
CHECK(connect_delayed == 1);
CHECK(connected == 1);
CHECK(total == expected_event_count);
}
TEST_CASE("monitor init abort", "[monitor]")
{
class mock_monitor : public mock_monitor_t
{
public:
mock_monitor(std::function<void(void)> handle_connected) :
handle_connected{std::move(handle_connected)}
{
}
void on_event_connected(const zmq_event_t &e, const char *m) ZMQ_OVERRIDE
{
mock_monitor_t::on_event_connected(e, m);
handle_connected();
}
std::function<void(void)> handle_connected;
};
common_server_client_setup s(false);
std::mutex mutex;
std::condition_variable cond_var;
bool done{false};
mock_monitor monitor([&]()
{
std::lock_guard<std::mutex> lock(mutex);
done = true;
cond_var.notify_one();
});
monitor.init(s.client, "inproc://foo");
auto thread = std::thread([&monitor]
{
while (monitor.check_event(-1)) {
}
});
s.init();
{
std::unique_lock<std::mutex> lock(mutex);
CHECK(cond_var.wait_for(lock, std::chrono::seconds(1),
[&done] { return done; }));
}
CHECK(monitor.connect_delayed == 1);
CHECK(monitor.connected == 1);
monitor.abort();
thread.join();
}
#endif