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
+4 -4
View File
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(tcp_client_check) {
c.credentials("tcp-tester", "", "")
.brokers("broker.hivemq.com", 1883)
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
.run();
.async_run(asio::detached);
asio::steady_timer timer(ioc);
timer.expires_after(std::chrono::seconds(5));
@@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(websocket_tcp_client_check) {
c.brokers("broker.hivemq.com/mqtt", 8000)
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
.run();
.async_run(asio::detached);
asio::steady_timer timer(ioc);
timer.expires_after(std::chrono::seconds(5));
@@ -187,7 +187,7 @@ BOOST_AUTO_TEST_CASE(openssl_tls_client_check) {
c.brokers("broker.hivemq.com", 8883)
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
.run();
.async_run(asio::detached);
asio::steady_timer timer(ioc);
timer.expires_after(std::chrono::seconds(5));
@@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(websocket_tls_client_check) {
c.brokers("broker.hivemq.com/mqtt", 8884)
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
.run();
.async_run(asio::detached);
asio::steady_timer timer(ioc);
timer.expires_after(std::chrono::seconds(5));
+196
View File
@@ -0,0 +1,196 @@
#include <boost/test/unit_test.hpp>
#include <chrono>
#include <boost/asio/io_context.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <async_mqtt5/impl/codecs/message_encoders.hpp>
#include <async_mqtt5.hpp>
#include "test_common/message_exchange.hpp"
#include "test_common/test_stream.hpp"
using namespace async_mqtt5;
BOOST_AUTO_TEST_SUITE(executors)
BOOST_AUTO_TEST_CASE(async_run) {
using test::after;
using std::chrono_literals::operator ""ms;
constexpr int expected_handlers_called = 9;
int handlers_called = 0;
// packets
auto connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
);
auto connack = encoders::encode_connack(
false, reason_codes::success.value(), {}
);
auto publish_0 = encoders::encode_publish(
0, "t_0", "p_0", qos_e::at_most_once, retain_e::no, dup_e::no, {}
);
auto publish_1 = encoders::encode_publish(
1, "t_1", "p_1", qos_e::at_least_once, retain_e::no, dup_e::no, {}
);
auto puback = encoders::encode_puback(
1, reason_codes::success.value(), {}
);
auto publish_2 = encoders::encode_publish(
2, "t_2", "p_2", qos_e::exactly_once, retain_e::no, dup_e::no, {}
);
auto pubrec = encoders::encode_pubrec(
2, reason_codes::success.value(), {}
);
auto pubrel = encoders::encode_pubrel(
2, reason_codes::success.value(), {}
);
auto pubcomp = encoders::encode_pubcomp(
2, reason_codes::success.value(), {}
);
auto subscribe = encoders::encode_subscribe(
3, std::vector<subscribe_topic> { { "t_0", {} } }, {}
);
auto suback = encoders::encode_suback(
3, std::vector<uint8_t> { reason_codes::granted_qos_2.value() }, {}
);
auto unsubscribe = encoders::encode_unsubscribe(
1, std::vector<std::string> { "t_0" }, {}
);
auto unsuback = encoders::encode_unsuback(
1, std::vector<uint8_t> { reason_codes::success.value() }, {}
);
auto disconnect = encoders::encode_disconnect(
reason_codes::normal_disconnection.value(), {}
);
test::msg_exchange broker_side;
error_code success {};
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(0ms))
.expect(subscribe, publish_0, publish_1, publish_2)
.complete_with(success, after(0ms))
.reply_with(puback, pubrec, suback, after(0ms))
.expect(pubrel)
.complete_with(success, after(0ms))
.reply_with(pubcomp, after(0ms))
.send(publish_0, after(50ms))
.expect(unsubscribe)
.complete_with(success, after(0ms))
.reply_with(unsuback, after(0ms))
.expect(publish_1)
.complete_with(success, 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)
);
auto strand = asio::make_strand(ioc);
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.async_run(asio::bind_executor(
strand,
[&](error_code ec) {
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
));
c.async_publish<qos_e::at_most_once>(
"t_0", "p_0", retain_e::no, {},
[&](error_code ec) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
c.async_publish<qos_e::at_least_once>(
"t_1", "p_1", retain_e::no, {},
[&](error_code ec, reason_code rc, auto) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rc, rc.message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
c.async_publish<qos_e::exactly_once>(
"t_2", "p_2", retain_e::no, {},
[&](error_code ec, reason_code rc, auto) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rc, rc.message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
c.async_subscribe(
subscribe_topic { "t_0", {} }, {},
[&](error_code ec, std::vector<reason_code> rcs, auto) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rcs[0], rcs[0].message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
c.async_receive(
[&](
error_code ec,
std::string rec_topic, std::string rec_payload,
publish_props
) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL("t_0", rec_topic);
BOOST_CHECK_EQUAL("p_0", rec_payload);
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
c.async_unsubscribe(
"t_0", {},
[&](error_code ec, std::vector<reason_code> rcs, auto) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rcs[0], rcs[0].message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
c.async_publish<qos_e::at_least_once>(
"t_1", "p_1", retain_e::no, {},
[&](error_code ec, reason_code rc, auto) {
BOOST_CHECK_EQUAL(ec, asio::error::operation_aborted);
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
c.async_disconnect(
[&](error_code ec) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK(strand.running_in_this_thread());
++handlers_called;
}
);
}
);
}
);
ioc.run_for(500ms);
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected());
}
BOOST_AUTO_TEST_SUITE_END()
+4 -4
View File
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(test_malformed_publish) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
asio::steady_timer timer(c.get_executor());
timer.expires_after(std::chrono::seconds(2));
@@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(test_malformed_pubrel, *boost::unit_test::disabled()) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_receive(
[&](
@@ -205,7 +205,7 @@ BOOST_AUTO_TEST_CASE(malformed_puback) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
"t", "p", retain_e::no, publish_props {},
@@ -302,7 +302,7 @@ BOOST_AUTO_TEST_CASE(malformed_pubrec_pubcomp) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_publish<qos_e::exactly_once>(
"t", "p", retain_e::no, publish_props {},
+2 -2
View File
@@ -73,7 +73,7 @@ void receive_publish() {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.run();
.async_run(asio::detached);
c.async_receive(
[&](
@@ -164,7 +164,7 @@ BOOST_AUTO_TEST_CASE(test_waiting_on_pubrel) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_receive(
[&](
+3 -3
View File
@@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE(ordering_after_reconnect) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
"t_1", "p_1", retain_e::no, publish_props{},
@@ -172,7 +172,7 @@ BOOST_AUTO_TEST_CASE(throttling) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.run();
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
"t_1", "p_1", retain_e::no, publish_props{},
@@ -270,7 +270,7 @@ BOOST_AUTO_TEST_CASE(cancel_multiple_ops) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.run();
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
"t_1", "p_1", retain_e::no, publish_props{},
+2 -2
View File
@@ -57,7 +57,7 @@ void test_receive_malformed_packet(
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
asio::steady_timer timer(c.get_executor());
timer.expires_after(std::chrono::milliseconds(100));
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(receive_disconnect) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
asio::steady_timer timer(c.get_executor());
timer.expires_after(std::chrono::milliseconds(100));
+4 -4
View File
@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(resend_multiple_publishes) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
"t", "p_1", retain_e::no, publish_props{},
@@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(resend_pubrel) {
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_publish<qos_e::exactly_once>(
"t_1", "p_1", retain_e::no, publish_props{},
@@ -220,7 +220,7 @@ BOOST_AUTO_TEST_CASE(resend_subscribe) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_subscribe(
topics, subscribe_props,
@@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE(resend_unsubscribe) {
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
.run();
.async_run(asio::detached);
c.async_unsubscribe(
topics, unsubscribe_props,