Skip to content

Commit

Permalink
Consistently use BOOST_TEST instead of BOOST_CHECK in tests
Browse files Browse the repository at this point in the history
Summary:
related to T13767
We used BOOST_CHECK in older tests and BOOST_TEST macro in newer tests.
Now we use BOOST_TEST consistently.

Reviewers: ivica!

Subscribers: iljazovic, miljen

Differential Revision: https://repo.mireo.local/D31701
  • Loading branch information
ksimicevic committed Oct 8, 2024
1 parent 4cbae10 commit 0e62d8f
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 379 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Using the library

1. Download [Boost](https://www.boost.org/users/download/), and add it to your include path.
2. If you use SSL, download [OpenSSL](https://www.openssl.org/), link the library and add it to your include path.
3. Additionally, you can add Async.MQTT5's `include` folder to your include path.
3. Add Async.MQTT5's `include` folder to your include path.

You can compile the example below with the following command line on Linux:

Expand Down
23 changes: 2 additions & 21 deletions example/hello_world_over_websocket_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,10 @@
#include <boost/asio/ip/tcp.hpp>

#include <boost/beast/websocket.hpp>
#include <boost/beast/ssl/ssl_stream.hpp> // async_teardown specialization for websocket ssl stream

#include <async_mqtt5.hpp>

namespace boost::beast::websocket {

// boost::beast::websocket::async_teardown is a free function designed to initiate the asynchronous teardown of a connection.
// The specific behaviour of this function is based on the NextLayer type (Socket type) used to create the ``__WEBSOCKET_STREAM__``.
// ``__Beast__`` library includes an implementation of this function for ``__TCP_SOCKET__``.
// However, the callers are responsible for providing a suitable overload of this function for any other type,
// such as ``__SSL_STREAM__`` as shown in this example.
// See ``__BEAST_ASYNC_TEARDOWN__`` for more information.
template <typename TeardownHandler>
void async_teardown(
boost::beast::role_type role,
asio::ssl::stream<asio::ip::tcp::socket>& stream,
TeardownHandler&& handler
) {
return stream.async_shutdown(std::forward<TeardownHandler>(handler));
}

} // end namespace boost::beast::websocket


// External customization point.
namespace async_mqtt5 {

Expand All @@ -51,7 +32,7 @@ struct tls_handshake_type<boost::asio::ssl::stream<StreamBase>> {
template <typename StreamBase>
void assign_tls_sni(
const authority_path& ap,
boost::asio::ssl::context& ctx,
boost::asio::ssl::context& /*ctx*/,
boost::asio::ssl::stream<StreamBase>& stream
) {
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
Expand Down
28 changes: 0 additions & 28 deletions test/include/test_common/protocol_logging.hpp

This file was deleted.

31 changes: 18 additions & 13 deletions test/include/test_common/test_broker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#ifndef ASYNC_MQTT5_TEST_TEST_BROKER_HPP
#define ASYNC_MQTT5_TEST_TEST_BROKER_HPP

#include <boost/test/unit_test.hpp>
#include <boost/test/tools/interface.hpp>

#include <algorithm>
#include <cstdint>
#include <deque>
#include <memory>
#include <numeric>
#include <string>
Expand All @@ -17,18 +23,21 @@

#include <boost/asio/any_completion_handler.hpp>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/cancellation_signal.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/prepend.hpp>

#include <boost/asio/ip/tcp.hpp>
#include <boost/system/error_code.hpp>

#include <async_mqtt5/types.hpp>
#include <async_mqtt5/impl/codecs/message_decoders.hpp>

#include "test_common/message_exchange.hpp"
#include "test_common/packet_util.hpp"
#include "test_common/protocol_logging.hpp"


namespace async_mqtt5::test {

Expand Down Expand Up @@ -157,16 +166,15 @@ class test_broker : public asio::execution_context::service {
size_t buffers_size = std::distance(
asio::buffer_sequence_begin(buffers), asio::buffer_sequence_end(buffers)
);
BOOST_CHECK_EQUAL(buffers_size, expected.size());
BOOST_TEST(buffers_size == expected.size());

size_t num_packets = std::min(buffers_size, expected.size());
auto it = asio::buffer_sequence_begin(buffers);
for (size_t i = 0; i < num_packets; ++i, ++it) {
BOOST_CHECK_EQUAL(it->size(), expected[i].size());
BOOST_TEST(it->size() == expected[i].size());
size_t len = std::min(it->size(), expected[i].size());
if (memcmp(it->data(), expected[i].data(), len))
BOOST_CHECK_MESSAGE(
false,
BOOST_TEST_MESSAGE(
concat_strings(
"Packet mismatch!\nExpected: ",
to_readable_packet(expected[i]),
Expand All @@ -176,12 +184,9 @@ class test_broker : public asio::execution_context::service {
);
}
} else
BOOST_CHECK_MESSAGE(
false,
concat_strings(
"Broker side did not expect: ",
boost::algorithm::join(to_readable_packets(buffers), ",")
)
BOOST_TEST_MESSAGE(
"Broker side did not expect: " <<
boost::algorithm::join(to_readable_packets(buffers), ",")
);

auto complete_op = reply_action ?
Expand Down
1 change: 1 addition & 0 deletions test/integration/async_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/detached.hpp>

#include <async_mqtt5/mqtt_client.hpp>
#include <async_mqtt5/types.hpp>
Expand Down
14 changes: 1 addition & 13 deletions test/integration/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,12 @@
#include <boost/asio/ssl.hpp>

#include <boost/beast/websocket.hpp>
#include <boost/beast/ssl/ssl_stream.hpp> // async_teardown specialization for websocket ssl stream

#include <boost/system/error_code.hpp>

#include <async_mqtt5.hpp>

namespace boost::beast::websocket {

template <typename TeardownHandler>
void async_teardown(
boost::beast::role_type /* role */,
asio::ssl::stream<asio::ip::tcp::socket>& stream,
TeardownHandler&& handler
) {
return stream.async_shutdown(std::forward<TeardownHandler>(handler));
}

} // end namespace boost::beast::websocket

namespace async_mqtt5 {

template <typename StreamBase>
Expand Down
2 changes: 1 addition & 1 deletion test/integration/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void run_test(
});

ioc.run();
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(broker.received_all_expected());
}

BOOST_FIXTURE_TEST_CASE(ping_pong_client_ka, shared_test_data) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/re_authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void run_test(
});

ioc.run();
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(broker.received_all_expected());
}

BOOST_FIXTURE_TEST_CASE(successful_re_auth, shared_test_data) {
Expand Down
32 changes: 16 additions & 16 deletions test/integration/sub_unsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ void run_test(test::msg_exchange broker_side) {
[&handlers_called, &c](error_code ec, std::vector<reason_code> rcs, suback_props) {
++handlers_called;

BOOST_CHECK(!ec);
BOOST_ASSERT(rcs.size() == 1);
BOOST_CHECK_EQUAL(rcs[0], reason_codes::granted_qos_0);
BOOST_TEST(!ec);
BOOST_TEST_REQUIRE(rcs.size() == 1);
BOOST_TEST(rcs[0] == reason_codes::granted_qos_0);

c.cancel();
}
Expand All @@ -106,17 +106,17 @@ void run_test(test::msg_exchange broker_side) {
[&handlers_called, &c](error_code ec, std::vector<reason_code> rcs, unsuback_props) {
++handlers_called;

BOOST_CHECK(!ec);
BOOST_ASSERT(rcs.size() == 1);
BOOST_CHECK_EQUAL(rcs[0], reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST_REQUIRE(rcs.size() == 1);
BOOST_TEST(rcs[0] == reason_codes::success);

c.cancel();
}
);

ioc.run_for(5s);
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}

// subscribe
Expand Down Expand Up @@ -423,9 +423,9 @@ void run_cancellation_test(test::msg_exchange broker_side) {
[&handlers_called, &c](error_code ec, std::vector<reason_code> rcs, suback_props) {
++handlers_called;

BOOST_CHECK(ec == asio::error::operation_aborted);
BOOST_ASSERT(rcs.size() == 1);
BOOST_CHECK_EQUAL(rcs[0], reason_codes::empty);
BOOST_TEST(ec == asio::error::operation_aborted);
BOOST_TEST_REQUIRE(rcs.size() == 1);
BOOST_TEST(rcs[0] == reason_codes::empty);

c.cancel();
}
Expand All @@ -439,9 +439,9 @@ void run_cancellation_test(test::msg_exchange broker_side) {
[&handlers_called, &c](error_code ec, std::vector<reason_code> rcs, unsuback_props) {
++handlers_called;

BOOST_CHECK(ec == asio::error::operation_aborted);
BOOST_ASSERT(rcs.size() == 1);
BOOST_CHECK_EQUAL(rcs[0], reason_codes::empty);
BOOST_TEST(ec == asio::error::operation_aborted);
BOOST_TEST_REQUIRE(rcs.size() == 1);
BOOST_TEST(rcs[0] == reason_codes::empty);

c.cancel();
}
Expand All @@ -451,8 +451,8 @@ void run_cancellation_test(test::msg_exchange broker_side) {
cancel_signal.emit(asio::cancellation_type::total);

ioc.run_for(2s);
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}

BOOST_FIXTURE_TEST_CASE(cancel_resending_subscribe, shared_test_data) {
Expand Down
3 changes: 0 additions & 3 deletions test/src/run_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

#include <boost/test/included/unit_test.hpp>

#include <test_common/protocol_logging.hpp>

boost::unit_test::test_suite* init_tests(
int /*argc*/, char* /*argv*/[]
) {
async_mqtt5::test::logging_enabled() = false;
return nullptr;
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/connect_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ void run_unit_test(
).perform(eps, ap);

ioc.run_for(1s);
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}

void run_unit_test(
Expand Down Expand Up @@ -121,7 +121,7 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_connect, shared_test_data) {
.complete_with(fail, after(2ms));

auto handler = [&](error_code ec) {
BOOST_CHECK(ec == fail);
BOOST_TEST(ec == fail);
};

run_unit_test(std::move(broker_side), std::move(handler));
Expand Down
Loading

0 comments on commit 0e62d8f

Please sign in to comment.