|
| 1 | +// Copyright 2026 Abderahmane BENALI |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "test_base.hpp" |
| 16 | + |
| 17 | +#include <cstring> |
| 18 | + |
| 19 | +#include "test_msgs/msg/basic_types.hpp" |
| 20 | + |
| 21 | +#include "rmw/event.h" |
| 22 | +#include "rmw/qos_profiles.h" |
| 23 | +#include "rosidl_typesupport_cpp/message_type_support.hpp" |
| 24 | + |
| 25 | +class EventTest : public RmwUdsNodeTest |
| 26 | +{ |
| 27 | +protected: |
| 28 | + rmw_publisher_t * pub = nullptr; |
| 29 | + rmw_subscription_t * sub = nullptr; |
| 30 | + const rosidl_message_type_support_t * ts = nullptr; |
| 31 | + rmw_qos_profile_t qos; |
| 32 | + |
| 33 | + void SetUp() override |
| 34 | + { |
| 35 | + RmwUdsNodeTest::SetUp(); |
| 36 | + ts = rosidl_typesupport_cpp::get_message_type_support_handle< |
| 37 | + test_msgs::msg::BasicTypes>(); |
| 38 | + std::memset(&qos, 0, sizeof(qos)); |
| 39 | + qos.history = RMW_QOS_POLICY_HISTORY_KEEP_LAST; |
| 40 | + qos.depth = 10; |
| 41 | + qos.reliability = RMW_QOS_POLICY_RELIABILITY_RELIABLE; |
| 42 | + qos.durability = RMW_QOS_POLICY_DURABILITY_VOLATILE; |
| 43 | + |
| 44 | + auto pub_opts = rmw_get_default_publisher_options(); |
| 45 | + pub = rmw_create_publisher(node, ts, "/event_test_topic", &qos, &pub_opts); |
| 46 | + ASSERT_NE(nullptr, pub); |
| 47 | + |
| 48 | + auto sub_opts = rmw_get_default_subscription_options(); |
| 49 | + sub = rmw_create_subscription(node, ts, "/event_test_topic", &qos, &sub_opts); |
| 50 | + ASSERT_NE(nullptr, sub); |
| 51 | + } |
| 52 | + |
| 53 | + void TearDown() override |
| 54 | + { |
| 55 | + if (sub) { auto _r [[maybe_unused]] = rmw_destroy_subscription(node, sub); } |
| 56 | + if (pub) { auto _r [[maybe_unused]] = rmw_destroy_publisher(node, pub); } |
| 57 | + RmwUdsNodeTest::TearDown(); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +// rmw_event_type_is_supported() reports no event types as supported, so |
| 62 | +// rclcpp expects *_event_init() to fail with RMW_RET_UNSUPPORTED for those |
| 63 | +// event types (it only swallows initialization failures reported this way, |
| 64 | +// via UnsupportedEventTypeException). If init instead reports success, |
| 65 | +// rclcpp constructs a live event handler for it, and the failure only |
| 66 | +// surfaces later - unhandled - when the executor registers its callback, |
| 67 | +// crashing with "failed to set the on new message callback for Event". |
| 68 | +TEST_F(EventTest, SubscriptionEventInitRejectsUnsupportedEventType) |
| 69 | +{ |
| 70 | + ASSERT_FALSE(rmw_event_type_is_supported(RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE)); |
| 71 | + |
| 72 | + rmw_event_t event = rmw_get_zero_initialized_event(); |
| 73 | + EXPECT_EQ( |
| 74 | + RMW_RET_UNSUPPORTED, |
| 75 | + rmw_subscription_event_init(&event, sub, RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(EventTest, PublisherEventInitRejectsUnsupportedEventType) |
| 79 | +{ |
| 80 | + ASSERT_FALSE(rmw_event_type_is_supported(RMW_EVENT_OFFERED_QOS_INCOMPATIBLE)); |
| 81 | + |
| 82 | + rmw_event_t event = rmw_get_zero_initialized_event(); |
| 83 | + EXPECT_EQ( |
| 84 | + RMW_RET_UNSUPPORTED, |
| 85 | + rmw_publisher_event_init(&event, pub, RMW_EVENT_OFFERED_QOS_INCOMPATIBLE)); |
| 86 | +} |
0 commit comments