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>
|
|
|
|
#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>
|
|
|
|
|
|
|
|
using namespace async_mqtt5;
|
|
|
|
|
|
|
|
namespace async_mqtt5::test {
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
enum cancel_type {
|
2023-10-05 10:19:37 +02:00
|
|
|
ioc_stop = 1,
|
2024-01-09 15:18:58 +01:00
|
|
|
client_cancel,
|
|
|
|
signal_emit
|
2023-10-05 10:19:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5::test
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
template <test::cancel_type type>
|
2023-10-05 10:19:37 +02:00
|
|
|
void cancel_async_receive() {
|
2023-12-06 08:25:12 +01:00
|
|
|
using namespace test;
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
constexpr int num_handlers = 3;
|
|
|
|
constexpr int expected_handlers_called = type == ioc_stop ? 0 : num_handlers;
|
|
|
|
int handlers_called = 0;
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = asio::ip::tcp::socket;
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
client_type c(ioc, "");
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
c.brokers("127.0.0.1", 1883)
|
|
|
|
.credentials("test-cli", "", "")
|
2023-10-05 10:19:37 +02:00
|
|
|
.run();
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
auto handler = [&handlers_called](
|
|
|
|
error_code ec, std::string, std::string, publish_props
|
|
|
|
) {
|
|
|
|
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
|
|
|
|
handlers_called++;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<asio::cancellation_signal> signals(3);
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
for (auto i = 0; i < num_handlers; ++i)
|
2024-01-09 15:18:58 +01:00
|
|
|
c.async_receive(asio::bind_cancellation_slot(
|
|
|
|
signals[i].slot(),
|
|
|
|
std::move(handler)
|
|
|
|
));
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
asio::steady_timer timer(c.get_executor());
|
2024-01-09 15:18:58 +01:00
|
|
|
timer.expires_after(std::chrono::milliseconds(10));
|
2023-10-05 10:19:37 +02:00
|
|
|
timer.async_wait([&](auto) {
|
|
|
|
if constexpr (type == ioc_stop)
|
|
|
|
ioc.stop();
|
2024-01-09 15:18:58 +01:00
|
|
|
else if constexpr (type == client_cancel)
|
2023-10-05 10:19:37 +02:00
|
|
|
c.cancel();
|
2024-01-09 15:18:58 +01:00
|
|
|
else if constexpr (type == signal_emit)
|
|
|
|
std::for_each(
|
|
|
|
signals.begin(), signals.end(),
|
|
|
|
[](auto& signal) {
|
|
|
|
signal.emit(asio::cancellation_type_t::terminal);
|
|
|
|
}
|
|
|
|
);
|
2023-10-05 10:19:37 +02:00
|
|
|
});
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
ioc.run_for(std::chrono::milliseconds(20));
|
2023-11-29 11:50:07 +01:00
|
|
|
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
template <test::cancel_type type>
|
2023-10-05 10:19:37 +02:00
|
|
|
void cancel_async_publish() {
|
2023-12-06 08:25:12 +01:00
|
|
|
using namespace test;
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
constexpr int expected_handlers_called = type == ioc_stop ? 0 : 3;
|
|
|
|
int handlers_called = 0;
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = asio::ip::tcp::socket;
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
client_type c(ioc, "");
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
c.brokers("127.0.0.1", 1883)
|
|
|
|
.credentials("test-cli", "", "")
|
2023-10-05 10:19:37 +02:00
|
|
|
.run();
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
std::vector<asio::cancellation_signal> signals(3);
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
c.async_publish<qos_e::at_most_once>(
|
|
|
|
"topic", "payload", retain_e::yes, {},
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
signals[0].slot(),
|
|
|
|
[&handlers_called](error_code ec) {
|
|
|
|
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
|
|
|
|
handlers_called++;
|
|
|
|
}
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
c.async_publish<qos_e::at_least_once>(
|
|
|
|
"topic", "payload", retain_e::yes, {},
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
signals[1].slot(),
|
|
|
|
[&handlers_called](error_code ec, reason_code rc, puback_props) {
|
|
|
|
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
|
|
|
|
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
|
|
|
|
handlers_called++;
|
|
|
|
}
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
c.async_publish<qos_e::exactly_once>(
|
|
|
|
"topic", "payload", retain_e::yes, {},
|
2024-01-09 15:18:58 +01:00
|
|
|
asio::bind_cancellation_slot(
|
|
|
|
signals[2].slot(),
|
|
|
|
[&handlers_called](error_code ec, reason_code rc, pubcomp_props) {
|
|
|
|
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
|
|
|
|
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
|
|
|
|
handlers_called++;
|
|
|
|
}
|
|
|
|
)
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
asio::steady_timer timer(c.get_executor());
|
2024-01-09 15:18:58 +01:00
|
|
|
timer.expires_after(std::chrono::milliseconds(10));
|
2023-10-05 10:19:37 +02:00
|
|
|
timer.async_wait([&](auto) {
|
|
|
|
if constexpr (type == ioc_stop)
|
|
|
|
ioc.stop();
|
2024-01-09 15:18:58 +01:00
|
|
|
else if constexpr (type == client_cancel)
|
2023-10-05 10:19:37 +02:00
|
|
|
c.cancel();
|
2024-01-09 15:18:58 +01:00
|
|
|
else if constexpr (type == signal_emit)
|
|
|
|
std::for_each(
|
|
|
|
signals.begin(), signals.end(),
|
|
|
|
[](auto& signal) {
|
|
|
|
signal.emit(asio::cancellation_type_t::terminal);
|
|
|
|
}
|
|
|
|
);
|
2023-10-05 10:19:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ioc.run();
|
2023-11-29 11:50:07 +01:00
|
|
|
BOOST_CHECK_EQUAL(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
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(ioc_stop_async_receive) {
|
2023-12-06 08:25:12 +01:00
|
|
|
cancel_async_receive<test::cancel_type::ioc_stop>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_receive) {
|
2023-12-06 08:25:12 +01:00
|
|
|
cancel_async_receive<test::cancel_type::client_cancel>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_receive) {
|
|
|
|
cancel_async_receive<test::cancel_type::signal_emit>();
|
|
|
|
}
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
// passes on debian, hangs on windows in io_context destructor
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(ioc_stop_async_publish, *boost::unit_test::disabled() ) {
|
2023-12-06 08:25:12 +01:00
|
|
|
cancel_async_publish<test::cancel_type::ioc_stop>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(client_cancel_async_publish) {
|
2023-12-06 08:25:12 +01:00
|
|
|
cancel_async_publish<test::cancel_type::client_cancel>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:18:58 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(signal_emit_async_publish) {
|
|
|
|
cancel_async_publish<test::cancel_type::signal_emit>();
|
2023-10-05 10:19:37 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(rerunning_the_client) {
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
co_spawn(ioc,
|
|
|
|
[&ioc]() -> asio::awaitable<void> {
|
|
|
|
using stream_type = asio::ip::tcp::socket;
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
client_type c(ioc, "");
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2024-01-16 10:25:05 +01:00
|
|
|
c.brokers("broker.hivemq.com,broker.hivemq.com", 1883) // to avoid reconnect backoff
|
2023-11-24 11:50:47 +01:00
|
|
|
.run();
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
auto [ec] = co_await c.async_publish<qos_e::at_most_once>(
|
|
|
|
"t", "p", retain_e::yes, publish_props {}, use_nothrow_awaitable
|
|
|
|
);
|
|
|
|
BOOST_CHECK(!ec);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
c.cancel();
|
|
|
|
|
|
|
|
auto [cec] = co_await c.async_publish<qos_e::at_most_once>(
|
|
|
|
"ct", "cp", retain_e::yes, publish_props {}, use_nothrow_awaitable
|
|
|
|
);
|
|
|
|
BOOST_CHECK(cec == asio::error::operation_aborted);
|
2023-10-05 10:19:37 +02:00
|
|
|
|
|
|
|
c.run();
|
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
auto [rec] = co_await c.async_publish<qos_e::at_most_once>(
|
|
|
|
"ct", "cp", retain_e::yes, publish_props {}, use_nothrow_awaitable
|
2023-10-05 10:19:37 +02:00
|
|
|
);
|
2023-11-24 11:50:47 +01:00
|
|
|
BOOST_CHECK(!rec);
|
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();
|
|
|
|
}
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
#endif
|
|
|
|
|
2023-10-05 10:19:37 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END();
|