From 3423d2e1058ef12a58d1a4801d9d20a223088ed4 Mon Sep 17 00:00:00 2001 From: Thomas Debrunner Date: Thu, 29 Feb 2024 13:54:02 +0100 Subject: [PATCH] connection: added unit test to showcase cleanup problem when waiting on never arriving messages --- include/mav/Connection.h | 5 +++++ tests/Network.cpp | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/include/mav/Connection.h b/include/mav/Connection.h index bc3af7e..a3eda87 100644 --- a/include/mav/Connection.h +++ b/include/mav/Connection.h @@ -88,6 +88,11 @@ namespace mav { public: + size_t callbackCount() { + std::scoped_lock lock(_message_callback_mtx); + return _message_callbacks.size(); + } + void removeAllCallbacks() { std::scoped_lock lock(_message_callback_mtx); _message_callbacks.clear(); diff --git a/tests/Network.cpp b/tests/Network.cpp index 18b4e03..ab1bc59 100644 --- a/tests/Network.cpp +++ b/tests/Network.cpp @@ -265,4 +265,13 @@ TEST_CASE("Create network runtime") { interface_partner)); CHECK(found); } + + SUBCASE("Callbacks are cleaned up on receive timeout") { + interface.reset(); + for (int i = 0; i < 10; i++) { + auto expectation = connection->expect("TEST_MESSAGE"); + CHECK_THROWS_AS(auto message = connection->receive(expectation, 100), TimeoutException); + } + CHECK_EQ(connection->callbackCount(), 0); + } }