Sanity tests related to number of packets, packet size & packet priority

Summary:
related to T12015
- add tests related to sending/receiving really big packets
- add tests related to receiving multiple packets at once
- send_big_publish fails in the testing environment, but it works fine in real life

Reviewers: ivica

Reviewed By: ivica

Subscribers: miljen, iljazovic

Differential Revision: https://repo.mireo.local/D27652
This commit is contained in:
Korina Šimičević
2024-02-01 10:35:18 +01:00
parent 33c8eea890
commit 0affdb76f4
7 changed files with 388 additions and 96 deletions

View File

@ -94,12 +94,16 @@ public:
);
}
void operator()(on_pingreq, error_code) {
void operator()(on_pingreq, error_code ec) {
get_cancellation_slot().clear();
if (_cancellation_state.cancelled() == asio::cancellation_type::terminal)
if (
_cancellation_state.cancelled() == asio::cancellation_type::terminal ||
ec == asio::error::no_recovery
)
return;
_cancellation_state.clear();
perform();
}

View File

@ -343,10 +343,8 @@ inline std::string to_readable_packet(std::string packet) {
case control_code_e::unsuback:
return detail::to_string<control_code_e::unsuback>(*varlen, begin);
default:
assert(false);
return "";
}
return {};
}
template <typename ConstBufferSequence>

View File

@ -1,6 +1,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <async_mqtt5/mqtt_client.hpp>
@ -16,6 +17,9 @@ struct shared_test_data {
error_code success {};
error_code fail = asio::error::not_connected;
const std::string topic = "topic";
const std::string payload = "payload";
const std::string connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
);
@ -23,8 +27,14 @@ struct shared_test_data {
false, reason_codes::success.value(), {}
);
const std::string topic = "topic";
const std::string payload = "payload";
const std::string publish_qos1 = encoders::encode_publish(
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::no, {}
);
const std::string publish_qos1_dup = encoders::encode_publish(
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::yes, {}
);
const std::string puback = encoders::encode_puback(1, uint8_t(0x00), {});
};
using test::after;
@ -35,18 +45,10 @@ BOOST_FIXTURE_TEST_CASE(ordering_after_reconnect, shared_test_data) {
int handlers_called = 0;
// packets
auto publish_qos1 = encoders::encode_publish(
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::no, {}
);
auto publish_qos1_dup = encoders::encode_publish(
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::yes, {}
);
auto publish_qos2 = encoders::encode_publish(
2, topic, payload, qos_e::exactly_once, retain_e::no, dup_e::no, {}
);
auto puback = encoders::encode_puback(1, uint8_t(0x00), {});
auto pubrec = encoders::encode_pubrec(2, uint8_t(0x00), {});
auto pubrel = encoders::encode_pubrel(2, uint8_t(0x00), {});
auto pubcomp = encoders::encode_pubcomp(2, uint8_t(0x00), {});
@ -84,8 +86,8 @@ BOOST_FIXTURE_TEST_CASE(ordering_after_reconnect, shared_test_data) {
[&](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rc, rc.message());
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
if (handlers_called == expected_handlers_called)
c.cancel();
@ -97,8 +99,8 @@ BOOST_FIXTURE_TEST_CASE(ordering_after_reconnect, shared_test_data) {
[&](error_code ec, reason_code rc, pubcomp_props) {
++handlers_called;
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rc, rc.message());
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
if (handlers_called == expected_handlers_called)
c.cancel();
@ -106,8 +108,8 @@ BOOST_FIXTURE_TEST_CASE(ordering_after_reconnect, shared_test_data) {
);
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());
}
BOOST_FIXTURE_TEST_CASE(throttling, shared_test_data) {
@ -168,9 +170,9 @@ BOOST_FIXTURE_TEST_CASE(throttling, shared_test_data) {
[&c, &handlers_called, i](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_MESSAGE(!rc, rc.message());
BOOST_CHECK_EQUAL(handlers_called, i + 1);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
BOOST_TEST(handlers_called == i + 1);
if (i == 2)
c.cancel();
@ -178,8 +180,82 @@ BOOST_FIXTURE_TEST_CASE(throttling, shared_test_data) {
);
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());
}
BOOST_FIXTURE_TEST_CASE(prioritize_disconnect, shared_test_data) {
constexpr int expected_handlers_called = 3;
int handlers_called = 0;
// data
std::vector<subscribe_topic> sub_topics = {
subscribe_topic { "topic", subscribe_options {} }
};
std::vector<uint8_t> reason_codes = { uint8_t(0x00) };
// packets
auto disconnect = encoders::encode_disconnect(uint8_t(0x00), {});
auto subscribe = encoders::encode_subscribe(
1, sub_topics, subscribe_props {}
);
auto suback = encoders::encode_suback(1, reason_codes, suback_props {});
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(1ms))
.reply_with(connack, after(2ms))
.expect(publish_qos1) // first one goes alone
.complete_with(success, after(3ms))
.expect(disconnect) // disconnect overrides a subscribe request
.complete_with(success, after(1ms));
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
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
.async_run(asio::detached);
// give time to establish a connection
asio::steady_timer timer(executor);
timer.expires_after(100ms);
timer.async_wait([&](error_code) {
c.async_publish<qos_e::at_least_once>(
topic, payload, retain_e::no, publish_props {},
[&](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_TEST(ec == asio::error::operation_aborted);
BOOST_TEST(rc == reason_codes::empty);
}
);
c.async_subscribe(
sub_topics, subscribe_props {},
[&](error_code ec, std::vector<reason_code> rcs, suback_props) {
++handlers_called;
BOOST_TEST(ec == asio::error::operation_aborted);
BOOST_ASSERT(rcs.size() == 1);
BOOST_TEST(rcs[0] == reason_codes::empty);
}
);
c.async_disconnect([&](error_code ec) {
++handlers_called;
BOOST_TEST(!ec);
});
});
ioc.run_for(2s);
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}
BOOST_AUTO_TEST_SUITE_END();

View File

@ -183,23 +183,21 @@ BOOST_FIXTURE_TEST_CASE(keep_alive_change_while_waiting, shared_test_data) {
BOOST_FIXTURE_TEST_CASE(keep_alive_change_during_writing, shared_test_data) {
// data
uint16_t keep_alive = 0;
uint16_t keep_alive = 1;
uint16_t server_keep_alive = 1;
test::msg_exchange broker_side;
broker_side
.expect(connect_with_keep_alive(keep_alive))
.complete_with(success, after(1ms))
.reply_with(connack_with_keep_alive(server_keep_alive), after(2ms))
.reply_with(connack_with_keep_alive(server_keep_alive), after(1500ms))
.expect(pingreq)
.complete_with(fail, after(1ms))
.expect(connect_with_keep_alive(keep_alive))
.complete_with(success, after(1ms))
.reply_with(connack_no_ka, after(2ms));
.reply_with(pingresp, after(2ms));
run_test(
std::move(broker_side),
std::chrono::milliseconds(1500), keep_alive
std::chrono::milliseconds(2700), keep_alive
);
}

View File

@ -1,5 +1,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/asio/any_completion_handler.hpp>
#include <async_mqtt5/mqtt_client.hpp>
#include "test_common/message_exchange.hpp"
@ -60,7 +62,7 @@ void test_receive_malformed_packet(
timer.async_wait([&c](error_code) { c.cancel(); });
ioc.run();
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(broker.received_all_expected());
}
BOOST_AUTO_TEST_CASE(forbidden_packet_type) {
@ -99,12 +101,28 @@ BOOST_AUTO_TEST_CASE(receive_malformed_publish) {
}
struct shared_test_data {
error_code success {};
error_code success{};
error_code fail = asio::error::not_connected;
const std::string topic = "topic";
const std::string payload = "payload";
const std::string connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
);
const std::string connack = encoders::encode_connack(false, uint8_t(0x00), {});
const std::string publish = encoders::encode_publish(
0, topic, payload, qos_e::at_most_once, retain_e::no, dup_e::no, {}
);
std::string encode_disconnect_with_rs(std::string_view reason_string) {
disconnect_props dc_props;
dc_props[prop::reason_string] = reason_string;
return encoders::encode_disconnect(
reason_codes::malformed_packet.value(), dc_props
);
}
};
BOOST_FIXTURE_TEST_CASE(receive_disconnect, shared_test_data) {
@ -121,7 +139,6 @@ BOOST_FIXTURE_TEST_CASE(receive_disconnect, shared_test_data) {
.complete_with(success, after(0ms))
.reply_with(connack, after(0ms));
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
@ -138,23 +155,46 @@ BOOST_FIXTURE_TEST_CASE(receive_disconnect, shared_test_data) {
timer.async_wait([&c](error_code) { c.cancel(); });
ioc.run();
BOOST_CHECK(broker.received_all_expected());
BOOST_TEST(broker.received_all_expected());
}
template <typename VerifyFun>
void run_receive_test(
test::msg_exchange broker_side, int num_of_receives,
VerifyFun&& verify_fun
) {
const int expected_handlers_called = num_of_receives;
int handlers_called = 0;
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
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
.async_run(asio::detached);
for (int i = 0; i < num_of_receives; ++i)
c.async_receive([&](
error_code ec,
std::string topic, std::string payload, publish_props props
) {
handlers_called++;
verify_fun(ec, topic, payload, props);
if (handlers_called == expected_handlers_called)
c.cancel();
});
ioc.run();
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}
BOOST_FIXTURE_TEST_CASE(receive_byte_by_byte, shared_test_data) {
constexpr int expected_handlers_called = 1;
int handlers_called = 0;
// data
std::string topic = "topic";
std::string payload = "payload";
// packets
auto publish = encoders::encode_publish(
0, topic, payload, qos_e::at_most_once, retain_e::no, dup_e::no, {}
);
test::msg_exchange broker_side;
broker_side
.expect(connect)
@ -166,32 +206,67 @@ BOOST_FIXTURE_TEST_CASE(receive_byte_by_byte, shared_test_data) {
std::string { publish[i] }, after(std::chrono::milliseconds(i + 2))
);
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.async_run(asio::detached);
c.async_receive([&](
auto verify_fun = [&](
error_code ec, std::string topic_, std::string payload_, publish_props
) {
handlers_called++;
BOOST_TEST(!ec);
BOOST_TEST(topic == topic_);
BOOST_TEST(payload == payload_);
};
BOOST_CHECK(!ec);
BOOST_CHECK_EQUAL(topic, topic_);
BOOST_CHECK_EQUAL(payload, payload_);
run_receive_test(std::move(broker_side), 1, std::move(verify_fun));
}
c.cancel();
});
BOOST_FIXTURE_TEST_CASE(receive_multiple_packets_at_once, shared_test_data) {
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(1ms))
.reply_with(connack, after(2ms))
.send(publish, publish, publish, publish, publish, after(100ms));
ioc.run_for(100ms);
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected());
auto verify_fun = [&](
error_code ec, std::string topic_, std::string payload_, publish_props
) {
BOOST_TEST(!ec);
BOOST_TEST(topic == topic_);
BOOST_TEST(payload == payload_);
};
run_receive_test(std::move(broker_side), 5, std::move(verify_fun));
}
BOOST_FIXTURE_TEST_CASE(receive_multiple_packets_with_malformed, shared_test_data) {
// packets
// ghost publishes need to be cleared once the client reads the malformed packet
auto ghost_publish = encoders::encode_publish(
0, topic, "should not be received!", qos_e::at_most_once, retain_e::no, dup_e::no, {}
);
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(1ms))
.reply_with(connack, after(2ms))
.send(
publish, publish, std::string({ 0x00 }) /* malformed */,
ghost_publish, ghost_publish, after(100ms)
)
.expect(encode_disconnect_with_rs("Malformed Packet received from the Server"))
.expect(connect)
.complete_with(success, after(1ms))
.reply_with(connack, after(2ms))
.send(publish, after(300ms));
auto verify_fun = [&](
error_code ec, std::string topic_, std::string payload_, publish_props
) {
BOOST_TEST(!ec);
BOOST_TEST(topic == topic_);
BOOST_TEST(payload == payload_);
};
run_receive_test(std::move(broker_side), 3, std::move(verify_fun));
}
BOOST_AUTO_TEST_SUITE_END();

View File

@ -19,6 +19,11 @@ struct shared_test_data {
error_code success {};
error_code fail = asio::error::not_connected;
connect_props cprops = allow_big_packets_cprops();
const std::string connect_with_cprops = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, cprops, std::nullopt
);
const std::string connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
);
@ -38,12 +43,33 @@ struct shared_test_data {
const std::string publish_qos2 = encoders::encode_publish(
1, topic, payload, qos_e::exactly_once, retain_e::no, dup_e::no, {}
);
const std::string big_publish = encode_big_publish();
const std::string puback = encoders::encode_puback(1, uint8_t(0x00), {});
const std::string pubrec = encoders::encode_pubrec(1, uint8_t(0x00), {});
const std::string pubrel = encoders::encode_pubrel(1, uint8_t(0x00), {});
const std::string pubcomp = encoders::encode_pubcomp(1, uint8_t(0x00), {});
private:
connect_props allow_big_packets_cprops() {
connect_props c_props;
c_props[prop::maximum_packet_size] = std::numeric_limits<int32_t>::max();
return c_props;
}
std::string encode_big_publish() {
publish_props big_props;
for (int i = 0; i < 100; i++)
big_props[prop::user_property].push_back(std::string(65534, 'u'));
return encoders::encode_publish(
1, topic, payload,
qos_e::at_most_once, retain_e::no, dup_e::no,
std::move(big_props)
);
}
};
using test::after;
@ -70,17 +96,17 @@ void run_test(test::msg_exchange broker_side) {
++handlers_called;
auto data = shared_test_data();
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(data.topic, rec_topic);
BOOST_CHECK_EQUAL(data.payload, rec_payload);
BOOST_TEST(!ec);
BOOST_TEST(data.topic == rec_topic);
BOOST_TEST(data.payload == rec_payload);
c.cancel();
}
);
ioc.run_for(3s);
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());
}
@ -285,4 +311,45 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_pubcomp, shared_test_data) {
run_test(std::move(broker_side));
}
BOOST_FIXTURE_TEST_CASE(receive_big_publish, shared_test_data) {
const int expected_handlers_called = 1;
int handlers_called = 0;
test::msg_exchange broker_side;
broker_side
.expect(connect_with_cprops)
.complete_with(success, after(1ms))
.reply_with(connack, after(2ms))
.send(big_publish, after(1s));
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1")
.connect_properties(cprops)
.async_run(asio::detached);
c.async_receive([&](
error_code ec, std::string topic_, std::string payload_, publish_props pprops
) {
handlers_called++;
BOOST_TEST(!ec);
BOOST_TEST(topic == topic_);
BOOST_TEST(payload == payload_);
BOOST_TEST(pprops[prop::user_property].size() == 100);
c.cancel();
});
ioc.run();
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}
BOOST_AUTO_TEST_SUITE_END();

View File

@ -26,7 +26,7 @@ struct shared_test_data {
const std::string topic = "topic";
const std::string payload = "payload";
const std::string publish_qos0 = encoders::encode_publish(
0, topic, payload, qos_e::at_most_once, retain_e::no, dup_e::no, {}
);
@ -104,7 +104,7 @@ BOOST_FIXTURE_TEST_CASE(send_publish_qos0, shared_test_data) {
run_test<qos_e::at_most_once>(
std::move(broker_side),
[](error_code ec) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_TEST(!ec);
}
);
}
@ -122,8 +122,8 @@ BOOST_FIXTURE_TEST_CASE(send_publish_qos1, shared_test_data) {
run_test<qos_e::at_least_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, puback_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -144,8 +144,8 @@ BOOST_FIXTURE_TEST_CASE(send_publish_qos2, shared_test_data) {
run_test<qos_e::exactly_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, pubcomp_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -168,8 +168,8 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_publish, shared_test_data) {
run_test<qos_e::at_least_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, puback_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -195,8 +195,8 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_pubrel, shared_test_data) {
run_test<qos_e::exactly_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, pubcomp_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -233,8 +233,8 @@ BOOST_FIXTURE_TEST_CASE(receive_malformed_puback, shared_test_data) {
run_test<qos_e::at_least_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, puback_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -275,8 +275,8 @@ BOOST_FIXTURE_TEST_CASE(receive_malformed_pubrec, shared_test_data) {
run_test<qos_e::exactly_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, pubcomp_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -313,8 +313,8 @@ BOOST_FIXTURE_TEST_CASE(receive_malformed_pubcomp, shared_test_data) {
run_test<qos_e::exactly_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, pubcomp_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::success);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
}
);
}
@ -337,8 +337,8 @@ BOOST_FIXTURE_TEST_CASE(receive_pubrec_with_rc, shared_test_data) {
run_test<qos_e::exactly_once>(
std::move(broker_side),
[](error_code ec, reason_code rc, pubcomp_props) {
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(rc, reason_codes::unspecified_error);
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::unspecified_error);
}
);
}
@ -372,8 +372,8 @@ BOOST_FIXTURE_TEST_CASE(cancel_resending_publish, shared_test_data) {
[&handlers_called, &c](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK(ec = asio::error::operation_aborted);
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
BOOST_TEST(ec = asio::error::operation_aborted);
BOOST_TEST(rc == reason_codes::empty);
c.cancel();
}
@ -382,8 +382,82 @@ BOOST_FIXTURE_TEST_CASE(cancel_resending_publish, shared_test_data) {
cancel_signal.emit(asio::cancellation_type::total);
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());
}
BOOST_FIXTURE_TEST_CASE(send_big_publish, shared_test_data) {
// currently broken in test environment
constexpr int expected_handlers_called = 1;
int handlers_called = 0;
// data
connack_props cprops;
cprops[prop::maximum_packet_size] = 10'000'000;
const std::string big_topic = std::string(65534, 't');
const std::string big_payload = std::string(65534, 'p');
publish_props big_props;
for (int i = 0; i < 1; i++)
big_props[prop::user_property].push_back(std::string(65534, 'u'));
// packets
auto allow_big_connack = encoders::encode_connack(false, uint8_t(0x00), cprops);
auto big_publish = encoders::encode_publish(
1, big_topic, big_payload,
qos_e::at_least_once, retain_e::no, dup_e::no,
big_props
);
auto pingreq = encoders::encode_pingreq();
auto pingresp = encoders::encode_pingresp();
std::vector<std::string> buffers;
for (size_t i = 0; i < big_publish.size(); i += 65536)
buffers.push_back(big_publish.substr(i, 65536));
BOOST_TEST_REQUIRE(buffers.size() == 3);
// this single async_send will result in 3 calls to async_write_some in our stream
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(1ms))
.reply_with(allow_big_connack, after(2ms))
.expect(buffers[0])
.complete_with(success, after(1ms))
.expect(buffers[1])
.complete_with(success, after(1ms))
.expect(buffers[2])
.complete_with(success, after(1ms))
.reply_with(puback, after(2ms));
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
using client_type = mqtt_client<test::test_stream>;
client_type c(executor, "");
c.brokers("127.0.0.1") // to avoid reconnect backoff
.async_run(asio::detached);
c.async_publish<qos_e::at_least_once>(
big_topic, big_payload, retain_e::no, big_props,
[&handlers_called, &c](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_TEST(!ec);
BOOST_TEST(rc == reason_codes::success);
c.cancel();
}
);
ioc.run_for(2s);
BOOST_TEST(handlers_called == expected_handlers_called);
BOOST_TEST(broker.received_all_expected());
}
BOOST_AUTO_TEST_SUITE_END();