From 98b6fa8a8d7216d7e70e16a075e1ecb1d5722808 Mon Sep 17 00:00:00 2001 From: Abderahmane Benali <46283596+benaliabderrahmane@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:08:10 +0100 Subject: [PATCH] Expect event callbacks only for handlers that were created 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. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012EJmdSvyUzRoZmYQKqLnjQ Signed-off-by: Abderahmane Benali <46283596+benaliabderrahmane@users.noreply.github.com> --- 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 f1ad0f8b18..802ed9d2f9 100644 --- a/rclcpp/test/rclcpp/test_generic_pubsub.cpp +++ b/rclcpp/test/rclcpp/test_generic_pubsub.cpp @@ -389,9 +389,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); @@ -416,6 +413,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); @@ -429,9 +433,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) { @@ -442,8 +446,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) { @@ -452,6 +456,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 b90db762ff..5f5645681e 100644 --- a/rclcpp/test/rclcpp/test_subscription.cpp +++ b/rclcpp/test/rclcpp/test_subscription.cpp @@ -842,6 +842,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); @@ -855,9 +862,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) { @@ -868,8 +875,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) { @@ -878,8 +885,8 @@ 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); } /*