2023-10-05 10:19:37 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
#include <boost/asio/as_tuple.hpp>
|
2024-01-19 13:45:09 +01:00
|
|
|
#include <boost/asio/bind_cancellation_slot.hpp>
|
|
|
|
|
#include <boost/asio/cancellation_signal.hpp>
|
2023-11-24 11:50:47 +01:00
|
|
|
#include <boost/asio/co_spawn.hpp>
|
|
|
|
|
#include <boost/asio/detached.hpp>
|
2023-10-05 10:19:37 +02:00
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
|
#include <boost/asio/steady_timer.hpp>
|
2023-12-06 08:25:12 +01:00
|
|
|
#include <boost/asio/use_awaitable.hpp>
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
|
#include <async_mqtt5.hpp>
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
#include "test_common/message_exchange.hpp"
|
|
|
|
|
#include "test_common/test_service.hpp"
|
|
|
|
|
#include "test_common/test_stream.hpp"
|
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
using namespace async_mqtt5;
|
|
|
|
|
|
|
|
|
|
namespace async_mqtt5::test {
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
enum operation_type {
|
|
|
|
|
async_run = 1,
|
|
|
|
|
publish,
|
|
|
|
|
receive,
|
|
|
|
|
subscribe,
|
2024-02-22 10:26:47 +01:00
|
|
|
unsubscribe,
|
|
|
|
|
disconnect
|
2024-01-19 13:45:09 +01:00
|
|
|
};
|
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
enum cancel_type {
|
2024-02-06 13:40:05 +01:00
|
|
|
client_cancel = 1,
|
2024-01-09 15:18:58 +01:00
|
|
|
signal_emit
|
2023-10-05 10:19:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5::test
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
using stream_type = asio::ip::tcp::socket;
|
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
|
|
|
|
std::enable_if_t<op_type == test::operation_type::async_run, bool> = true
|
|
|
|
|
>
|
|
|
|
|
void setup_cancel_op_test_case(
|
|
|
|
|
client_type& c, asio::cancellation_signal& signal, int& handlers_called
|
|
|
|
|
) {
|
|
|
|
|
c.async_run(
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
2024-01-19 13:45:09 +01:00
|
|
|
signal.slot(),
|
2024-01-09 15:18:58 +01:00
|
|
|
[&handlers_called](error_code ec) {
|
2024-02-06 13:40:05 +01:00
|
|
|
++handlers_called;
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
2024-01-09 15:18:58 +01:00
|
|
|
}
|
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
2024-01-19 13:45:09 +01:00
|
|
|
}
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
|
|
|
|
std::enable_if_t<op_type == test::operation_type::publish, 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_publish<qos_e::at_most_once>("topic", "payload", retain_e::no, publish_props {},
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
2024-01-19 13:45:09 +01:00
|
|
|
signal.slot(),
|
|
|
|
|
[&handlers_called](error_code ec) {
|
2024-02-06 13:40:05 +01:00
|
|
|
++handlers_called;
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
2024-01-09 15:18:58 +01:00
|
|
|
}
|
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
2024-01-19 13:45:09 +01:00
|
|
|
}
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
|
|
|
|
std::enable_if_t<op_type == test::operation_type::receive, 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_receive(
|
|
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
|
signal.slot(),
|
2024-02-22 10:26:47 +01:00
|
|
|
[&c, &handlers_called](
|
2024-01-19 13:45:09 +01:00
|
|
|
error_code ec, std::string t, std::string p, publish_props
|
|
|
|
|
) {
|
2024-02-06 13:40:05 +01:00
|
|
|
++handlers_called;
|
|
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
|
|
|
|
BOOST_TEST(t == "");
|
|
|
|
|
BOOST_TEST(p == "");
|
2024-02-22 10:26:47 +01:00
|
|
|
|
|
|
|
|
// right now, emitting a terminal signal on async_receive
|
|
|
|
|
// does NOT cancel the client
|
|
|
|
|
c.cancel();
|
2024-01-19 13:45:09 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
2024-02-22 10:26:47 +01:00
|
|
|
std::enable_if_t<op_type == test::operation_type::subscribe, bool> = true
|
2024-01-19 13:45:09 +01:00
|
|
|
>
|
|
|
|
|
void setup_cancel_op_test_case(
|
|
|
|
|
client_type& c, asio::cancellation_signal& signal, int& handlers_called
|
|
|
|
|
) {
|
|
|
|
|
c.async_run(asio::detached);
|
2024-02-22 10:26:47 +01:00
|
|
|
c.async_subscribe(
|
|
|
|
|
subscribe_topic { "topic", subscribe_options {} }, subscribe_props {},
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
2024-01-19 13:45:09 +01:00
|
|
|
signal.slot(),
|
|
|
|
|
[&handlers_called](
|
2024-02-22 10:26:47 +01:00
|
|
|
error_code ec, std::vector<reason_code> rcs, suback_props
|
2024-01-19 13:45:09 +01:00
|
|
|
) {
|
2024-02-06 13:40:05 +01:00
|
|
|
++handlers_called;
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
|
|
|
|
BOOST_TEST_REQUIRE(rcs.size() == 1u);
|
|
|
|
|
BOOST_TEST(rcs[0] == reason_codes::empty);
|
2024-01-19 13:45:09 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
2024-02-22 10:26:47 +01:00
|
|
|
std::enable_if_t<op_type == test::operation_type::unsubscribe, bool > = true
|
2024-01-19 13:45:09 +01:00
|
|
|
>
|
|
|
|
|
void setup_cancel_op_test_case(
|
|
|
|
|
client_type& c, asio::cancellation_signal& signal, int& handlers_called
|
|
|
|
|
) {
|
|
|
|
|
c.async_run(asio::detached);
|
2024-02-22 10:26:47 +01:00
|
|
|
c.async_unsubscribe(
|
|
|
|
|
"topic" ,unsubscribe_props {},
|
2024-01-19 13:45:09 +01:00
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
|
signal.slot(),
|
|
|
|
|
[&handlers_called](
|
2024-02-22 10:26:47 +01:00
|
|
|
error_code ec, std::vector<reason_code> rcs, unsuback_props
|
2024-01-19 13:45:09 +01:00
|
|
|
) {
|
2024-02-06 13:40:05 +01:00
|
|
|
++handlers_called;
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
|
|
|
|
BOOST_TEST_REQUIRE(rcs.size() == 1u);
|
|
|
|
|
BOOST_TEST(rcs[0] == reason_codes::empty);
|
2024-01-09 15:18:58 +01:00
|
|
|
}
|
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
2024-01-19 13:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 10:26:47 +01:00
|
|
|
template <
|
|
|
|
|
test::operation_type op_type,
|
|
|
|
|
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_disconnect(
|
|
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
|
signal.slot(),
|
|
|
|
|
[&handlers_called](error_code ec) {
|
|
|
|
|
++handlers_called;
|
|
|
|
|
BOOST_TEST(ec == asio::error::operation_aborted);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
template<test::cancel_type c_type, test::operation_type op_type>
|
|
|
|
|
void run_cancel_op_test() {
|
|
|
|
|
using namespace test;
|
|
|
|
|
|
2024-02-06 13:40:05 +01:00
|
|
|
constexpr int expected_handlers_called = 1;
|
2024-01-19 13:45:09 +01:00
|
|
|
int handlers_called = 0;
|
|
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
2024-02-06 13:40:05 +01:00
|
|
|
asio::cancellation_signal signal;
|
2024-02-14 07:59:27 +01:00
|
|
|
client_type c(ioc);
|
2024-01-19 13:45:09 +01:00
|
|
|
c.brokers("127.0.0.1");
|
|
|
|
|
|
|
|
|
|
setup_cancel_op_test_case<op_type>(c, signal, handlers_called);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
|
asio::steady_timer timer(c.get_executor());
|
2024-02-06 13:40:05 +01:00
|
|
|
timer.expires_after(std::chrono::milliseconds(100));
|
2024-02-15 13:57:22 +01:00
|
|
|
timer.async_wait([&](error_code) {
|
2024-02-06 13:40:05 +01:00
|
|
|
if constexpr (c_type == client_cancel)
|
2023-10-05 10:19:37 +02:00
|
|
|
c.cancel();
|
2024-01-19 13:45:09 +01:00
|
|
|
else if constexpr (c_type == signal_emit)
|
|
|
|
|
signal.emit(asio::cancellation_type_t::terminal);
|
2023-10-05 10:19:37 +02:00
|
|
|
});
|
|
|
|
|
|
2024-02-06 13:40:05 +01:00
|
|
|
ioc.run();
|
|
|
|
|
BOOST_TEST(handlers_called == expected_handlers_called);
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(cancellation/*, *boost::unit_test::disabled()*/)
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_run) {
|
|
|
|
|
run_cancel_op_test<test::client_cancel, test::async_run>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_run) {
|
|
|
|
|
run_cancel_op_test<test::signal_emit, test::async_run>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_publish) {
|
|
|
|
|
run_cancel_op_test<test::client_cancel, test::publish>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_publish) {
|
|
|
|
|
run_cancel_op_test<test::signal_emit, test::publish>();
|
|
|
|
|
}
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_receive) {
|
2024-01-19 13:45:09 +01:00
|
|
|
run_cancel_op_test<test::client_cancel, test::receive>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 10:26:47 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_receive) {
|
2024-01-19 13:45:09 +01:00
|
|
|
run_cancel_op_test<test::signal_emit, test::receive>();
|
2024-01-09 15:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_subscribe) {
|
|
|
|
|
run_cancel_op_test<test::client_cancel, test::subscribe>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_subscribe) {
|
|
|
|
|
run_cancel_op_test<test::signal_emit, test::subscribe>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_unsubscribe) {
|
|
|
|
|
run_cancel_op_test<test::client_cancel, test::unsubscribe>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_unsubscribe) {
|
|
|
|
|
run_cancel_op_test<test::signal_emit, test::unsubscribe>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 10:26:47 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_disconnect) {
|
|
|
|
|
run_cancel_op_test<test::signal_emit, test::disconnect>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
struct shared_test_data {
|
|
|
|
|
error_code success {};
|
|
|
|
|
error_code fail = asio::error::not_connected;
|
|
|
|
|
|
|
|
|
|
const std::string connect = encoders::encode_connect(
|
2024-02-01 07:53:48 +01:00
|
|
|
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
|
2024-01-19 13:45:09 +01:00
|
|
|
);
|
|
|
|
|
const std::string connack = encoders::encode_connack(
|
|
|
|
|
false, reason_codes::success.value(), {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const std::string topic = "topic";
|
|
|
|
|
const std::string payload = "payload";
|
2024-02-23 10:48:21 +01:00
|
|
|
const publish_props pub_props {};
|
2024-01-19 13:45:09 +01:00
|
|
|
|
|
|
|
|
const std::string publish_qos1 = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::no, {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const std::string puback = encoders::encode_puback(1, uint8_t(0x00), {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using test::after;
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
|
|
|
|
|
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
|
|
|
|
|
|
2024-02-13 10:31:18 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(rerunning_the_client, shared_test_data) {
|
2024-01-25 13:43:59 +01:00
|
|
|
// packets
|
|
|
|
|
auto disconnect = encoders::encode_disconnect(uint8_t(0x00), {});
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(connack, after(2ms))
|
|
|
|
|
.expect(publish_qos1)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(puback, after(2ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(connack, after(2ms))
|
|
|
|
|
.expect(publish_qos1)
|
|
|
|
|
.complete_with(success, after(1ms))
|
2024-01-25 13:43:59 +01:00
|
|
|
.reply_with(puback, after(2ms))
|
|
|
|
|
.expect(disconnect);
|
2024-01-19 13:45:09 +01:00
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
asio::io_context ioc;
|
2024-01-19 13:45:09 +01:00
|
|
|
auto executor = ioc.get_executor();
|
|
|
|
|
auto& broker = asio::make_service<test::test_broker>(
|
|
|
|
|
ioc, executor, std::move(broker_side)
|
|
|
|
|
);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
co_spawn(ioc,
|
2024-01-19 13:45:09 +01:00
|
|
|
[&]() -> asio::awaitable<void> {
|
2024-02-14 07:59:27 +01:00
|
|
|
mqtt_client<test::test_stream> c(ioc);
|
2024-01-19 13:45:09 +01:00
|
|
|
c.brokers("127.0.0.1,127.0.0.1", 1883) // to avoid reconnect backoff
|
2024-01-16 13:04:21 +01:00
|
|
|
.async_run(asio::detached);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-02-19 11:52:36 +01:00
|
|
|
// Note: Older versions of GCC compilers may not handle temporaries
|
|
|
|
|
// correctly in co_await expressions.
|
|
|
|
|
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98401)
|
2024-01-19 13:45:09 +01:00
|
|
|
auto [ec, rc, props] = co_await c.async_publish<qos_e::at_least_once>(
|
2024-02-19 11:52:36 +01:00
|
|
|
topic, payload, retain_e::no, pub_props, use_nothrow_awaitable
|
2023-11-24 11:50:47 +01:00
|
|
|
);
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(!ec);
|
|
|
|
|
BOOST_TEST(!rc);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
c.cancel();
|
|
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
auto [cec, crc, cprops] = co_await c.async_publish<qos_e::at_least_once>(
|
2024-02-19 11:52:36 +01:00
|
|
|
topic, payload, retain_e::no, pub_props, use_nothrow_awaitable
|
2023-11-24 11:50:47 +01:00
|
|
|
);
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(cec == asio::error::operation_aborted);
|
|
|
|
|
BOOST_TEST(crc == reason_codes::empty);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-16 13:04:21 +01:00
|
|
|
c.async_run(asio::detached);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-19 13:45:09 +01:00
|
|
|
auto [rec, rrc, rprops] = co_await c.async_publish<qos_e::at_least_once>(
|
2024-02-19 11:52:36 +01:00
|
|
|
topic, payload, retain_e::no, pub_props, use_nothrow_awaitable
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(!rec);
|
|
|
|
|
BOOST_TEST(!rrc);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
co_await c.async_disconnect(use_nothrow_awaitable);
|
|
|
|
|
co_return;
|
|
|
|
|
},
|
|
|
|
|
asio::detached
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ioc.run();
|
2024-02-05 07:53:47 +01:00
|
|
|
BOOST_TEST(broker.received_all_expected());
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END();
|