mirror of
https://github.com/boostorg/mqtt5.git
synced 2025-07-30 04:27:34 +02:00
Reconnect on every Reason Code > 0x80 received in the CONNACK packet
Summary: related to T13767 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D28217
This commit is contained in:
@ -327,9 +327,8 @@ public:
|
|||||||
if (!rc.has_value()) // reason code not allowed in CONNACK
|
if (!rc.has_value()) // reason code not allowed in CONNACK
|
||||||
return complete(client::error::malformed_packet);
|
return complete(client::error::malformed_packet);
|
||||||
|
|
||||||
auto ec = to_asio_error(*rc);
|
if (*rc)
|
||||||
if (ec)
|
return complete(asio::error::try_again);
|
||||||
return complete(ec);
|
|
||||||
|
|
||||||
if (_ctx.co_props[prop::authentication_method].has_value())
|
if (_ctx.co_props[prop::authentication_method].has_value())
|
||||||
return _ctx.authenticator.async_auth(
|
return _ctx.authenticator.async_auth(
|
||||||
@ -425,20 +424,6 @@ private:
|
|||||||
_cancellation_state.slot().clear();
|
_cancellation_state.slot().clear();
|
||||||
std::move(_handler)(ec);
|
std::move(_handler)(ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static error_code to_asio_error(reason_code rc) {
|
|
||||||
using namespace boost::asio::error;
|
|
||||||
using namespace reason_codes;
|
|
||||||
|
|
||||||
if (rc == success)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (rc == unspecified_error || rc == server_unavailable ||
|
|
||||||
rc == server_busy || rc == connection_rate_exceeded)
|
|
||||||
return connection_refused;
|
|
||||||
|
|
||||||
return access_denied;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ struct shared_test_data {
|
|||||||
error_code success {};
|
error_code success {};
|
||||||
|
|
||||||
const std::string connack = encoders::encode_connack(
|
const std::string connack = encoders::encode_connack(
|
||||||
false, reason_codes::server_moved.value(), {}
|
false, reason_codes::success.value(), {}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -44,10 +44,16 @@ void run_test(
|
|||||||
|
|
||||||
client_type c(executor);
|
client_type c(executor);
|
||||||
client_fun(c);
|
client_fun(c);
|
||||||
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
|
c.brokers("127.0.0.1")
|
||||||
.async_run(asio::detached);
|
.async_run(asio::detached);
|
||||||
|
|
||||||
ioc.run_for(5s);
|
asio::steady_timer timer(executor);
|
||||||
|
timer.expires_after(700ms);
|
||||||
|
timer.async_wait(
|
||||||
|
[&c](error_code) { c.cancel(); }
|
||||||
|
);
|
||||||
|
|
||||||
|
ioc.run();
|
||||||
BOOST_TEST(broker.received_all_expected());
|
BOOST_TEST(broker.received_all_expected());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,7 +297,6 @@ struct shared_connack_prop_test_data {
|
|||||||
|
|
||||||
// data
|
// data
|
||||||
const uint8_t session_present = 1;
|
const uint8_t session_present = 1;
|
||||||
const uint8_t reason_code = reason_codes::server_moved.value();
|
|
||||||
|
|
||||||
const int32_t session_expiry_interval = 20;
|
const int32_t session_expiry_interval = 20;
|
||||||
const int16_t receive_maximum = 2000;
|
const int16_t receive_maximum = 2000;
|
||||||
@ -319,7 +324,7 @@ struct shared_connack_prop_test_data {
|
|||||||
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
|
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
|
||||||
);
|
);
|
||||||
const std::string connack = encoders::encode_connack(
|
const std::string connack = encoders::encode_connack(
|
||||||
false, reason_codes::server_moved.value(), cprops
|
false, reason_codes::success.value(), cprops
|
||||||
);
|
);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -357,11 +362,20 @@ void run_test_with_post_fun(
|
|||||||
);
|
);
|
||||||
|
|
||||||
client_type c(executor);
|
client_type c(executor);
|
||||||
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
|
c.brokers("127.0.0.1")
|
||||||
.async_run(asio::detached);
|
.async_run(asio::detached);
|
||||||
|
|
||||||
ioc.run_for(5s);
|
asio::steady_timer timer(executor);
|
||||||
client_fun(c);
|
timer.expires_after(700ms);
|
||||||
|
timer.async_wait(
|
||||||
|
[&c, fun = std::forward<TestingClientFun>(client_fun)](error_code) {
|
||||||
|
fun(c);
|
||||||
|
c.cancel();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
ioc.run();
|
||||||
|
|
||||||
BOOST_TEST(broker.received_all_expected());
|
BOOST_TEST(broker.received_all_expected());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,31 +81,13 @@ BOOST_FIXTURE_TEST_CASE(successfully_connect, shared_test_data) {
|
|||||||
.reply_with(connack, after(4ms));
|
.reply_with(connack, after(4ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == success);
|
BOOST_TEST(ec == success);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(connection_refused, shared_test_data) {
|
BOOST_FIXTURE_TEST_CASE(connack_with_fail_rc, shared_test_data) {
|
||||||
auto refuse_connack = encoders::encode_connack(
|
|
||||||
true, reason_codes::server_unavailable.value(), {}
|
|
||||||
);
|
|
||||||
|
|
||||||
test::msg_exchange broker_side;
|
|
||||||
broker_side
|
|
||||||
.expect(connect)
|
|
||||||
.complete_with(success, after(2ms))
|
|
||||||
.reply_with(refuse_connack, after(4ms));
|
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
|
||||||
BOOST_CHECK(ec == asio::error::connection_refused);
|
|
||||||
};
|
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(access_denied, shared_test_data) {
|
|
||||||
auto denied_connack = encoders::encode_connack(
|
auto denied_connack = encoders::encode_connack(
|
||||||
true, reason_codes::bad_username_or_password.value(), {}
|
true, reason_codes::bad_username_or_password.value(), {}
|
||||||
);
|
);
|
||||||
@ -117,7 +99,7 @@ BOOST_FIXTURE_TEST_CASE(access_denied, shared_test_data) {
|
|||||||
.reply_with(denied_connack, after(4ms));
|
.reply_with(denied_connack, after(4ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::access_denied);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -147,7 +129,7 @@ BOOST_FIXTURE_TEST_CASE(receive_wrong_packet, shared_test_data) {
|
|||||||
.reply_with(unexpected_packet, after(3ms));
|
.reply_with(unexpected_packet, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::try_again);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -164,7 +146,7 @@ BOOST_FIXTURE_TEST_CASE(malformed_connack_varlen, shared_test_data) {
|
|||||||
.reply_with(malformed_connack, after(3ms));
|
.reply_with(malformed_connack, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::try_again);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -181,7 +163,7 @@ BOOST_FIXTURE_TEST_CASE(malformed_connack_rc, shared_test_data) {
|
|||||||
.reply_with(malformed_connack, after(3ms));
|
.reply_with(malformed_connack, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == client::error::malformed_packet);
|
BOOST_TEST(ec == client::error::malformed_packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -204,7 +186,7 @@ BOOST_FIXTURE_TEST_CASE(fail_reading_connack_payload, shared_test_data) {
|
|||||||
.send(fail, after(7ms));
|
.send(fail, after(7ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == fail);
|
BOOST_TEST(ec == fail);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -224,7 +206,7 @@ BOOST_FIXTURE_TEST_CASE(receive_unexpected_auth, shared_test_data) {
|
|||||||
.send(auth, after(5ms));
|
.send(auth, after(5ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == client::error::malformed_packet);
|
BOOST_TEST(ec == client::error::malformed_packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
run_unit_test(std::move(broker_side), std::move(handler));
|
run_unit_test(std::move(broker_side), std::move(handler));
|
||||||
@ -276,7 +258,7 @@ BOOST_FIXTURE_TEST_CASE(successful_auth, shared_test_auth_data) {
|
|||||||
.reply_with(connack, after(4ms));
|
.reply_with(connack, after(4ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == success);
|
BOOST_TEST(ec == success);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -299,7 +281,7 @@ BOOST_FIXTURE_TEST_CASE(successful_auth_multi_step, shared_test_auth_data) {
|
|||||||
.reply_with(connack, after(4ms));
|
.reply_with(connack, after(4ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == success);
|
BOOST_TEST(ec == success);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -320,7 +302,7 @@ BOOST_FIXTURE_TEST_CASE(malformed_auth_rc, shared_test_auth_data) {
|
|||||||
.reply_with(malformed_auth_challenge, after(3ms));
|
.reply_with(malformed_auth_challenge, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == client::error::malformed_packet);
|
BOOST_TEST(ec == client::error::malformed_packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -344,7 +326,7 @@ BOOST_FIXTURE_TEST_CASE(mismatched_auth_method, shared_test_auth_data) {
|
|||||||
.reply_with(mismatched_auth_challenge, after(3ms));
|
.reply_with(mismatched_auth_challenge, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == client::error::malformed_packet);
|
BOOST_TEST(ec == client::error::malformed_packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -363,7 +345,7 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_auth, shared_test_auth_data) {
|
|||||||
.complete_with(fail, after(2ms));
|
.complete_with(fail, after(2ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == fail);
|
BOOST_TEST(ec == fail);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -376,7 +358,7 @@ BOOST_FIXTURE_TEST_CASE(auth_step_client_initial_fail, shared_test_auth_data) {
|
|||||||
test::msg_exchange broker_side;
|
test::msg_exchange broker_side;
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::try_again);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -393,7 +375,7 @@ BOOST_FIXTURE_TEST_CASE(auth_step_server_challenge_fail, shared_test_auth_data)
|
|||||||
.reply_with(auth_challenge, after(3ms));
|
.reply_with(auth_challenge, after(3ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::try_again);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
@ -413,7 +395,7 @@ BOOST_FIXTURE_TEST_CASE(auth_step_server_final_fail, shared_test_auth_data) {
|
|||||||
.reply_with(connack, after(4ms));
|
.reply_with(connack, after(4ms));
|
||||||
|
|
||||||
auto handler = [&](error_code ec) {
|
auto handler = [&](error_code ec) {
|
||||||
BOOST_CHECK(ec == asio::error::try_again);
|
BOOST_TEST(ec == asio::error::try_again);
|
||||||
};
|
};
|
||||||
|
|
||||||
detail::mqtt_ctx mqtt_ctx;
|
detail::mqtt_ctx mqtt_ctx;
|
||||||
|
Reference in New Issue
Block a user