diff --git a/cie_thread_configurator/CMakeLists.txt b/cie_thread_configurator/CMakeLists.txt index 4ab2101..add2124 100644 --- a/cie_thread_configurator/CMakeLists.txt +++ b/cie_thread_configurator/CMakeLists.txt @@ -11,17 +11,6 @@ find_package(rclcpp REQUIRED) find_package(cie_config_msgs REQUIRED) find_package(yaml-cpp REQUIRED) -if(BUILD_TESTING) - find_package(ament_lint_auto REQUIRED) - # the following line skips the linter which checks for copyrights - # uncomment the line when a copyright and license is not present in all source files - #set(ament_cmake_copyright_FOUND TRUE) - # the following line skips cpplint (only works in a git repo) - # uncomment the line when this package is not in a git repo - #set(ament_cmake_cpplint_FOUND TRUE) - ament_lint_auto_find_test_dependencies() -endif() - add_library(thread_configurator SHARED src/util.cpp) ament_target_dependencies(thread_configurator rclcpp cie_config_msgs) @@ -66,4 +55,24 @@ install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET) ament_export_include_directories(include) ament_export_dependencies(cie_config_msgs yaml-cpp rclcpp) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + # the following line skips the linter which checks for copyrights + # uncomment the line when a copyright and license is not present in all source files + #set(ament_cmake_copyright_FOUND TRUE) + # the following line skips cpplint (only works in a git repo) + # uncomment the line when this package is not in a git repo + #set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() + + find_package(ament_cmake_ros REQUIRED) + find_package(std_msgs REQUIRED) + find_package(std_srvs REQUIRED) + + ament_add_ros_isolated_gtest(test_util test/test_util.cpp) + target_link_libraries(test_util thread_configurator) + ament_target_dependencies(test_util rclcpp std_msgs std_srvs) +endif() + ament_package() diff --git a/cie_thread_configurator/package.xml b/cie_thread_configurator/package.xml index f14a7c1..e739ad6 100644 --- a/cie_thread_configurator/package.xml +++ b/cie_thread_configurator/package.xml @@ -19,6 +19,10 @@ ament_lint_auto ament_lint_common + ament_cmake_ros + std_msgs + std_srvs + rclcpp yaml-cpp diff --git a/cie_thread_configurator/test/test_util.cpp b/cie_thread_configurator/test/test_util.cpp new file mode 100644 index 0000000..d3c7811 --- /dev/null +++ b/cie_thread_configurator/test/test_util.cpp @@ -0,0 +1,212 @@ +// Copyright 2026 The Autoware Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/string.hpp" +#include "std_srvs/srv/empty.hpp" +#include "gtest/gtest.h" + +#include "cie_thread_configurator/cie_thread_configurator.hpp" + +using cie_thread_configurator::create_callback_group_id; + +// Each test runs in its own rclcpp context so node names and the ROS graph +// do not leak between cases. +class CreateCallbackGroupIdTest : public ::testing::Test { +protected: + void SetUp() override { rclcpp::init(0, nullptr); } + void TearDown() override { rclcpp::shutdown(); } + + rclcpp::CallbackGroup::SharedPtr + make_group(const rclcpp::Node::SharedPtr &node) { + return node->create_callback_group( + rclcpp::CallbackGroupType::MutuallyExclusive); + } + + rclcpp::SubscriptionBase::SharedPtr + add_subscription(const rclcpp::Node::SharedPtr &node, + const rclcpp::CallbackGroup::SharedPtr &group, + const std::string &topic) { + rclcpp::SubscriptionOptions options; + options.callback_group = group; + return node->create_subscription( + topic, rclcpp::QoS(10), + [](const std_msgs::msg::String::ConstSharedPtr) {}, options); + } + + rclcpp::ServiceBase::SharedPtr + add_service(const rclcpp::Node::SharedPtr &node, + const rclcpp::CallbackGroup::SharedPtr &group, + const std::string &name) { + return node->create_service( + name, + [](const std::shared_ptr, + std::shared_ptr) {}, + rclcpp::ServicesQoS().get_rmw_qos_profile(), group); + } + + rclcpp::ClientBase::SharedPtr + add_client(const rclcpp::Node::SharedPtr &node, + const rclcpp::CallbackGroup::SharedPtr &group, + const std::string &name) { + return node->create_client( + name, rclcpp::ServicesQoS().get_rmw_qos_profile(), group); + } + + rclcpp::TimerBase::SharedPtr + add_timer(const rclcpp::Node::SharedPtr &node, + const rclcpp::CallbackGroup::SharedPtr &group, + std::chrono::nanoseconds period) { + return node->create_wall_timer(period, []() {}, group); + } +}; + +// A node at the root namespace must produce a single leading slash +// (e.g. "/node", not "//node"), then the entity descriptor. +TEST_F(CreateCallbackGroupIdTest, RootNamespaceWithSubscription) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto sub = add_subscription(node, group, "/chatter"); + ASSERT_NE(sub, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node@Subscription(/chatter)"); +} + +// A non-root namespace exercises the "ns + '/'" branch. +TEST_F(CreateCallbackGroupIdTest, NonRootNamespace) { + // Arrange + auto node = std::make_shared("test_node", "my_ns"); + auto group = make_group(node); + auto sub = add_subscription(node, group, "/chatter"); + ASSERT_NE(sub, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/my_ns/test_node@Subscription(/chatter)"); +} + +// A service entity is rendered as Service(). +TEST_F(CreateCallbackGroupIdTest, ServiceEntity) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto service = add_service(node, group, "/srv"); + ASSERT_NE(service, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node@Service(/srv)"); +} + +// A client entity is rendered as Client(). +TEST_F(CreateCallbackGroupIdTest, ClientEntity) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto client = add_client(node, group, "/srv"); + ASSERT_NE(client, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node@Client(/srv)"); +} + +// A timer has no name, so it is identified by its period in nanoseconds. +// 100 ms == 100000000 ns. +TEST_F(CreateCallbackGroupIdTest, TimerEntityUsesPeriodNanoseconds) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto timer = add_timer(node, group, std::chrono::milliseconds(100)); + ASSERT_NE(timer, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node@Timer(100000000)"); +} + +// Multiple entities are joined with '@'. collect_all_ptrs emits them in the +// order: subscriptions, services, clients, then timers. This test pins that +// ordering and the internal '@' joining. +TEST_F(CreateCallbackGroupIdTest, MultipleEntitiesAreJoinedInStorageOrder) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto sub = add_subscription(node, group, "/chatter"); + auto timer = add_timer(node, group, std::chrono::milliseconds(100)); + auto service = add_service(node, group, "/srv"); + auto client = add_client(node, group, "/cli"); + ASSERT_NE(sub, nullptr); + ASSERT_NE(timer, nullptr); + ASSERT_NE(service, nullptr); + ASSERT_NE(client, nullptr); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node@Subscription(/chatter)@Service(/srv)@Client(/" + "cli)@Timer(100000000)"); +} + +// With no entities, only the bare "node@" prefix is built, so stripping the +// trailing '@' must yield exactly the node identifier and nothing else. +TEST_F(CreateCallbackGroupIdTest, EmptyGroupStripsTrailingSeparator) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + + // Act + const std::string id = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id, "/test_node"); +} + +// The Node overload must delegate to the NodeBaseInterface overload and yield +// an identical result. +TEST_F(CreateCallbackGroupIdTest, + NodeBaseInterfaceOverloadMatchesNodeOverload) { + // Arrange + auto node = std::make_shared("test_node"); + auto group = make_group(node); + auto sub = add_subscription(node, group, "/chatter"); + ASSERT_NE(sub, nullptr); + + // Act + const std::string id_base = + create_callback_group_id(group, node->get_node_base_interface()); + const std::string id_node = create_callback_group_id(group, node); + + // Assert + EXPECT_EQ(id_base, id_node); + EXPECT_EQ(id_base, "/test_node@Subscription(/chatter)"); +}