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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "rgw_amqp.h"
#include "common/ceph_context.h"
#include "amqp_mock.h"
#include <gtest/gtest.h>
#include <chrono>
#include <thread>
#include <atomic>
using namespace rgw;
const std::chrono::milliseconds wait_time(10);
const std::chrono::milliseconds long_wait_time = wait_time*50;
const std::chrono::seconds idle_time(35);
class CctCleaner {
CephContext* cct;
public:
CctCleaner(CephContext* _cct) : cct(_cct) {}
~CctCleaner() {
#ifdef WITH_SEASTAR
delete cct;
#else
cct->put();
#endif
}
};
auto cct = new CephContext(CEPH_ENTITY_TYPE_CLIENT);
CctCleaner cleaner(cct);
class TestAMQP : public ::testing::Test {
protected:
amqp::connection_id_t conn_id;
unsigned current_dequeued = 0U;
void SetUp() override {
ASSERT_TRUE(amqp::init(cct));
}
void TearDown() override {
amqp::shutdown();
}
// wait for at least one new (since last drain) message to be dequeueud
// and then wait for all pending answers to be received
void wait_until_drained() {
while (amqp::get_dequeued() == current_dequeued) {
std::this_thread::sleep_for(wait_time);
}
while (amqp::get_inflight() > 0) {
std::this_thread::sleep_for(wait_time);
}
current_dequeued = amqp::get_dequeued();
}
};
std::atomic<bool> callback_invoked = false;
std::atomic<int> callbacks_invoked = 0;
// note: because these callback are shared among different "publish" calls
// they should be used on different connections
void my_callback_expect_ack(int rc) {
EXPECT_EQ(0, rc);
callback_invoked = true;
}
void my_callback_expect_nack(int rc) {
EXPECT_LT(rc, 0);
callback_invoked = true;
}
void my_callback_expect_multiple_acks(int rc) {
EXPECT_EQ(0, rc);
++callbacks_invoked;
}
class dynamic_callback_wrapper {
dynamic_callback_wrapper() = default;
public:
static dynamic_callback_wrapper* create() {
return new dynamic_callback_wrapper;
}
void callback(int rc) {
EXPECT_EQ(0, rc);
++callbacks_invoked;
delete this;
}
};
void my_callback_expect_close_or_ack(int rc) {
// deleting the connection should trigger the callback with -4098
// but due to race conditions, some my get an ack
EXPECT_TRUE(-4098 == rc || 0 == rc);
}
TEST_F(TestAMQP, ConnectionOK)
{
const auto connection_number = amqp::get_connection_count();
auto rc = amqp::connect(conn_id, "amqp://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = amqp::publish(conn_id, "topic", "message");
EXPECT_EQ(rc, 0);
}
TEST_F(TestAMQP, SSLConnectionOK)
{
const int port = 5671;
const auto connection_number = amqp::get_connection_count();
amqp_mock::set_valid_port(port);
auto rc = amqp::connect(conn_id, "amqps://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = amqp::publish(conn_id, "topic", "message");
EXPECT_EQ(rc, 0);
amqp_mock::set_valid_port(5672);
}
TEST_F(TestAMQP, PlainAndSSLConnectionsOK)
{
const int port = 5671;
const auto connection_number = amqp::get_connection_count();
amqp_mock::set_valid_port(port);
amqp::connection_id_t conn_id1;
auto rc = amqp::connect(conn_id1, "amqps://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = amqp::publish(conn_id1, "topic", "message");
EXPECT_EQ(rc, 0);
EXPECT_EQ(amqp::to_string(conn_id1), "amqps://localhost:5671/?exchange=ex1");
amqp_mock::set_valid_port(5672);
amqp::connection_id_t conn_id2;
rc = amqp::connect(conn_id2, "amqp://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::to_string(conn_id2), "amqp://localhost:5672/?exchange=ex1");
EXPECT_EQ(amqp::get_connection_count(), connection_number + 2);
rc = amqp::publish(conn_id2, "topic", "message");
EXPECT_EQ(rc, 0);
}
TEST_F(TestAMQP, ConnectionReuse)
{
amqp::connection_id_t conn_id1;
auto rc = amqp::connect(conn_id1, "amqp://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id2;
rc = amqp::connect(conn_id2, "amqp://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number);
rc = amqp::publish(conn_id1, "topic", "message");
EXPECT_EQ(rc, 0);
}
TEST_F(TestAMQP, NameResolutionFail)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://kaboom", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, InvalidPort)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://localhost:1234", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, InvalidHost)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://0.0.0.1", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, InvalidVhost)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://localhost/kaboom", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, UserPassword)
{
amqp_mock::set_valid_host("127.0.0.1");
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://foo:bar@127.0.0.1", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
// now try the same connection with default user/password
amqp_mock::set_valid_host("127.0.0.2");
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://guest:guest@127.0.0.2", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, URLParseError)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "http://localhost", "ex1", false, false, boost::none);
EXPECT_FALSE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, ExchangeMismatch)
{
callback_invoked = false;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "http://localhost", "ex2", false, false, boost::none);
EXPECT_FALSE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
TEST_F(TestAMQP, MaxConnections)
{
// fill up all connections
std::vector<amqp::connection_id_t> connections;
auto remaining_connections = amqp::get_max_connections() - amqp::get_connection_count();
while (remaining_connections > 0) {
const auto host = "127.10.0." + std::to_string(remaining_connections);
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
--remaining_connections;
connections.push_back(conn_id);
}
EXPECT_EQ(amqp::get_connection_count(), amqp::get_max_connections());
wait_until_drained();
// try to add another connection
{
const std::string host = "toomany";
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_FALSE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
}
EXPECT_EQ(amqp::get_connection_count(), amqp::get_max_connections());
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, ReceiveAck)
{
callback_invoked = false;
const std::string host("localhost1");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, ImplicitConnectionClose)
{
callback_invoked = false;
const std::string host("localhost1");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
const auto NUMBER_OF_CALLS = 2000;
for (auto i = 0; i < NUMBER_OF_CALLS; ++i) {
auto rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_close_or_ack);
EXPECT_EQ(rc, 0);
}
wait_until_drained();
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, ReceiveMultipleAck)
{
callbacks_invoked = 0;
const std::string host("localhost1");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
const auto NUMBER_OF_CALLS = 100;
for (auto i=0; i < NUMBER_OF_CALLS; ++i) {
auto rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_multiple_acks);
EXPECT_EQ(rc, 0);
}
wait_until_drained();
EXPECT_EQ(callbacks_invoked, NUMBER_OF_CALLS);
callbacks_invoked = 0;
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, ReceiveAckForMultiple)
{
callbacks_invoked = 0;
const std::string host("localhost1");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
amqp_mock::set_multiple(59);
const auto NUMBER_OF_CALLS = 100;
for (auto i=0; i < NUMBER_OF_CALLS; ++i) {
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_multiple_acks);
EXPECT_EQ(rc, 0);
}
wait_until_drained();
EXPECT_EQ(callbacks_invoked, NUMBER_OF_CALLS);
callbacks_invoked = 0;
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, DynamicCallback)
{
callbacks_invoked = 0;
const std::string host("localhost1");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
amqp_mock::set_multiple(59);
const auto NUMBER_OF_CALLS = 100;
for (auto i=0; i < NUMBER_OF_CALLS; ++i) {
rc = publish_with_confirm(conn_id, "topic", "message",
std::bind(&dynamic_callback_wrapper::callback, dynamic_callback_wrapper::create(), std::placeholders::_1));
EXPECT_EQ(rc, 0);
}
wait_until_drained();
EXPECT_EQ(callbacks_invoked, NUMBER_OF_CALLS);
callbacks_invoked = 0;
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, ReceiveNack)
{
callback_invoked = false;
amqp_mock::REPLY_ACK = false;
const std::string host("localhost2");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::REPLY_ACK = true;
callback_invoked = false;
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, FailWrite)
{
callback_invoked = false;
amqp_mock::FAIL_NEXT_WRITE = true;
const std::string host("localhost2");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::FAIL_NEXT_WRITE = false;
callback_invoked = false;
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, RetryInvalidHost)
{
callback_invoked = false;
const std::string host = "192.168.0.1";
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://"+host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
// now next retry should be ok
callback_invoked = false;
amqp_mock::set_valid_host(host);
std::this_thread::sleep_for(long_wait_time);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, RetryInvalidPort)
{
callback_invoked = false;
const int port = 9999;
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://localhost:" + std::to_string(port), "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
// now next retry should be ok
callback_invoked = false;
amqp_mock::set_valid_port(port);
std::this_thread::sleep_for(long_wait_time);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::set_valid_port(5672);
}
TEST_F(TestAMQP, RetryFailWrite)
{
callback_invoked = false;
amqp_mock::FAIL_NEXT_WRITE = true;
const std::string host("localhost2");
amqp_mock::set_valid_host(host);
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://" + host, "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
// now next retry should be ok
amqp_mock::FAIL_NEXT_WRITE = false;
callback_invoked = false;
std::this_thread::sleep_for(long_wait_time);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_ack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
amqp_mock::set_valid_host("localhost");
}
TEST_F(TestAMQP, IdleConnection)
{
// this test is skipped since it takes 30seconds
//GTEST_SKIP();
const auto connection_number = amqp::get_connection_count();
amqp::connection_id_t conn_id;
auto rc = amqp::connect(conn_id, "amqp://localhost", "ex1", false, false, boost::none);
EXPECT_TRUE(rc);
EXPECT_EQ(amqp::get_connection_count(), connection_number + 1);
std::this_thread::sleep_for(idle_time);
EXPECT_EQ(amqp::get_connection_count(), connection_number);
rc = publish_with_confirm(conn_id, "topic", "message", my_callback_expect_nack);
EXPECT_EQ(rc, 0);
wait_until_drained();
EXPECT_TRUE(callback_invoked);
}
|