From 5cc0854a3ead0c4260c0996c21b2881a91aabf61 Mon Sep 17 00:00:00 2001 From: kminoda <44218668+kminoda@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:42:38 +0900 Subject: [PATCH] feat(autoware_test_utils): add autoware test manager (#7597) * feat(detected_object_validation): add test Signed-off-by: kminoda * move to autoware_test_utils Signed-off-by: kminoda * remove perception Signed-off-by: kminoda * update cmake Signed-off-by: kminoda * style(pre-commit): autofix * remove perception change Signed-off-by: kminoda * add include Signed-off-by: kminoda * refactored Signed-off-by: kminoda * avoid using void and static_pointer_cast Signed-off-by: kminoda --------- Signed-off-by: kminoda Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- common/autoware_test_utils/CMakeLists.txt | 9 ++- .../autoware_test_utils.hpp | 57 +++++++++++++++ .../test/test_autoware_test_manager.cpp | 71 +++++++++++++++++++ 3 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 common/autoware_test_utils/test/test_autoware_test_manager.cpp diff --git a/common/autoware_test_utils/CMakeLists.txt b/common/autoware_test_utils/CMakeLists.txt index 91adf935445b4..6a12539d06cbf 100644 --- a/common/autoware_test_utils/CMakeLists.txt +++ b/common/autoware_test_utils/CMakeLists.txt @@ -15,11 +15,10 @@ target_link_libraries(mock_data_parser ) if(BUILD_TESTING) - ament_add_ros_isolated_gtest(test_mock_data_parser - test/test_mock_data_parser.cpp) - - target_link_libraries(test_mock_data_parser - mock_data_parser) + ament_auto_add_gtest(test_autoware_test_utils + test/test_mock_data_parser.cpp + test/test_autoware_test_manager.cpp + ) endif() ament_auto_package(INSTALL_TO_SHARE diff --git a/common/autoware_test_utils/include/autoware_test_utils/autoware_test_utils.hpp b/common/autoware_test_utils/include/autoware_test_utils/autoware_test_utils.hpp index 49d2c848ab508..d7de1397bfaac 100644 --- a/common/autoware_test_utils/include/autoware_test_utils/autoware_test_utils.hpp +++ b/common/autoware_test_utils/include/autoware_test_utils/autoware_test_utils.hpp @@ -48,6 +48,7 @@ #include #include #include +#include #include namespace autoware::test_utils @@ -512,6 +513,62 @@ void publishToTargetNode( autoware::test_utils::spinSomeNodes(test_node, target_node, repeat_count); } +/** + * @brief Manages publishing and subscribing to ROS topics for testing Autoware. + * + * The AutowareTestManager class provides utility functions to facilitate + * the publishing of messages to specified topics and the setting up of + * subscribers to listen for messages on specified topics. This class + * simplifies the setup of test environments in Autoware. + */ +class AutowareTestManager +{ +public: + AutowareTestManager() + { + test_node_ = std::make_shared("autoware_test_manager_node"); + } + + template + void test_pub_msg( + rclcpp::Node::SharedPtr target_node, const std::string & topic_name, MessageType & msg) + { + if (publishers_.find(topic_name) == publishers_.end()) { + auto publisher = test_node_->create_publisher(topic_name, 10); + publishers_[topic_name] = std::static_pointer_cast(publisher); + } + + auto publisher = + std::dynamic_pointer_cast>(publishers_[topic_name]); + + autoware::test_utils::publishToTargetNode(test_node_, target_node, topic_name, publisher, msg); + RCLCPP_INFO(test_node_->get_logger(), "Published message on topic '%s'", topic_name.c_str()); + } + + template + void set_subscriber( + const std::string & topic_name, + std::function callback) + { + if (subscribers_.find(topic_name) == subscribers_.end()) { + std::shared_ptr> subscriber; + autoware::test_utils::createSubscription( + test_node_, topic_name, callback, subscriber); + subscribers_[topic_name] = std::static_pointer_cast(subscriber); + } else { + RCLCPP_WARN(test_node_->get_logger(), "Subscriber %s already set.", topic_name.c_str()); + } + } + +protected: + // Publisher + std::unordered_map> publishers_; + std::unordered_map> subscribers_; + + // Node + rclcpp::Node::SharedPtr test_node_; +}; // class AutowareTestManager + } // namespace autoware::test_utils #endif // AUTOWARE_TEST_UTILS__AUTOWARE_TEST_UTILS_HPP_ diff --git a/common/autoware_test_utils/test/test_autoware_test_manager.cpp b/common/autoware_test_utils/test/test_autoware_test_manager.cpp new file mode 100644 index 0000000000000..688ec9df9ad42 --- /dev/null +++ b/common/autoware_test_utils/test/test_autoware_test_manager.cpp @@ -0,0 +1,71 @@ +// Copyright 2024 TIER IV +// +// 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 "autoware_test_utils/autoware_test_utils.hpp" + +#include + +#include + +#include + +class RelayNode : public rclcpp::Node +{ +public: + RelayNode() : Node("relay_node") + { + subscription_ = this->create_subscription( + "input_topic", 10, [this](const std_msgs::msg::String::ConstSharedPtr msg) { + RCLCPP_INFO(this->get_logger(), "Received message: %s", msg->data.c_str()); + auto new_msg = std::make_shared(); + new_msg->data = msg->data; + publisher_->publish(*new_msg); + }); + + publisher_ = this->create_publisher("output_topic", 10); + } + +private: + rclcpp::Subscription::SharedPtr subscription_; + rclcpp::Publisher::SharedPtr publisher_; +}; + +TEST(AutowareTestManagerTest, TestRelayNode) +{ + rclcpp::init(0, nullptr); + + const std::string input_topic_name = "input_topic"; + const std::string output_topic_name = "output_topic"; + + // Setup target node and its test manager + auto target_node = std::make_shared(); + auto manager = std::make_shared(); + + // Setup subscriber for test manager + std::string received_msg; + manager->set_subscriber( + "output_topic", + [&received_msg](const std_msgs::msg::String::ConstSharedPtr msg) { received_msg = msg->data; }); + + // Publish a message to the relay node + std_msgs::msg::String msg; + msg.data = "Hello, Relay!"; + manager->test_pub_msg(target_node, input_topic_name, msg); + + // Spin to process callbacks + rclcpp::spin_some(target_node); + + // Check that the message was relayed and received by the test node + EXPECT_EQ(received_msg, "Hello, Relay!"); +}