| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "test_bits.hpp" |
| 21 |
#include "proton/connection.hpp" |
| 22 |
#include "proton/connection_options.hpp" |
| 23 |
#include "proton/container.hpp" |
| 24 |
#include "proton/delivery.hpp" |
| 25 |
#include "proton/error_condition.hpp" |
| 26 |
#include "proton/listen_handler.hpp" |
| 27 |
#include "proton/listener.hpp" |
| 28 |
#include "proton/message.hpp" |
| 29 |
#include "proton/messaging_handler.hpp" |
| 30 |
#include "proton/reconnect_options.hpp" |
| 31 |
#include "proton/receiver_options.hpp" |
| 32 |
#include "proton/transport.hpp" |
| 33 |
#include "proton/work_queue.hpp" |
| 34 |
|
| 35 |
#include <cstdlib> |
| 36 |
#include <ctime> |
| 37 |
#include <string> |
| 38 |
#include <cstdio> |
| 39 |
#include <memory> |
| 40 |
#include <sstream> |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
// Wait for N things to be done. |
| 45 |
class waiter { |
| 46 |
size_t count; |
| 47 |
public: |
| 48 |
waiter(size_t n) : count(n) {} |
| 49 |
void done() { if (--count == 0) ready(); } |
| 50 |
virtual void ready() = 0; |
| 51 |
}; |
| 52 |
|
| 53 |
class server_connection_handler : public proton::messaging_handler { |
| 54 |
|
| 55 |
struct listen_handler : public proton::listen_handler { |
| 56 |
proton::connection_options opts; |
| 57 |
std::string url; |
| 58 |
waiter& listen_waiter; |
| 59 |
|
| 60 |
listen_handler(proton::messaging_handler& h, waiter& w) : listen_waiter(w) { |
| 61 |
opts.handler(h); |
| 62 |
} |
| 63 |
|
| 64 |
void on_open(proton::listener& l) override { |
| 65 |
std::ostringstream o; |
| 66 |
o << "//:" << l.port(); // Connect to the actual listening port |
| 67 |
url = o.str(); |
| 68 |
// Schedule rather than call done() direct to ensure serialization |
| 69 |
l.container().schedule(proton::duration::IMMEDIATE, |
| 70 |
proton::make_work(&waiter::done, &listen_waiter)); |
| 71 |
} |
| 72 |
|
| 73 |
proton::connection_options on_accept(proton::listener&) override { return opts; } |
| 74 |
}; |
| 75 |
|
| 76 |
listen_handler listen_handler_; |
| 77 |
proton::listener listener_; |
| 78 |
int messages_ = 0; |
| 79 |
int expect_; |
| 80 |
bool closing_ = false; |
| 81 |
|
| 82 |
void close (proton::connection &c) { |
| 83 |
if (closing_) return; |
| 84 |
|
| 85 |
c.close(proton::error_condition("amqp:connection:forced", "Failover testing")); |
| 86 |
closing_ = true; |
| 87 |
} |
| 88 |
|
| 89 |
public: |
| 90 |
server_connection_handler(proton::container& c, int e, waiter& w) |
| 91 |
: listen_handler_(*this, w), expect_(e) |
| 92 |
{ |
| 93 |
listener_ = c.listen("//:0", listen_handler_); |
| 94 |
} |
| 95 |
|
| 96 |
std::string url() const { |
| 97 |
if (listen_handler_.url.empty()) throw std::runtime_error("no url"); |
| 98 |
return listen_handler_.url; |
| 99 |
} |
| 100 |
|
| 101 |
void on_connection_open(proton::connection &c) override { |
| 102 |
// Only listen for a single connection |
| 103 |
listener_.stop(); |
| 104 |
if (messages_==expect_) close(c); |
| 105 |
else c.open(); |
| 106 |
} |
| 107 |
|
| 108 |
void on_receiver_open(proton::receiver &r) override { |
| 109 |
// Reduce message noise in PN_TRACE output for debugging. |
| 110 |
// Only the first message is relevant |
| 111 |
// Control accepts, accepting the message tells the client to finally close. |
| 112 |
r.open(proton::receiver_options().credit_window(0).auto_accept(false)); |
| 113 |
r.add_credit(1); |
| 114 |
} |
| 115 |
|
| 116 |
void on_message(proton::delivery & d, proton::message & m) override { |
| 117 |
++messages_; |
| 118 |
proton::connection c = d.connection(); |
| 119 |
if (messages_==expect_) close(c); |
| 120 |
else d.accept(); |
| 121 |
} |
| 122 |
|
| 123 |
void on_transport_error(proton::transport & ) override { |
| 124 |
// If we get an error then (try to) stop the listener |
| 125 |
// - this will stop the listener if we didn't already accept a connection |
| 126 |
listener_.stop(); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
class tester_base: public proton::messaging_handler { |
| 131 |
void on_connection_open(proton::connection& c) override { |
| 132 |
if (!c.reconnected()) { |
| 133 |
start_count++; |
| 134 |
c.open_sender("messages"); |
| 135 |
} |
| 136 |
ASSERT_EQUAL(bool(open_count), c.reconnected()); |
| 137 |
open_count++; |
| 138 |
} |
| 139 |
|
| 140 |
void on_connection_error(proton::connection& c) override { |
| 141 |
connection_error_count++; |
| 142 |
} |
| 143 |
|
| 144 |
void on_sender_open(proton::sender &s) override { |
| 145 |
link_open_count++; |
| 146 |
} |
| 147 |
|
| 148 |
void on_sendable(proton::sender& s) override { |
| 149 |
s.send(proton::message("hello")); |
| 150 |
} |
| 151 |
|
| 152 |
void on_tracker_accept(proton::tracker& d) override { |
| 153 |
d.connection().close(); |
| 154 |
} |
| 155 |
|
| 156 |
void on_transport_error(proton::transport& t) override { |
| 157 |
ASSERT_EQUAL(bool(transport_error_count), t.connection().reconnected()); |
| 158 |
transport_error_count++; |
| 159 |
} |
| 160 |
|
| 161 |
void on_transport_close(proton::transport& t) override { |
| 162 |
transport_close_count++; |
| 163 |
} |
| 164 |
|
| 165 |
protected: |
| 166 |
int start_count = 0; |
| 167 |
int open_count = 0; |
| 168 |
int link_open_count = 0; |
| 169 |
int transport_error_count = 0; |
| 170 |
int transport_close_count = 0; |
| 171 |
int connection_error_count = 0; |
| 172 |
}; |
| 173 |
|
| 174 |
class tester : public tester_base, public waiter { |
| 175 |
public: |
| 176 |
tester() : waiter(3), container_(*this, "reconnect_client") {} |
| 177 |
|
| 178 |
void on_container_start(proton::container &c) override { |
| 179 |
// Server that fails upon connection |
| 180 |
s1.reset(new server_connection_handler(c, 0, *this)); |
| 181 |
// Server that fails on first message |
| 182 |
s2.reset(new server_connection_handler(c, 1, *this)); |
| 183 |
// server that doesn't fail in this test |
| 184 |
s3.reset(new server_connection_handler(c, 100, *this)); |
| 185 |
} |
| 186 |
|
| 187 |
// waiter::ready is called when all 3 listeners are ready. |
| 188 |
void ready() override { |
| 189 |
container_.connect(s1->url(), proton::connection_options().failover_urls({s2->url(), s3->url()})); |
| 190 |
} |
| 191 |
|
| 192 |
void run() { |
| 193 |
container_.run(); |
| 194 |
ASSERT_EQUAL(1, start_count); |
| 195 |
ASSERT_EQUAL(3, open_count); |
| 196 |
// Could be > 3, unpredictable number reconnects while listener comes up. |
| 197 |
ASSERT(2 < transport_error_count); |
| 198 |
// Last reconnect fails before opening links |
| 199 |
ASSERT(1 < link_open_count); |
| 200 |
// One final transport close, not an error |
| 201 |
ASSERT_EQUAL(1, transport_close_count); |
| 202 |
ASSERT_EQUAL(0, connection_error_count); |
| 203 |
} |
| 204 |
|
| 205 |
private: |
| 206 |
std::unique_ptr<server_connection_handler> s1; |
| 207 |
std::unique_ptr<server_connection_handler> s2; |
| 208 |
std::unique_ptr<server_connection_handler> s3; |
| 209 |
proton::container container_; |
| 210 |
}; |
| 211 |
|
| 212 |
int test_failover_simple() { |
| 213 |
tester().run(); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
class empty_failover_tester : public tester_base, public waiter { |
| 218 |
public: |
| 219 |
empty_failover_tester() : waiter(1), container_(*this, "reconnect_client") {} |
| 220 |
|
| 221 |
void on_container_start(proton::container &c) override { |
| 222 |
// Server that fails upon connection |
| 223 |
s1.reset(new server_connection_handler(c, 0, *this)); |
| 224 |
} |
| 225 |
|
| 226 |
// waiter::ready is called when a listener is ready. |
| 227 |
void ready() override { |
| 228 |
container_.connect(s1->url(), proton::connection_options().failover_urls({})); |
| 229 |
} |
| 230 |
|
| 231 |
void run() { |
| 232 |
container_.run(); |
| 233 |
ASSERT_EQUAL(1, start_count); |
| 234 |
ASSERT_EQUAL(1, open_count); |
| 235 |
// Could be >=0, unpredictable number reconnects while listener comes up. |
| 236 |
ASSERT(0 <= transport_error_count); |
| 237 |
ASSERT(0 <= link_open_count); |
| 238 |
ASSERT_EQUAL(1, transport_close_count); |
| 239 |
ASSERT_EQUAL(1, connection_error_count); |
| 240 |
} |
| 241 |
|
| 242 |
private: |
| 243 |
std::unique_ptr<server_connection_handler> s1; |
| 244 |
proton::container container_; |
| 245 |
}; |
| 246 |
|
| 247 |
int test_empty_failover() { |
| 248 |
empty_failover_tester().run(); |
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
class stop_reconnect_tester : public proton::messaging_handler { |
| 255 |
public: |
| 256 |
stop_reconnect_tester() : |
| 257 |
container_(*this, "reconnect_tester") |
| 258 |
{ |
| 259 |
} |
| 260 |
|
| 261 |
void deferred_stop() { |
| 262 |
container_.stop(); |
| 263 |
} |
| 264 |
|
| 265 |
void on_container_start(proton::container &c) override { |
| 266 |
proton::reconnect_options reconnect_options; |
| 267 |
c.connect("this-is-not-going-to work.com", proton::connection_options().reconnect(reconnect_options)); |
| 268 |
c.schedule(proton::duration::SECOND, proton::make_work(&stop_reconnect_tester::deferred_stop, this)); |
| 269 |
} |
| 270 |
|
| 271 |
void run() { |
| 272 |
container_.run(); |
| 273 |
} |
| 274 |
|
| 275 |
private: |
| 276 |
proton::container container_; |
| 277 |
}; |
| 278 |
|
| 279 |
int test_stop_reconnect() { |
| 280 |
stop_reconnect_tester().run(); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
class authfail_reconnect_tester : public proton::messaging_handler, public waiter { |
| 285 |
public: |
| 286 |
authfail_reconnect_tester() : |
| 287 |
waiter(1), container_(*this, "authfail_reconnect_tester") |
| 288 |
{} |
| 289 |
|
| 290 |
void deferred_stop() { |
| 291 |
container_.stop(); |
| 292 |
} |
| 293 |
|
| 294 |
void on_container_start(proton::container& c) override { |
| 295 |
// This server won't fail in this test |
| 296 |
s1.reset(new server_connection_handler(c, 100, *this)); |
| 297 |
c.schedule(proton::duration::SECOND, proton::make_work(&authfail_reconnect_tester::deferred_stop, this)); |
| 298 |
} |
| 299 |
|
| 300 |
void on_transport_error(proton::transport& t) override { |
| 301 |
errored_ = true; |
| 302 |
} |
| 303 |
|
| 304 |
void ready() override { |
| 305 |
proton::connection_options co; |
| 306 |
co.sasl_allowed_mechs("PLAIN"); |
| 307 |
co.reconnect(proton::reconnect_options()); |
| 308 |
container_.connect(s1->url(), co); |
| 309 |
} |
| 310 |
|
| 311 |
void run() { |
| 312 |
container_.run(); |
| 313 |
ASSERT(errored_); |
| 314 |
} |
| 315 |
|
| 316 |
private: |
| 317 |
proton::container container_; |
| 318 |
std::unique_ptr<server_connection_handler> s1; |
| 319 |
bool errored_ = false; |
| 320 |
}; |
| 321 |
|
| 322 |
// Verify we can stop reconnecting by calling close() in on_transport_error() |
| 323 |
class test_reconnecting_close : public proton::messaging_handler, public waiter { |
| 324 |
public: |
| 325 |
test_reconnecting_close() : waiter(1), container_(*this, "test_reconnecting_close") {} |
| 326 |
|
| 327 |
void on_container_start(proton::container &c) override { |
| 328 |
s1.reset(new server_connection_handler(c, 0, *this)); |
| 329 |
} |
| 330 |
|
| 331 |
void ready() override { |
| 332 |
container_.connect(s1->url(), proton::connection_options().reconnect(proton::reconnect_options())); |
| 333 |
} |
| 334 |
|
| 335 |
void on_transport_error(proton::transport& t) override { |
| 336 |
transport_error_called = true; |
| 337 |
t.connection().close(); // Abort reconnection |
| 338 |
} |
| 339 |
|
| 340 |
void on_connection_close(proton::connection& c) override { |
| 341 |
ASSERT(0); // Not expecting any clean close |
| 342 |
} |
| 343 |
|
| 344 |
void run() { |
| 345 |
container_.run(); |
| 346 |
} |
| 347 |
|
| 348 |
private: |
| 349 |
proton::container container_; |
| 350 |
std::string err_; |
| 351 |
bool transport_error_called = false; |
| 352 |
std::unique_ptr<server_connection_handler> s1; |
| 353 |
}; |
| 354 |
|
| 355 |
int test_auth_fail_reconnect() { |
| 356 |
authfail_reconnect_tester().run(); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
class test_reconnect_url : public proton::messaging_handler { |
| 361 |
public: |
| 362 |
test_reconnect_url() |
| 363 |
: container_(*this, "test_reconnect_update") {} |
| 364 |
|
| 365 |
proton::reconnect_options ropts() { |
| 366 |
// Fast as we can to avoid needless test slowness. |
| 367 |
return proton::reconnect_options().delay(proton::duration::MILLISECOND); |
| 368 |
} |
| 369 |
|
| 370 |
proton::connection_options copts() { return proton::connection_options(); } |
| 371 |
|
| 372 |
void on_container_start(proton::container &c) override { |
| 373 |
// Never actually connects, keeps re-trying to bogus hostnames with |
| 374 |
// changing options. |
| 375 |
c.connect("nosuchhost0", |
| 376 |
copts() |
| 377 |
.reconnect(ropts()) |
| 378 |
.virtual_host("vhost0") |
| 379 |
.user("user0") |
| 380 |
.reconnect_url("hahaha1")); |
| 381 |
} |
| 382 |
|
| 383 |
void on_transport_error(proton::transport &t) override { |
| 384 |
switch (++errors_) { |
| 385 |
case 1: |
| 386 |
ASSERT_SUBSTRING("nosuchhost0", t.error().what()); // First failure |
| 387 |
break; |
| 388 |
case 2: { |
| 389 |
ASSERT_SUBSTRING("hahaha1",t.error().what()); // Second failure |
| 390 |
ASSERT_EQUAL("user0", t.connection().user()); |
| 391 |
break; |
| 392 |
} |
| 393 |
case 3: |
| 394 |
ASSERT_SUBSTRING("hahaha1", t.error().what()); // Still trying reconnect url |
| 395 |
t.connection().update_options(copts().reconnect_url("nosuchhost1")); |
| 396 |
// Verify changing reconnect options does not affect other options. |
| 397 |
ASSERT_EQUAL("user0", t.connection().user()); |
| 398 |
break; |
| 399 |
case 4: |
| 400 |
ASSERT_SUBSTRING("nosuchhost1", t.error().what()); // Re-try new reconnect url |
| 401 |
break; |
| 402 |
default: |
| 403 |
t.connection().container().stop(); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void run() { container_.run(); } |
| 408 |
|
| 409 |
private: |
| 410 |
int errors_ = 0; |
| 411 |
proton::container container_; |
| 412 |
}; |
| 413 |
|
| 414 |
// Verify we can change connection options for reconnect on_transport_error() |
| 415 |
class test_reconnect_update_failover : public proton::messaging_handler { |
| 416 |
public: |
| 417 |
test_reconnect_update_failover() |
| 418 |
: container_(*this, "test_reconnect_update") {} |
| 419 |
|
| 420 |
proton::reconnect_options ropts() { |
| 421 |
// Fast as we can to avoid needless test slowness. |
| 422 |
return proton::reconnect_options().delay(proton::duration::MILLISECOND); |
| 423 |
} |
| 424 |
|
| 425 |
proton::connection_options copts() { return proton::connection_options(); } |
| 426 |
|
| 427 |
void on_container_start(proton::container &c) override { |
| 428 |
// Never actually connects, keeps re-trying to bogus hostnames with |
| 429 |
// changing options. |
| 430 |
c.connect("nosuchhost0", copts().reconnect(ropts()).virtual_host("vhost0").user("user0")); |
| 431 |
} |
| 432 |
|
| 433 |
void on_transport_error(proton::transport &t) override { |
| 434 |
switch (++errors_) { |
| 435 |
case 1: |
| 436 |
ASSERT_SUBSTRING("nosuchhost0", t.error().what()); // First failure |
| 437 |
break; |
| 438 |
case 2: { |
| 439 |
ASSERT_SUBSTRING("nosuchhost0",t.error().what()); // Second failure |
| 440 |
std::vector<std::string> urls; |
| 441 |
urls.push_back("nosuchhost1"); |
| 442 |
// Update the failover list |
| 443 |
t.connection().update_options(copts().failover_urls(urls)); |
| 444 |
// Verify changing reconnect options does not affect other options. |
| 445 |
ASSERT_EQUAL("user0", t.connection().user()); |
| 446 |
break; |
| 447 |
} |
| 448 |
case 3: |
| 449 |
ASSERT_SUBSTRING("nosuchhost1", t.error().what()); // Using failover host |
| 450 |
// Change a non-reconnect option should not affect reconnect |
| 451 |
t.connection().update_options(copts().user("user1")); |
| 452 |
break; |
| 453 |
case 4: |
| 454 |
ASSERT_SUBSTRING("nosuchhost0", t.error().what()); // Back to original url |
| 455 |
ASSERT_EQUAL("user1", t.connection().user()); |
| 456 |
break; |
| 457 |
case 5: |
| 458 |
ASSERT_SUBSTRING("nosuchhost1", t.error().what()); // Still have failover |
| 459 |
break; |
| 460 |
default: |
| 461 |
t.connection().container().stop(); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
void run() { container_.run(); } |
| 466 |
|
| 467 |
private: |
| 468 |
int errors_ = 0; |
| 469 |
proton::container container_; |
| 470 |
}; |
| 471 |
|
| 472 |
class test_reconnect_update_simple : public proton::messaging_handler { |
| 473 |
public: |
| 474 |
test_reconnect_update_simple() |
| 475 |
: container_(*this, "test_reconnect_update") {} |
| 476 |
|
| 477 |
proton::reconnect_options ropts() { |
| 478 |
// Fast as we can to avoid needless test slowness. |
| 479 |
return proton::reconnect_options().delay(proton::duration::MILLISECOND); |
| 480 |
} |
| 481 |
|
| 482 |
proton::connection_options copts() { return proton::connection_options(); } |
| 483 |
|
| 484 |
void on_container_start(proton::container &c) override { |
| 485 |
// Never actually connects, keeps re-trying to bogus hostnames with |
| 486 |
// changing options. |
| 487 |
c.connect("nosuchhost0", copts().reconnect(ropts()).virtual_host("vhost0").user("user0")); |
| 488 |
} |
| 489 |
|
| 490 |
void on_transport_error(proton::transport &t) override { |
| 491 |
switch (++errors_) { |
| 492 |
case 1: |
| 493 |
ASSERT_SUBSTRING("nosuchhost0", t.error().what()); // First failure |
| 494 |
break; |
| 495 |
case 2: { |
| 496 |
ASSERT_SUBSTRING("nosuchhost0",t.error().what()); // Second failure |
| 497 |
t.connection().update_options(copts().reconnect_url("nosuchhost1")); |
| 498 |
// Verify changing reconnect options does not affect other options. |
| 499 |
ASSERT_EQUAL("user0", t.connection().user()); |
| 500 |
break; |
| 501 |
} |
| 502 |
case 3: |
| 503 |
ASSERT_SUBSTRING("nosuchhost1", t.error().what()); // Re-try original |
| 504 |
t.connection().update_options(copts().reconnect_url("notsuchahostatall")); |
| 505 |
break; |
| 506 |
case 4: |
| 507 |
ASSERT_SUBSTRING("notsuchahostatall", t.error().what()); // Re-try new reconnect url |
| 508 |
break; |
| 509 |
case 5: |
| 510 |
ASSERT_SUBSTRING("notsuchahostatall", t.error().what()); // Re-try new reconnect url |
| 511 |
// Change a non-reconnect option should not affect reconnect |
| 512 |
t.connection().update_options(copts().user("user1")); |
| 513 |
break; |
| 514 |
case 6: |
| 515 |
ASSERT_SUBSTRING("notsuchahostatall", t.error().what()); // Same reconnect url |
| 516 |
ASSERT_EQUAL("user1", t.connection().user()); |
| 517 |
t.connection().update_options(copts().reconnect_url("nosuchhost1")); |
| 518 |
break; |
| 519 |
case 7: |
| 520 |
ASSERT_SUBSTRING("nosuchhost1", t.error().what()); |
| 521 |
break; |
| 522 |
default: |
| 523 |
t.connection().container().stop(); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void run() { container_.run(); } |
| 528 |
|
| 529 |
private: |
| 530 |
int errors_ = 0; |
| 531 |
proton::container container_; |
| 532 |
}; |
| 533 |
|
| 534 |
int main(int argc, char **argv) { |
| 535 |
int failed = 0; |
| 536 |
RUN_ARGV_TEST(failed, test_failover_simple()); |
| 537 |
RUN_ARGV_TEST(failed, test_empty_failover()); |
| 538 |
RUN_ARGV_TEST(failed, test_stop_reconnect()); |
| 539 |
RUN_ARGV_TEST(failed, test_auth_fail_reconnect()); |
| 540 |
RUN_ARGV_TEST(failed, test_reconnecting_close().run()); |
| 541 |
RUN_ARGV_TEST(failed, test_reconnect_url().run()); |
| 542 |
RUN_ARGV_TEST(failed, test_reconnect_update_failover().run()); |
| 543 |
RUN_ARGV_TEST(failed, test_reconnect_update_simple().run()); |
| 544 |
return failed; |
| 545 |
} |