async_run

Reviewers: ivica

Reviewed By: ivica

Subscribers: korina

Differential Revision: https://repo.mireo.local/D27342
This commit is contained in:
Bruno Iljazovic committed 2024-01-16 13:18:21 +01:00
1 parent 2f6751d162
commit 4f87b27861
29 files changed
+398 -85

No files matched your search

+48 -4
View File
@@ -37,7 +37,7 @@ void cancel_async_receive() {
c.brokers("127.0.0.1", 1883)
.credentials("test-cli", "", "")
.run();
.async_run(asio::detached);
auto handler = [&handlers_called](
error_code ec, std::string, std::string, publish_props
@@ -89,7 +89,7 @@ void cancel_async_publish() {
c.brokers("127.0.0.1", 1883)
.credentials("test-cli", "", "")
.run();
.async_run(asio::detached);
std::vector<asio::cancellation_signal> signals(3);
@@ -177,6 +177,50 @@ BOOST_AUTO_TEST_CASE(signal_emit_async_publish) {
cancel_async_publish<test::cancel_type::signal_emit>();
}
BOOST_AUTO_TEST_CASE(signal_emit_async_run) {
using namespace test;
constexpr int expected_handlers_called = 2;
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, "");
asio::cancellation_signal signal;
c.brokers("127.0.0.1", 1883)
.credentials("test-cli", "", "")
.async_run(
asio::bind_cancellation_slot(
signal.slot(),
[&handlers_called](error_code ec) {
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
handlers_called++;
}
)
);
c.async_publish<qos_e::at_most_once>(
"topic", "payload", retain_e::yes, {},
[&handlers_called](error_code ec) {
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
handlers_called++;
}
);
asio::steady_timer timer(c.get_executor());
timer.expires_after(std::chrono::milliseconds(10));
timer.async_wait([&](auto) {
signal.emit(asio::cancellation_type::terminal);
});
ioc.run_for(std::chrono::milliseconds(20));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
}
#ifdef BOOST_ASIO_HAS_CO_AWAIT
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
@@ -191,7 +235,7 @@ BOOST_AUTO_TEST_CASE(rerunning_the_client) {
client_type c(ioc, "");
c.brokers("broker.hivemq.com,broker.hivemq.com", 1883) // to avoid reconnect backoff
.run();
.async_run(asio::detached);
auto [ec] = co_await c.async_publish<qos_e::at_most_once>(
"t", "p", retain_e::yes, publish_props {}, use_nothrow_awaitable
@@ -205,7 +249,7 @@ BOOST_AUTO_TEST_CASE(rerunning_the_client) {
);
BOOST_CHECK(cec == asio::error::operation_aborted);
c.run();
c.async_run(asio::detached);
auto [rec] = co_await c.async_publish<qos_e::at_most_once>(
"ct", "cp", retain_e::yes, publish_props {}, use_nothrow_awaitable