Skip to content

Commit

Permalink
Merge pull request #180 from redboltz/incr_cov
Browse files Browse the repository at this point in the history
Increase coverage.
  • Loading branch information
redboltz authored May 3, 2024
2 parents 24d0e29 + d3de3d5 commit cde1662
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit/ut_cpp20coro_ep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ BOOST_AUTO_TEST_CASE(pingresp_tout_v311) {
version,
ioc.get_executor()
);
ep->set_pingresp_recv_timeout_ms(0); // for coverage
ep->set_pingresp_recv_timeout_ms(10);
// prepare connect
{
Expand Down
10 changes: 10 additions & 0 deletions test/unit/ut_ep_pid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ BOOST_AUTO_TEST_CASE(wait_until) {
ep->acquire_unique_packet_id_wait_until(as::use_future).get();
}

as::dispatch(
as::bind_executor(
ep->strand(),
[&] {
auto pid_opt = ep->acquire_unique_packet_id();
BOOST_CHECK(!pid_opt);
}
)
);

auto fut1 = ep->acquire_unique_packet_id_wait_until(as::use_future);
auto fut2 = ep->acquire_unique_packet_id_wait_until(as::use_future);
auto fut3 = ep->acquire_unique_packet_id_wait_until(as::use_future);
Expand Down
115 changes: 115 additions & 0 deletions test/unit/ut_ep_size_max.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,121 @@ BOOST_AUTO_TEST_CASE(client_send) {
th.join();
}

BOOST_AUTO_TEST_CASE(client_send_no_store) {
auto version = am::protocol_version::v5;
as::io_context ioc;
auto guard = as::make_work_guard(ioc.get_executor());
std::thread th {
[&] {
ioc.run();
}
};

auto ep = am::endpoint<async_mqtt::role::client, async_mqtt::stub_socket>::create(
version,
// for stub_socket args
version,
ioc
);

ep->next_layer().set_close_checker(
[&] { BOOST_TEST(false); }
);

auto connect = am::v5::connect_packet{
true, // clean_start
0x1234, // keep_alive
am::allocate_buffer("cid1"),
am::nullopt, // will
am::allocate_buffer("user1"),
am::allocate_buffer("pass1")
};

auto connack = am::v5::connack_packet{
false, // session_present
am::connect_reason_code::success,
am::properties{
am::property::maximum_packet_size{21}
}
};

ep->next_layer().set_recv_packets(
{
// receive packets
connack,
}
);

// send connect
ep->next_layer().set_write_packet_checker(
[&](am::packet_variant wp) {
BOOST_TEST(connect == wp);
}
);
{
auto ec = ep->send(connect, as::use_future).get();
BOOST_TEST(!ec);
}

// recv connack
{
auto pv = ep->recv(as::use_future).get();
BOOST_TEST(connack == pv);
}

// size: 21bytes
auto pid_opt1 = ep->acquire_unique_packet_id(as::use_future).get();
BOOST_TEST(pid_opt1.has_value());
auto publish_1_q1 = am::v5::publish_packet(
*pid_opt1,
am::allocate_buffer("topic1"),
am::allocate_buffer("payload1"),
am::qos::at_least_once,
am::properties{}
);

// size: 22bytes
auto pid_opt2 = ep->acquire_unique_packet_id(as::use_future).get();
BOOST_TEST(pid_opt2.has_value());
auto publish_2_q1 = am::v5::publish_packet(
*pid_opt2,
am::allocate_buffer("topic1"),
am::allocate_buffer("payload1+"),
am::qos::at_least_once,
am::properties{}
);

// send publish_1
ep->next_layer().set_write_packet_checker(
[&](am::packet_variant wp) {
BOOST_TEST(publish_1_q1 == wp);
}
);
{
auto ec = ep->send(publish_1_q1, as::use_future).get();
BOOST_TEST(!ec);
}

// send publish_2
ep->next_layer().set_write_packet_checker(
[&](am::packet_variant) {
BOOST_TEST(false);
}
);
{
auto ec = ep->send(publish_2_q1, as::use_future).get();
BOOST_TEST(ec.code() == am::errc::bad_message);
}
auto pid_opt3 = ep->acquire_unique_packet_id(as::use_future).get();
BOOST_TEST(pid_opt3.has_value());
BOOST_TEST(*pid_opt3 == 2); // 2 can be resused

ep->next_layer().set_close_checker({});
ep->close(as::use_future).get();
guard.reset();
th.join();
}

BOOST_AUTO_TEST_CASE(client_recv) {
auto version = am::protocol_version::v5;
as::io_context ioc;
Expand Down
39 changes: 39 additions & 0 deletions test/unit/ut_ep_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1744,4 +1744,43 @@ BOOST_AUTO_TEST_CASE(v5_topic_alias) {
th.join();
}

BOOST_AUTO_TEST_CASE(restore_packets_error) {
auto version = am::protocol_version::v3_1_1;
as::io_context ioc;
auto guard = as::make_work_guard(ioc.get_executor());
std::thread th {
[&] {
ioc.run();
}
};

auto ep = am::endpoint<async_mqtt::role::client, async_mqtt::stub_socket>::create(
version,
// for stub_socket args
version,
ioc
);

auto pid_opt1 = ep->acquire_unique_packet_id(as::use_future).get();
BOOST_TEST(pid_opt1.has_value());
auto publish1 = am::v3_1_1::publish_packet(
*pid_opt1,
am::allocate_buffer("topic1"),
am::allocate_buffer("payload1"),
am::qos::at_least_once
);
as::dispatch(
as::bind_executor(
ep->strand(),
[&] {
ep->restore_packets({am::store_packet_variant{publish1}}); // pid is already used
auto stored = ep->get_stored_packets();
BOOST_TEST(stored.empty());
}
)
);
guard.reset();
th.join();
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit cde1662

Please sign in to comment.