forked from boostorg/mqtt5
Async.MQTT5 -> Boost.MQTT5
Summary: related to T15996 folder structure include/async_mqtt5 -> include/boost/mqtt5 namespace async_mqtt5 -> namespace boost::mqtt5 all tabs replaced with 4 spaces (because tabs are banned) boost-like order of includes TODO: fix all docs Reviewers: ivica Reviewed By: ivica Subscribers: iljazovic, miljen Differential Revision: https://repo.mireo.local/D33152
This commit is contained in:
1 parent
1225cc778a
commit
afc270f10e
136 files changed
+17834
-17859
No files matched your search
+111
-112
@@ -10,27 +10,26 @@
|
||||
#include <boost/asio/use_awaitable.hpp>
|
||||
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <boost/mqtt5.hpp>
|
||||
#include <boost/mqtt5/websocket.hpp>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <boost/asio/experimental/awaitable_operators.hpp>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/beast/websocket/stream.hpp>
|
||||
|
||||
#include <async_mqtt5.hpp>
|
||||
#include <async_mqtt5/websocket.hpp>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(mqtt_features/*, *boost::unit_test::disabled()*/)
|
||||
|
||||
using namespace async_mqtt5;
|
||||
using namespace boost::mqtt5;
|
||||
namespace asio = boost::asio;
|
||||
|
||||
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
|
||||
@@ -47,149 +46,149 @@ constexpr auto payload = "hello from async-mqtt5";
|
||||
|
||||
template <typename TestCase>
|
||||
void run_test(TestCase&& test_case) {
|
||||
using namespace asio::experimental::awaitable_operators;
|
||||
using namespace asio::experimental::awaitable_operators;
|
||||
|
||||
asio::io_context ioc;
|
||||
co_spawn(
|
||||
ioc,
|
||||
[&ioc, test_case = std::forward<TestCase>(test_case)]() -> asio::awaitable<void> {
|
||||
asio::steady_timer test_timer(ioc, test_duration);
|
||||
co_await(test_case() || test_timer.async_wait(use_nothrow_awaitable));
|
||||
},
|
||||
asio::detached
|
||||
);
|
||||
ioc.run();
|
||||
asio::io_context ioc;
|
||||
co_spawn(
|
||||
ioc,
|
||||
[&ioc, test_case = std::forward<TestCase>(test_case)]() -> asio::awaitable<void> {
|
||||
asio::steady_timer test_timer(ioc, test_duration);
|
||||
co_await(test_case() || test_timer.async_wait(use_nothrow_awaitable));
|
||||
},
|
||||
asio::detached
|
||||
);
|
||||
ioc.run();
|
||||
}
|
||||
|
||||
asio::awaitable<void> test_manual_use_topic_alias() {
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.connect_property(prop::topic_alias_maximum, uint16_t(10))
|
||||
.async_run(asio::detached);
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.connect_property(prop::topic_alias_maximum, uint16_t(10))
|
||||
.async_run(asio::detached);
|
||||
|
||||
asio::steady_timer connect_timer(ex, connect_wait_dur);
|
||||
co_await connect_timer.async_wait(use_nothrow_awaitable);
|
||||
asio::steady_timer connect_timer(ex, connect_wait_dur);
|
||||
co_await connect_timer.async_wait(use_nothrow_awaitable);
|
||||
|
||||
uint16_t topic_alias = 1;
|
||||
publish_props pprops;
|
||||
pprops[prop::topic_alias] = topic_alias;
|
||||
uint16_t topic_alias = 1;
|
||||
publish_props pprops;
|
||||
pprops[prop::topic_alias] = topic_alias;
|
||||
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
|
||||
auto&& [ec_2, rc_2, __] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
"", payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rc_2);
|
||||
auto&& [ec_2, rc_2, __] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
"", payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rc_2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(manual_use_topic_alias) {
|
||||
run_test(test_manual_use_topic_alias);
|
||||
run_test(test_manual_use_topic_alias);
|
||||
}
|
||||
|
||||
asio::awaitable<void> test_subscription_identifiers() {
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
|
||||
publish_props pprops;
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::yes, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
publish_props pprops;
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::yes, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
|
||||
int32_t sub_id = 123;
|
||||
subscribe_props sprops;
|
||||
sprops[prop::subscription_identifier] = sub_id;
|
||||
int32_t sub_id = 123;
|
||||
subscribe_props sprops;
|
||||
sprops[prop::subscription_identifier] = sub_id;
|
||||
|
||||
subscribe_options sub_opts = { .no_local = no_local_e::no };
|
||||
subscribe_topic sub_topic = { topic, sub_opts };
|
||||
auto&& [ec_2, rcs, __] = co_await client.async_subscribe(
|
||||
sub_topic, sprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rcs[0]);
|
||||
subscribe_options sub_opts = { .no_local = no_local_e::no };
|
||||
subscribe_topic sub_topic = { topic, sub_opts };
|
||||
auto&& [ec_2, rcs, __] = co_await client.async_subscribe(
|
||||
sub_topic, sprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rcs[0]);
|
||||
|
||||
auto&& [ec_3, rec_topic, rec_payload, rec_props] =
|
||||
co_await client.async_receive(use_nothrow_awaitable);
|
||||
BOOST_TEST_WARN(!ec_3);
|
||||
BOOST_TEST_WARN(rec_topic == topic);
|
||||
BOOST_TEST_WARN(rec_payload == payload);
|
||||
const auto& sub_ids = rec_props[prop::subscription_identifier];
|
||||
BOOST_TEST_WARN(!sub_ids.empty());
|
||||
if (!sub_ids.empty())
|
||||
BOOST_TEST_WARN(sub_ids[0] == sub_id);
|
||||
auto&& [ec_3, rec_topic, rec_payload, rec_props] =
|
||||
co_await client.async_receive(use_nothrow_awaitable);
|
||||
BOOST_TEST_WARN(!ec_3);
|
||||
BOOST_TEST_WARN(rec_topic == topic);
|
||||
BOOST_TEST_WARN(rec_payload == payload);
|
||||
const auto& sub_ids = rec_props[prop::subscription_identifier];
|
||||
BOOST_TEST_WARN(!sub_ids.empty());
|
||||
if (!sub_ids.empty())
|
||||
BOOST_TEST_WARN(sub_ids[0] == sub_id);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(subscription_identifiers) {
|
||||
run_test(test_subscription_identifiers);
|
||||
run_test(test_subscription_identifiers);
|
||||
}
|
||||
|
||||
asio::awaitable<void> test_shared_subscription() {
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
|
||||
subscribe_options sub_opts = { .no_local = no_local_e::no };
|
||||
subscribe_topic sub_topic = { share_topic, sub_opts };
|
||||
subscribe_props sprops;
|
||||
auto&& [ec_1, rcs, __] = co_await client.async_subscribe(
|
||||
sub_topic, sprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rcs[0]);
|
||||
subscribe_options sub_opts = { .no_local = no_local_e::no };
|
||||
subscribe_topic sub_topic = { share_topic, sub_opts };
|
||||
subscribe_props sprops;
|
||||
auto&& [ec_1, rcs, __] = co_await client.async_subscribe(
|
||||
sub_topic, sprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rcs[0]);
|
||||
|
||||
publish_props pprops;
|
||||
// shared subscriptions do not send Retained Messages on first subscribe
|
||||
auto&& [ec_2, rc_2, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rc_2);
|
||||
publish_props pprops;
|
||||
// shared subscriptions do not send Retained Messages on first subscribe
|
||||
auto&& [ec_2, rc_2, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_2);
|
||||
BOOST_TEST_WARN(!rc_2);
|
||||
|
||||
auto&& [ec_3, rec_topic, rec_payload, ___] =
|
||||
co_await client.async_receive(use_nothrow_awaitable);
|
||||
BOOST_TEST_WARN(!ec_3);
|
||||
BOOST_TEST_WARN(rec_topic == topic);
|
||||
BOOST_TEST_WARN(rec_payload == payload);
|
||||
auto&& [ec_3, rec_topic, rec_payload, ___] =
|
||||
co_await client.async_receive(use_nothrow_awaitable);
|
||||
BOOST_TEST_WARN(!ec_3);
|
||||
BOOST_TEST_WARN(rec_topic == topic);
|
||||
BOOST_TEST_WARN(rec_payload == payload);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(shared_subscription) {
|
||||
run_test(test_shared_subscription);
|
||||
run_test(test_shared_subscription);
|
||||
}
|
||||
|
||||
asio::awaitable<void> test_user_property() {
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
auto ex = co_await asio::this_coro::executor;
|
||||
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
mqtt_client<stream_type> client(ex);
|
||||
client.brokers(broker, 8000)
|
||||
.async_run(asio::detached);
|
||||
|
||||
publish_props pprops;
|
||||
pprops[prop::user_property].push_back({ "key_1", "value_1" });
|
||||
pprops[prop::user_property].push_back({ "key_2", "value_2" });
|
||||
pprops[prop::user_property].push_back({ "key_3", "value_3" });
|
||||
publish_props pprops;
|
||||
pprops[prop::user_property].push_back({ "key_1", "value_1" });
|
||||
pprops[prop::user_property].push_back({ "key_2", "value_2" });
|
||||
pprops[prop::user_property].push_back({ "key_3", "value_3" });
|
||||
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
auto&& [ec_1, rc_1, _] = co_await client.async_publish<qos_e::at_least_once>(
|
||||
topic, payload, retain_e::no, pprops, use_nothrow_awaitable
|
||||
);
|
||||
BOOST_TEST_WARN(!ec_1);
|
||||
BOOST_TEST_WARN(!rc_1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(user_property) {
|
||||
run_test(test_user_property);
|
||||
run_test(test_user_property);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in new issue
Block a user