From 944637fc09a91fc81faf6be64ce77b802428146c Mon Sep 17 00:00:00 2001 From: Abderahmane BENALI <46283596+benaliabderrahmane@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:15:43 +0100 Subject: [PATCH] Expect event callbacks only for handlers that were created (#3461) disable_enable_event_callbacks and its generic pubsub twin expect the deadline and liveliness callbacks to run. rclcpp creates no handler for an event type the RMW doesn't support (it only warns), so on such an RMW the count stays at 0 and the test fails although enabling and disabling work. Derive the expected counts from the handlers that exist and skip only when there are none. This drops the rmw_zenoh skip in test_generic_pubsub as well: test_subscription runs the same body on rmw_zenoh_cpp without a skip and passes. Claude-Session: https://claude.ai/code/session_012EJmdSvyUzRoZmYQKqLnjQ Signed-off-by: Abderahmane Benali <46283596+benaliabderrahmane@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (cherry picked from commit 06702a1a110f36a7d1d5ffd5a85b3427ffe79206) --- rclcpp/test/rclcpp/test_generic_pubsub.cpp | 24 +++++++++++++--------- rclcpp/test/rclcpp/test_subscription.cpp | 21 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/rclcpp/test/rclcpp/test_generic_pubsub.cpp b/rclcpp/test/rclcpp/test_generic_pubsub.cpp index 498322334a..290d475ec7 100644 --- a/rclcpp/test/rclcpp/test_generic_pubsub.cpp +++ b/rclcpp/test/rclcpp/test_generic_pubsub.cpp @@ -382,9 +382,6 @@ TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_callbacks) TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_event_callbacks) { - if (std::string(rmw_get_implementation_identifier()).find("rmw_zenoh_cpp") == 0) { - GTEST_SKIP() << "rmw_zenoh doesn't support deadline and liveliness events"; - } const std::string topic_name = "test_event_callbacks_topic"; const std::string type = "test_msgs/msg/Strings"; const auto qos = rclcpp::QoS(10U).deadline(100ms); @@ -409,6 +406,13 @@ TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_event_callbacks) node_->create_generic_subscription(topic_name, type, qos, do_nothing, sub_options); const auto & event_handlers = subscription->get_event_handlers(); + // rclcpp creates no handler (and warns) for an event type the RMW doesn't support. + const int deadline_step = + event_handlers.count(RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED) ? 1 : 0; + const int liveliness_step = event_handlers.count(RCL_SUBSCRIPTION_LIVELINESS_CHANGED) ? 1 : 0; + if (deadline_step + liveliness_step == 0) { + GTEST_SKIP() << "the RMW supports neither deadline nor liveliness events"; + } // Initially, both callbacks counters should be zero EXPECT_EQ(deadline_callback_count, 0); @@ -422,9 +426,9 @@ TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_event_callbacks) EXPECT_NO_THROW(handler->disable()); } } - // Expect both callbacks to have been called once since they should be enabled by default - EXPECT_EQ(deadline_callback_count, 1); - EXPECT_EQ(liveliness_callback_count, 1); + // Expect each handler that exists to have called back once, since handlers start enabled + EXPECT_EQ(deadline_callback_count, deadline_step); + EXPECT_EQ(liveliness_callback_count, liveliness_step); // Execute events one more time and enable all event handlers for (const auto & [event_type, handler] : event_handlers) { @@ -435,8 +439,8 @@ TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_event_callbacks) } } // Expect no change since they were disabled - EXPECT_EQ(deadline_callback_count, 1); - EXPECT_EQ(liveliness_callback_count, 1); + EXPECT_EQ(deadline_callback_count, deadline_step); + EXPECT_EQ(liveliness_callback_count, liveliness_step); // Execute events one more time for (const auto & [event_type, handler] : event_handlers) { @@ -445,6 +449,6 @@ TEST_F(RclcppGenericNodeFixture, disable_enable_subscription_event_callbacks) handler->execute(data); } } - EXPECT_EQ(deadline_callback_count, 2); - EXPECT_EQ(liveliness_callback_count, 2); + EXPECT_EQ(deadline_callback_count, 2 * deadline_step); + EXPECT_EQ(liveliness_callback_count, 2 * liveliness_step); } diff --git a/rclcpp/test/rclcpp/test_subscription.cpp b/rclcpp/test/rclcpp/test_subscription.cpp index 376102482a..e58cfece99 100644 --- a/rclcpp/test/rclcpp/test_subscription.cpp +++ b/rclcpp/test/rclcpp/test_subscription.cpp @@ -830,6 +830,13 @@ TEST_F(TestSubscription, disable_enable_event_callbacks) "test_event_callbacks_topic", qos, do_nothing, subscription_options); const auto & event_handlers = subscription->get_event_handlers(); + // rclcpp creates no handler (and warns) for an event type the RMW doesn't support. + const int deadline_step = + event_handlers.count(RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED) ? 1 : 0; + const int liveliness_step = event_handlers.count(RCL_SUBSCRIPTION_LIVELINESS_CHANGED) ? 1 : 0; + if (deadline_step + liveliness_step == 0) { + GTEST_SKIP() << "the RMW supports neither deadline nor liveliness events"; + } // Initially, both callbacks counters should be zero EXPECT_EQ(deadline_callback_count, 0); @@ -843,9 +850,9 @@ TEST_F(TestSubscription, disable_enable_event_callbacks) EXPECT_NO_THROW(handler->disable()); } } - // Expect both callbacks to have been called once since they should be enabled by default - EXPECT_EQ(deadline_callback_count, 1); - EXPECT_EQ(liveliness_callback_count, 1); + // Expect each handler that exists to have called back once, since handlers start enabled + EXPECT_EQ(deadline_callback_count, deadline_step); + EXPECT_EQ(liveliness_callback_count, liveliness_step); // Execute events one more time and enable all event handlers for (const auto & [event_type, handler] : event_handlers) { @@ -856,8 +863,8 @@ TEST_F(TestSubscription, disable_enable_event_callbacks) } } // Expect no change since they were disabled - EXPECT_EQ(deadline_callback_count, 1); - EXPECT_EQ(liveliness_callback_count, 1); + EXPECT_EQ(deadline_callback_count, deadline_step); + EXPECT_EQ(liveliness_callback_count, liveliness_step); // Execute events one more time for (const auto & [event_type, handler] : event_handlers) { @@ -866,6 +873,6 @@ TEST_F(TestSubscription, disable_enable_event_callbacks) handler->execute(data); } } - EXPECT_EQ(deadline_callback_count, 2); - EXPECT_EQ(liveliness_callback_count, 2); + EXPECT_EQ(deadline_callback_count, 2 * deadline_step); + EXPECT_EQ(liveliness_callback_count, 2 * liveliness_step); }