From ae6a066ca5f7de4aa05eafbd3f2f266926a8e26c Mon Sep 17 00:00:00 2001 From: Wouter van Kleunen Date: Tue, 8 Dec 2020 20:39:44 +0100 Subject: [PATCH] System test broker with crash --- test/system/CMakeLists.txt | 1 + test/system/st_broker.cpp | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/system/st_broker.cpp diff --git a/test/system/CMakeLists.txt b/test/system/CMakeLists.txt index 1d5a1ab89..7f0cbd21a 100644 --- a/test/system/CMakeLists.txt +++ b/test/system/CMakeLists.txt @@ -59,6 +59,7 @@ ENDIF () IF (MQTT_TEST_7) LIST (APPEND check_PROGRAMS st_async_pubsub_2.cpp + st_broker st_broker_offline_message.cpp st_remaining_length.cpp st_utf8string_validate.cpp diff --git a/test/system/st_broker.cpp b/test/system/st_broker.cpp new file mode 100644 index 000000000..7b00e17b3 --- /dev/null +++ b/test/system/st_broker.cpp @@ -0,0 +1,77 @@ +// Copyright Takatoshi Kondo 2020 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include "../common/test_main.hpp" +#include "combi_test.hpp" +#include "checker.hpp" +#include "test_util.hpp" +#include "../common/global_fixture.hpp" + +BOOST_AUTO_TEST_SUITE(st_broker) + +using namespace MQTT_NS::literals; + +BOOST_AUTO_TEST_CASE( broker_bug ) { + + boost::asio::io_context iocb; + MQTT_NS::broker::broker_t b(iocb); + MQTT_NS::optional s; + + std::thread th( + [&] { + s.emplace(iocb, b); + iocb.run(); + } + ); + + std::vector> client_th; + + std::size_t num_clients = 10; + + auto client_thread_func = + [] { + boost::asio::io_context ioc; + + int publish_count = 100; + + auto c1 = MQTT_NS::make_client(ioc, broker_url, broker_notls_port); + c1->set_clean_session(true); + c1->set_client_id("cid1"); + + c1->set_connack_handler( + [&c1, &publish_count] + (bool /*sp*/, MQTT_NS::connect_return_code /*connack_return_code*/) { + std::cout << "Publish: " << publish_count << std::endl; + for (std::size_t i = 0; i != 100; ++i) { + c1->publish("topic1", "topic1_contents1", MQTT_NS::qos::at_most_once); + } + return true; + } + ); + + c1->connect(); + ioc.run(); + }; + + for (unsigned int i = 0; i != num_clients; ++i) { + client_th.push_back(std::make_shared(client_thread_func)); + } + + for(auto& th: client_th) { + th->join(); + } + + as::post( + iocb, + [&] { + s->close(); + } + ); + + th.join(); +} + +BOOST_AUTO_TEST_SUITE_END()