mirror of
https://github.com/boostorg/mqtt5.git
synced 2025-07-30 04:27:34 +02:00
async_disconnect will close the connection regardless of the current connection state
Summary: related to T13761 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D28057
This commit is contained in:
@ -24,7 +24,8 @@ enum operation_type {
|
||||
publish,
|
||||
receive,
|
||||
subscribe,
|
||||
unsubscribe
|
||||
unsubscribe,
|
||||
disconnect
|
||||
};
|
||||
|
||||
enum cancel_type {
|
||||
@ -85,13 +86,41 @@ void setup_cancel_op_test_case(
|
||||
c.async_receive(
|
||||
asio::bind_cancellation_slot(
|
||||
signal.slot(),
|
||||
[&handlers_called](
|
||||
[&c, &handlers_called](
|
||||
error_code ec, std::string t, std::string p, publish_props
|
||||
) {
|
||||
++handlers_called;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
BOOST_TEST(t == "");
|
||||
BOOST_TEST(p == "");
|
||||
|
||||
// right now, emitting a terminal signal on async_receive
|
||||
// does NOT cancel the client
|
||||
c.cancel();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
template <
|
||||
test::operation_type op_type,
|
||||
std::enable_if_t<op_type == test::operation_type::subscribe, bool> = true
|
||||
>
|
||||
void setup_cancel_op_test_case(
|
||||
client_type& c, asio::cancellation_signal& signal, int& handlers_called
|
||||
) {
|
||||
c.async_run(asio::detached);
|
||||
c.async_subscribe(
|
||||
subscribe_topic { "topic", subscribe_options {} }, subscribe_props {},
|
||||
asio::bind_cancellation_slot(
|
||||
signal.slot(),
|
||||
[&handlers_called](
|
||||
error_code ec, std::vector<reason_code> rcs, suback_props
|
||||
) {
|
||||
++handlers_called;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
BOOST_TEST_REQUIRE(rcs.size() == 1u);
|
||||
BOOST_TEST(rcs[0] == reason_codes::empty);
|
||||
}
|
||||
)
|
||||
);
|
||||
@ -123,23 +152,18 @@ void setup_cancel_op_test_case(
|
||||
|
||||
template <
|
||||
test::operation_type op_type,
|
||||
std::enable_if_t<op_type == test::operation_type::subscribe, bool> = true
|
||||
std::enable_if_t<op_type == test::operation_type::disconnect, bool> = true
|
||||
>
|
||||
void setup_cancel_op_test_case(
|
||||
client_type& c, asio::cancellation_signal& signal, int& handlers_called
|
||||
) {
|
||||
c.async_run(asio::detached);
|
||||
c.async_subscribe(
|
||||
subscribe_topic { "topic", subscribe_options {} }, subscribe_props {},
|
||||
c.async_disconnect(
|
||||
asio::bind_cancellation_slot(
|
||||
signal.slot(),
|
||||
[&handlers_called](
|
||||
error_code ec, std::vector<reason_code> rcs, suback_props
|
||||
) {
|
||||
[&handlers_called](error_code ec) {
|
||||
++handlers_called;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
BOOST_TEST_REQUIRE(rcs.size() == 1u);
|
||||
BOOST_TEST(rcs[0] == reason_codes::empty);
|
||||
}
|
||||
)
|
||||
);
|
||||
@ -194,8 +218,7 @@ BOOST_AUTO_TEST_CASE(client_cancel_async_receive) {
|
||||
run_cancel_op_test<test::client_cancel, test::receive>();
|
||||
}
|
||||
|
||||
// hangs
|
||||
BOOST_AUTO_TEST_CASE(signal_emit_async_receive, *boost::unit_test::disabled()) {
|
||||
BOOST_AUTO_TEST_CASE(signal_emit_async_receive) {
|
||||
run_cancel_op_test<test::signal_emit, test::receive>();
|
||||
}
|
||||
|
||||
@ -215,6 +238,10 @@ BOOST_AUTO_TEST_CASE(signal_emit_async_unsubscribe) {
|
||||
run_cancel_op_test<test::signal_emit, test::unsubscribe>();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(signal_emit_async_disconnect) {
|
||||
run_cancel_op_test<test::signal_emit, test::disconnect>();
|
||||
}
|
||||
|
||||
struct shared_test_data {
|
||||
error_code success {};
|
||||
error_code fail = asio::error::not_connected;
|
||||
|
@ -42,7 +42,6 @@ void run_malformed_props_test(const disconnect_props& dprops) {
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(malformed_reason_string) {
|
||||
disconnect_props dprops;
|
||||
dprops[prop::reason_string] = std::string { 0x01 };
|
||||
@ -64,10 +63,182 @@ BOOST_AUTO_TEST_CASE(malformed_user_property_value) {
|
||||
run_malformed_props_test(dprops);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(omit_props) {
|
||||
using test::after;
|
||||
using namespace std::chrono;
|
||||
struct shared_test_data {
|
||||
error_code success {};
|
||||
error_code fail = asio::error::not_connected;
|
||||
|
||||
const std::string connect = encoders::encode_connect(
|
||||
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
|
||||
);
|
||||
const std::string connack = encoders::encode_connack(
|
||||
true, reason_codes::success.value(), {}
|
||||
);
|
||||
const std::string disconnect = encoders::encode_disconnect(
|
||||
reason_codes::normal_disconnection.value(), disconnect_props {}
|
||||
);
|
||||
};
|
||||
|
||||
using test::after;
|
||||
using namespace std::chrono_literals;
|
||||
using client_type = mqtt_client<test::test_stream>;
|
||||
|
||||
template <typename TestCase>
|
||||
void run_test(test::msg_exchange broker_side, TestCase&& test_case) {
|
||||
asio::io_context ioc;
|
||||
auto executor = ioc.get_executor();
|
||||
auto& broker = asio::make_service<test::test_broker>(
|
||||
ioc, executor, std::move(broker_side)
|
||||
);
|
||||
|
||||
asio::steady_timer timer(executor);
|
||||
client_type c(executor);
|
||||
c.brokers("127.0.0.1")
|
||||
.async_run(asio::detached);
|
||||
|
||||
test_case(c, timer);
|
||||
|
||||
ioc.run();
|
||||
BOOST_TEST(broker.received_all_expected());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(successful_disconnect, shared_test_data) {
|
||||
constexpr int expected_handlers_called = 1;
|
||||
int handlers_called = 0;
|
||||
|
||||
test::msg_exchange broker_side;
|
||||
broker_side
|
||||
.expect(connect)
|
||||
.complete_with(success, after(0ms))
|
||||
.reply_with(connack, after(0ms))
|
||||
.expect(disconnect)
|
||||
.complete_with(success, after(0ms));
|
||||
|
||||
run_test(
|
||||
std::move(broker_side),
|
||||
[&](client_type& c, asio::steady_timer& timer) {
|
||||
timer.expires_after(100ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_disconnect(
|
||||
[&](error_code ec) {
|
||||
handlers_called++;
|
||||
BOOST_TEST(!ec);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(successful_disconnect_in_queue, shared_test_data) {
|
||||
constexpr int expected_handlers_called = 2;
|
||||
int handlers_called = 0;
|
||||
|
||||
// packets
|
||||
auto publish_qos0 = encoders::encode_publish(
|
||||
0, "topic", "payload", qos_e::at_most_once, retain_e::no, dup_e::no, {}
|
||||
);
|
||||
|
||||
test::msg_exchange broker_side;
|
||||
broker_side
|
||||
.expect(connect)
|
||||
.complete_with(success, after(1ms))
|
||||
.reply_with(connack, after(2ms))
|
||||
.expect(publish_qos0)
|
||||
.complete_with(success, after(1ms))
|
||||
.expect(disconnect)
|
||||
.complete_with(success, after(0ms));
|
||||
|
||||
run_test(
|
||||
std::move(broker_side),
|
||||
[&](client_type& c, asio::steady_timer& timer) {
|
||||
timer.expires_after(50ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_publish<qos_e::at_most_once>(
|
||||
"topic", "payload", retain_e::no, {},
|
||||
[&handlers_called](error_code ec) {
|
||||
BOOST_TEST(handlers_called == 0);
|
||||
handlers_called++;
|
||||
BOOST_TEST(!ec);
|
||||
}
|
||||
);
|
||||
c.async_disconnect(
|
||||
[&](error_code ec) {
|
||||
BOOST_TEST(handlers_called == 1);
|
||||
handlers_called++;
|
||||
BOOST_TEST(!ec);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(disconnect_on_disconnected_client, shared_test_data) {
|
||||
constexpr int expected_handlers_called = 1;
|
||||
int handlers_called = 0;
|
||||
|
||||
test::msg_exchange broker_side;
|
||||
broker_side
|
||||
.expect(connect);
|
||||
|
||||
run_test(
|
||||
std::move(broker_side),
|
||||
[&](client_type& c, asio::steady_timer& timer) {
|
||||
timer.expires_after(50ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_disconnect(
|
||||
[&](error_code ec) {
|
||||
handlers_called++;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(disconnect_in_queue_on_disconnected_client, shared_test_data) {
|
||||
constexpr int expected_handlers_called = 2;
|
||||
int handlers_called = 0;
|
||||
|
||||
test::msg_exchange broker_side;
|
||||
broker_side
|
||||
.expect(connect);
|
||||
|
||||
run_test(
|
||||
std::move(broker_side),
|
||||
[&](client_type& c, asio::steady_timer& timer) {
|
||||
timer.expires_after(50ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_publish<qos_e::at_most_once>(
|
||||
"topic", "payload", retain_e::no, {},
|
||||
[&handlers_called](error_code ec) {
|
||||
BOOST_TEST(handlers_called == 1);
|
||||
handlers_called++;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
}
|
||||
);
|
||||
c.async_disconnect(
|
||||
[&](error_code ec) {
|
||||
BOOST_TEST(handlers_called == 0);
|
||||
handlers_called++;
|
||||
BOOST_TEST(ec == asio::error::operation_aborted);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(omit_props, shared_test_data) {
|
||||
constexpr int expected_handlers_called = 1;
|
||||
int handlers_called = 0;
|
||||
|
||||
@ -75,58 +246,41 @@ BOOST_AUTO_TEST_CASE(omit_props) {
|
||||
co_props[prop::maximum_packet_size] = 20;
|
||||
|
||||
// packets
|
||||
auto connect = encoders::encode_connect(
|
||||
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
|
||||
);
|
||||
auto connack = encoders::encode_connack(
|
||||
auto connack_with_max_packet = encoders::encode_connack(
|
||||
false, reason_codes::success.value(), co_props
|
||||
);
|
||||
|
||||
disconnect_props props;
|
||||
props[prop::reason_string] = std::string(50, 'a');
|
||||
auto disconnect = encoders::encode_disconnect(
|
||||
auto big_disconnect = encoders::encode_disconnect(
|
||||
reason_codes::normal_disconnection.value(), props
|
||||
);
|
||||
auto disconnect_no_props = encoders::encode_disconnect(
|
||||
reason_codes::normal_disconnection.value(), disconnect_props {}
|
||||
);
|
||||
|
||||
error_code success {};
|
||||
|
||||
test::msg_exchange broker_side;
|
||||
broker_side
|
||||
.expect(connect)
|
||||
.complete_with(success, after(0ms))
|
||||
.reply_with(connack, after(0ms))
|
||||
.expect(disconnect_no_props)
|
||||
.reply_with(connack_with_max_packet, after(0ms))
|
||||
.expect(disconnect)
|
||||
.complete_with(success, after(0ms));
|
||||
|
||||
asio::io_context ioc;
|
||||
auto executor = ioc.get_executor();
|
||||
auto& broker = asio::make_service<test::test_broker>(
|
||||
ioc, executor, std::move(broker_side)
|
||||
run_test(
|
||||
std::move(broker_side),
|
||||
[&](client_type& c, asio::steady_timer& timer) {
|
||||
timer.expires_after(50ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_disconnect(
|
||||
disconnect_rc_e::normal_disconnection, props,
|
||||
[&](error_code ec) {
|
||||
handlers_called++;
|
||||
BOOST_TEST(!ec);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
using client_type = mqtt_client<test::test_stream>;
|
||||
client_type c(executor);
|
||||
c.brokers("127.0.0.1")
|
||||
.async_run(asio::detached);
|
||||
|
||||
asio::steady_timer timer(c.get_executor());
|
||||
timer.expires_after(50ms);
|
||||
timer.async_wait([&](error_code) {
|
||||
c.async_disconnect(
|
||||
disconnect_rc_e::normal_disconnection, props,
|
||||
[&](error_code ec) {
|
||||
handlers_called++;
|
||||
BOOST_TEST(!ec);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
ioc.run_for(2s);
|
||||
BOOST_TEST(handlers_called == expected_handlers_called);
|
||||
BOOST_TEST(broker.received_all_expected());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
Reference in New Issue
Block a user