From b2266adb32a63e7ef77a74e3a2abf6ee87dc8192 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Fri, 21 Mar 2025 09:05:26 -0600 Subject: [PATCH] :white_check_mark: Add tests for msg handlers calling multiple callbacks Problem: - When multiple callbacks match a message, they should all be called. There was no test binding this behaviour for either the "normal" handler or the indexed handler. Solution: - Add tests that bind the behaviour. --- test/msg/handler_builder.cpp | 24 ++++++++++++++++++++++++ test/msg/indexed_builder.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/test/msg/handler_builder.cpp b/test/msg/handler_builder.cpp index df37943a..d92f6ecc 100644 --- a/test/msg/handler_builder.cpp +++ b/test/msg/handler_builder.cpp @@ -143,3 +143,27 @@ TEST_CASE("handle extra arguments", "[handler_builder]") { CHECK(callback_success); CHECK(callback_extra_arg == 42); } + +namespace { +bool callback2_success; + +constexpr auto test_callback2 = msg::callback<"cb2", msg_defn>( + id_match, [](msg_view_t) { callback2_success = true; }); + +struct test_project_multi_cb { + constexpr static auto config = + cib::config(cib::exports, + cib::extend(test_callback, test_callback2)); +}; +} // namespace + +TEST_CASE("call multiple callbacks", "[handler_builder]") { + cib::nexus test_nexus{}; + test_nexus.init(); + + callback_success = false; + callback2_success = false; + cib::service->handle(test_msg_t{"id"_field = 0x80}); + CHECK(callback_success); + CHECK(callback2_success); +} diff --git a/test/msg/indexed_builder.cpp b/test/msg/indexed_builder.cpp index 9b21a4ac..ab022c0d 100644 --- a/test/msg/indexed_builder.cpp +++ b/test/msg/indexed_builder.cpp @@ -490,3 +490,28 @@ TEST_CASE("handle raw message by mixture of callbacks", "[indexed_builder]") { std::array{0x8000ba11u, 0x0042d00du})); CHECK(callback_count == 2); } + +namespace { +bool callback2_success; + +constexpr auto test_callback2 = msg::callback<"TestCallback2", msg_defn>( + msg::in, [](auto) { callback2_success = true; }); + +struct test_project_multi_cb { + constexpr static auto config = + cib::config(cib::exports, + cib::extend(test_callback, test_callback2)); +}; +} // namespace + +TEST_CASE("call multiple callbacks", "[handler_builder]") { + cib::nexus test_nexus{}; + test_nexus.init(); + + callback_success = false; + callback2_success = false; + cib::service->handle( + test_msg_t{"test_id_field"_field = 0x80}); + CHECK(callback_success); + CHECK(callback2_success); +}