diff --git a/test/unit/ut_cpp20coro_ep.cpp b/test/unit/ut_cpp20coro_ep.cpp index a0dd79493..50735e115 100644 --- a/test/unit/ut_cpp20coro_ep.cpp +++ b/test/unit/ut_cpp20coro_ep.cpp @@ -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 { diff --git a/test/unit/ut_ep_pid.cpp b/test/unit/ut_ep_pid.cpp index 4d8140682..bc7a8669b 100644 --- a/test/unit/ut_ep_pid.cpp +++ b/test/unit/ut_ep_pid.cpp @@ -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); diff --git a/test/unit/ut_ep_size_max.cpp b/test/unit/ut_ep_size_max.cpp index 829fd8e08..9132f4eee 100644 --- a/test/unit/ut_ep_size_max.cpp +++ b/test/unit/ut_ep_size_max.cpp @@ -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::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; diff --git a/test/unit/ut_ep_store.cpp b/test/unit/ut_ep_store.cpp index f5b8e625c..a3e1c5ba0 100644 --- a/test/unit/ut_ep_store.cpp +++ b/test/unit/ut_ep_store.cpp @@ -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::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()