forked from eclipse-paho/paho.mqtt.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_token.cpp
358 lines (300 loc) · 10.4 KB
/
test_token.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// test_token.h
//
// Unit tests for the token class in the Paho MQTT C++ library.
//
/*******************************************************************************
* Copyright (c) 2016 Guilherme M. Ferreira <[email protected]>
* Copyright (c) 2016-2020 Frank Pagliughi <[email protected]>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Guilherme M. Ferreira
* - initial implementation and documentation
* Frank Pagliughi
* - additional tests. Made this test a friend of token.
* - updated for token::Type
* - Converted to use Catch2
*******************************************************************************/
#define UNIT_TESTS
#include <cstring>
#include "catch2/catch.hpp"
#include "mqtt/token.h"
#include "mock_async_client.h"
#include "mock_action_listener.h"
using namespace mqtt;
using milliseconds = std::chrono::milliseconds;
using steady_clock = std::chrono::steady_clock;
static mock_async_client cli;
static constexpr token::Type TYPE = token::Type::CONNECT;
// ----------------------------------------------------------------------
// Test user constructor (iasync_client)
// ----------------------------------------------------------------------
TEST_CASE("token user constructor client", "[token]")
{
mqtt::token tok{TYPE, cli};
REQUIRE(0 == tok.get_message_id());
REQUIRE(dynamic_cast<mqtt::iasync_client*>(&cli) == tok.get_client());
REQUIRE(nullptr == tok.get_user_context());
REQUIRE(nullptr == tok.get_action_callback());
REQUIRE(!tok.is_complete());
REQUIRE(nullptr == tok.get_topics());
}
// ----------------------------------------------------------------------
// Test user constructor (iasync_client, MQTTAsync_token)
// ----------------------------------------------------------------------
TEST_CASE("token user constructor client token", "[token]")
{
MQTTAsync_token id{2};
mqtt::token tok{TYPE, cli, id};
REQUIRE(id == tok.get_message_id());
REQUIRE(dynamic_cast<mqtt::iasync_client*>(&cli) == tok.get_client());
REQUIRE(nullptr == tok.get_user_context());
REQUIRE(nullptr == tok.get_action_callback());
REQUIRE(!tok.is_complete());
REQUIRE(nullptr == tok.get_topics());
}
// ----------------------------------------------------------------------
// Test user constructor (iasync_client, string)
// ----------------------------------------------------------------------
TEST_CASE("token user constructor client string", "[token]")
{
std::string topic{"topic"};
mqtt::token tok{TYPE, cli, topic};
REQUIRE(0 == tok.get_message_id());
REQUIRE(dynamic_cast<mqtt::iasync_client*>(&cli) == tok.get_client());
REQUIRE(nullptr == tok.get_user_context());
REQUIRE(nullptr == tok.get_action_callback());
REQUIRE(!tok.is_complete());
REQUIRE(nullptr != tok.get_topics());
REQUIRE(size_t(1) == tok.get_topics()->size());
REQUIRE(topic == (*tok.get_topics())[0]);
}
// ----------------------------------------------------------------------
// Test user constructor (iasync_client, vector<string>)
// ----------------------------------------------------------------------
TEST_CASE("token user constructor client vector", "[token]")
{
auto topics = string_collection::create({ "topic1", "topic2" });
mqtt::token tok{TYPE, cli, topics};
REQUIRE(0 == tok.get_message_id());
REQUIRE(dynamic_cast<mqtt::iasync_client*>(&cli) == tok.get_client());
REQUIRE(static_cast<void*>(nullptr) == tok.get_user_context());
REQUIRE(static_cast<mqtt::iaction_listener*>(nullptr) == tok.get_action_callback());
REQUIRE(!tok.is_complete());
REQUIRE(nullptr != tok.get_topics());
REQUIRE(size_t(2) == tok.get_topics()->size());
REQUIRE((*topics)[0] == (*tok.get_topics())[0]);
REQUIRE((*topics)[1] == (*tok.get_topics())[1]);
}
// ----------------------------------------------------------------------
// Test on success with data
// ----------------------------------------------------------------------
TEST_CASE("token on success with data", "[token]")
{
mqtt::token tok{TYPE, cli};
constexpr int MESSAGE_ID = 12;
MQTTAsync_successData data = {
.token = MESSAGE_ID,
};
data.alt.connect.serverURI = const_cast<char*>("tcp://some_server.com");
REQUIRE(!tok.is_complete());
mock_async_client::succeed(&tok, &data);
REQUIRE(tok.is_complete());
REQUIRE(MESSAGE_ID == tok.get_message_id());
}
// ----------------------------------------------------------------------
// Test on success without data
// ----------------------------------------------------------------------
TEST_CASE("token on success without data", "[token]")
{
mqtt::token tok{TYPE, cli};
REQUIRE(!tok.is_complete());
mock_async_client::succeed(&tok, nullptr);
REQUIRE(tok.is_complete());
}
// ----------------------------------------------------------------------
// Test on failure with data
// ----------------------------------------------------------------------
TEST_CASE("token on failure with data", "[token]")
{
mqtt::token tok{TYPE, cli};
REQUIRE(!tok.is_complete());
constexpr int MESSAGE_ID = 12;
MQTTAsync_failureData data = {
.token = MESSAGE_ID,
.code = 13,
.message = nullptr,
};
mock_async_client::fail(&tok, &data);
REQUIRE(tok.is_complete());
REQUIRE(MESSAGE_ID == tok.get_message_id());
}
// ----------------------------------------------------------------------
// Test on failure without data
// ----------------------------------------------------------------------
TEST_CASE("token on failure without data", "[token]")
{
mqtt::token tok{TYPE, cli};
REQUIRE(!tok.is_complete());
mock_async_client::fail(&tok, nullptr);
REQUIRE(tok.is_complete());
REQUIRE(0 == tok.get_message_id());
}
// ----------------------------------------------------------------------
// Test set callbacks
// ----------------------------------------------------------------------
TEST_CASE("token action callback", "[token]")
{
mock_action_listener listener;
mqtt::token tok{TYPE, cli};
tok.set_action_callback(listener);
REQUIRE(dynamic_cast<mqtt::iaction_listener*>(&listener) == tok.get_action_callback());
REQUIRE(!listener.succeeded());
mock_async_client::succeed(&tok, nullptr);
REQUIRE(listener.succeeded());
REQUIRE(!listener.failed());
mock_async_client::fail(&tok, nullptr);
REQUIRE(listener.failed());
}
// ----------------------------------------------------------------------
// Test wait for completion on success case
// All wait's should succeed immediately on successful completion.
// ----------------------------------------------------------------------
TEST_CASE("token wait success", "[token]")
{
const auto TIMEOUT = milliseconds(10);
mqtt::token tok{TYPE, cli};
// NOTE: Make sure the complete flag is already true and the return
// code (rc) is MQTTASYNC_SUCCESS, so the token::wait()
// returns immediately. Otherwise we will get stuck in a single thread
// that can't change the complete flag.
mock_async_client::succeed(&tok, nullptr);
REQUIRE(tok.is_complete());
// A wait does not reset the "complete" flag.
try {
tok.wait();
REQUIRE(tok.is_complete());
}
catch (...) {
FAIL("token::wait() should not throw on success");
}
// try_wait()
try {
REQUIRE(tok.try_wait());
REQUIRE(tok.is_complete());
}
catch (...) {
FAIL("token::wait() should not throw on success");
}
// wait_for()
try {
REQUIRE(tok.wait_for(TIMEOUT));
REQUIRE(tok.is_complete());
}
catch (...) {
FAIL("token::wait_for() should not throw on success");
}
// wait_until()
const auto TO = steady_clock::now() + TIMEOUT;
try {
REQUIRE(tok.wait_until(TO));
REQUIRE(tok.is_complete());
}
catch (...) {
FAIL("token::wait_until() should not throw on success");
}
}
// ----------------------------------------------------------------------
// Test wait for completion on failure case
// All wait's should throw if the action failed
// ----------------------------------------------------------------------
TEST_CASE("token wait failure", "[token]")
{
const auto TIMEOUT = milliseconds(10);
mqtt::token tok{TYPE, cli};
// NOTE: Make sure the complete flag is already true and the return
// code (rc) is MQTTASYNC_FAILURE, so the token::wait()
// returns immediately. Otherwise we will get stuck in a single thread
// that can't change the complete flag.
constexpr int MESSAGE_ID = 12;
MQTTAsync_failureData data = {
.token = MESSAGE_ID,
.code = MQTTASYNC_FAILURE,
.message = nullptr,
};
mock_async_client::fail(&tok, &data);
REQUIRE(tok.is_complete());
REQUIRE(MESSAGE_ID == tok.get_message_id());
REQUIRE(MQTTASYNC_FAILURE == tok.get_return_code());
try {
tok.wait();
FAIL("token::wait() should throw on failure");
}
catch (mqtt::exception& ex) {
REQUIRE(MQTTASYNC_FAILURE == ex.get_return_code());
}
try {
tok.try_wait();
FAIL("token::try_wait() should throw on failure");
}
catch (mqtt::exception& ex) {
REQUIRE(MQTTASYNC_FAILURE == ex.get_return_code());
}
try {
tok.wait_for(TIMEOUT);
FAIL("token::wait_for() should throw on failure");
}
catch (mqtt::exception& ex) {
REQUIRE(MQTTASYNC_FAILURE == ex.get_return_code());
}
try {
tok.wait_until(steady_clock::now() + TIMEOUT);
FAIL("token::wait_until() should throw on failure");
}
catch (mqtt::exception& ex) {
REQUIRE(MQTTASYNC_FAILURE == ex.get_return_code());
}
}
// ----------------------------------------------------------------------
// Test wait for completion on failure due timeout case
// All waits should return false, but not throw, on a timeout
// ----------------------------------------------------------------------
TEST_CASE("token wait for timeout", "[token]")
{
const auto TIMEOUT = milliseconds(10);
mqtt::token tok{TYPE, cli};
// Test for timeout on non-signaled token.
REQUIRE(!tok.is_complete());
// try_wait()
try {
REQUIRE(!tok.try_wait());
}
catch (...) {
FAIL("token::try_wait() should not throw");
}
// wait_for()
REQUIRE(!tok.is_complete());
try {
REQUIRE(!tok.wait_for(TIMEOUT));
}
catch (...) {
FAIL("token::wait_for() should not throw on timeout");
}
// wait_until()
const auto TO = steady_clock::now() + TIMEOUT;
REQUIRE(!tok.is_complete());
try {
REQUIRE(!tok.wait_until(TO));
}
catch (...) {
FAIL("token::wait_until() should not throw on timeout");
}
}