main.cpp
9.74 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (c) TrueBrain
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <TrueMQTT.h>
#include <catch2/catch_test_macros.hpp>
#include <mutex>
#include <thread>
#include <iostream>
TEST_CASE("regression", "[regression]")
{
// last_message / logger_mutex needs to outlive client1, as it is captured in a lambda.
std::string last_message = {};
std::mutex logger_mutex;
TrueMQTT::Client client1("localhost", 1883, "client-1");
TrueMQTT::Client client2("localhost", 1883, "client-2");
client1.setLogger(
TrueMQTT::Client::LogLevel::INFO,
[&last_message, &logger_mutex](TrueMQTT::Client::LogLevel level, const std::string_view message)
{
std::scoped_lock lock(logger_mutex);
last_message = message;
});
client2.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10);
uint8_t connected = 0x0;
client1.setStateChangeCallback(
[&connected](TrueMQTT::Client::State state)
{
if (state == TrueMQTT::Client::State::CONNECTED)
{
connected |= 0x1;
}
});
client2.setStateChangeCallback(
[&connected](TrueMQTT::Client::State state)
{
if (state == TrueMQTT::Client::State::CONNECTED)
{
connected |= 0x2;
}
});
// TC: Cannot publish/subscribe/unsubscribe when disconnected
{
CHECK(client1.publish("regression/topic1", "Hello World!", false) == false);
CHECK(last_message == "Cannot publish when disconnected");
client1.subscribe("regression/topic1", [](const std::string_view topic, const std::string_view message) { /* empty */ });
CHECK(last_message == "Cannot subscribe when disconnected");
client1.unsubscribe("regression/topic1");
CHECK(last_message == "Cannot unsubscribe when disconnected");
}
uint8_t received = 0;
uint8_t received2 = 0;
// TC: Connect the subscribing client first and instantly make a subscription; subscription should be created after connect.
{
client1.connect();
CHECK(last_message == "Connecting to localhost:1883");
client1.subscribe(
"regression/topic1",
[&received](const std::string_view topic, const std::string_view payload)
{
CHECK(std::string(topic) == "regression/topic1");
received++;
});
auto start = std::chrono::steady_clock::now();
while ((connected & 0x1) == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(5))
{
FAIL("Timeout waiting for client to connect");
}
}
// Give some time for the subscription to actually be made.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// TC: Connect the publishing client next and instantly publish a message; message should be published after connect.
{
client2.connect();
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
auto start = std::chrono::steady_clock::now();
while ((connected & 0x2) == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(1))
{
FAIL("Timeout waiting for client to connect");
}
}
}
// TC: Publish before connect happened and check message arrives after connection is made
{
// Wait for the pending publish to arrive.
auto start = std::chrono::steady_clock::now();
while (received != 1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(1))
{
FAIL("Timeout waiting for message to arrive");
}
}
received = 0;
}
// TC: Cannot connect when already connected
{
client1.connect();
CHECK(last_message == "Can't connect when already connecting / connected");
}
// TC: Publish to delayed subscription must work
{
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
auto start = std::chrono::steady_clock::now();
while (received != 4)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(1))
{
FAIL("Timeout waiting for message to arrive");
}
}
received = 0;
}
// TC: Subscribe to new topic and publish on it
{
client1.subscribe(
"regression/topic2",
[&received2](const std::string_view topic, const std::string_view payload)
{
CHECK(std::string(topic) == "regression/topic2");
received2++;
});
// Give some time for the subscription to actually be made.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(client2.publish("regression/topic2", "Hello World!", false) == true);
// Wait for the pending publish to arrive.
auto start = std::chrono::steady_clock::now();
while (received2 != 1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(1))
{
FAIL("Timeout waiting for message to arrive");
}
}
received2 = 0;
}
// TC: Unsubscribe and check a publish to the topic no longer arrives
{
client1.unsubscribe("regression/topic1");
client1.unsubscribe("regression/topic2");
CHECK(client2.publish("regression/topic1", "Hello World!", false) == true);
CHECK(client2.publish("regression/topic2", "Hello World!", false) == true);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
REQUIRE(received == 0);
REQUIRE(received2 == 0);
}
// TC: Check wildcard subscriptions
{
client1.subscribe(
"regression/topic3/+",
[&received](const std::string_view topic, const std::string_view payload)
{
CHECK(std::string(topic) == "regression/topic3/1");
received++;
});
client1.subscribe(
"regression/#",
[&received2](const std::string_view topic, const std::string_view payload)
{
if (received2 == 0)
{
CHECK(std::string(topic) == "regression/topic3/1");
}
else
{
CHECK(std::string(topic) == "regression/topic4/2");
}
received2++;
});
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(client2.publish("regression/topic3/1", "Hello World!", false) == true);
CHECK(client2.publish("regression/topic4/2", "Hello World!", false) == true);
auto start = std::chrono::steady_clock::now();
while (received != 1 || received2 != 2)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::seconds(1))
{
FAIL("Timeout waiting for message to arrive");
}
}
received = 0;
received2 = 0;
}
// TC: Cannot unsubscribe from topic not subscribed to
{
client1.unsubscribe("regression/topic1");
CHECK(last_message == "Cannot unsubscribe from topic 'regression/topic1' because we are not subscribed to it");
}
// TC: Validate disconnect is seen
{
client1.disconnect();
CHECK(last_message == "Disconnecting from broker");
client2.disconnect();
}
// TC: Cannot disconnect when not connected
{
client1.disconnect();
CHECK(last_message == "Can't disconnect when already disconnected");
}
// TC: Connect to a non-existing broker
{
TrueMQTT::Client client3("localhost", 1884, "client-3");
client3.setLogger(
TrueMQTT::Client::LogLevel::INFO,
[&last_message, &logger_mutex](TrueMQTT::Client::LogLevel level, const std::string_view message)
{
std::scoped_lock lock(logger_mutex);
last_message = message;
});
client3.setStateChangeCallback(
[&connected](TrueMQTT::Client::State state)
{
if (state == TrueMQTT::Client::State::CONNECTED)
{
connected |= 0x4;
}
});
client3.connect();
CHECK(last_message == "Connecting to localhost:1884");
bool failed = false;
auto start = std::chrono::steady_clock::now();
while ((connected & 0x4) == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::steady_clock::now() - start > std::chrono::milliseconds(100))
{
failed = true;
break;
}
}
REQUIRE(failed == true);
client3.disconnect();
CHECK(last_message == "Disconnecting from broker");
}
}