From 5827423bd3dd24f9c9b6717c7edc1574dace080f Mon Sep 17 00:00:00 2001 From: "Takagi, Isamu" Date: Thu, 6 Mar 2025 20:16:40 +0900 Subject: [PATCH] add mock node Signed-off-by: Takagi, Isamu --- .../autoware_utils_testing/mock_node.hpp | 28 ++++++++++ .../include/autoware_utils_testing/runner.hpp | 23 -------- .../autoware_utils_testing/spin_thread.hpp | 54 +++++++++++++++++++ 3 files changed, 82 insertions(+), 23 deletions(-) delete mode 100644 autoware_utils_testing/include/autoware_utils_testing/runner.hpp create mode 100644 autoware_utils_testing/include/autoware_utils_testing/spin_thread.hpp diff --git a/autoware_utils_testing/include/autoware_utils_testing/mock_node.hpp b/autoware_utils_testing/include/autoware_utils_testing/mock_node.hpp index d56f734..b17d079 100644 --- a/autoware_utils_testing/include/autoware_utils_testing/mock_node.hpp +++ b/autoware_utils_testing/include/autoware_utils_testing/mock_node.hpp @@ -17,19 +17,47 @@ #include +#include #include +#include +#include namespace autoware_utils_testing { +template +struct Topic +{ + int count; + std::vector data; +}; + class MockNode : public rclcpp::Node { public: explicit MockNode(const std::string & name) : Node(name) {} + template + std::shared_ptr> sub(const std::string & name, const rclcpp::QoS & qos); + private: + std::unordered_map> topics_; + std::unordered_map subs_; }; +template +std::shared_ptr> MockNode::sub(const std::string & name, const rclcpp::QoS & qos) +{ + const auto topic = std::make_shared>(); + const auto sub = create_subscription(name, qos, [this, topic](const T & msg) { + topic->count += 1; + topic->data.push_back(msg); + }); + subs_[name] = sub; + topics_[name] = topic; + return topic; +} + } // namespace autoware_utils_testing #endif // AUTOWARE_UTILS_TESTING__MOCK_NODE_HPP_ diff --git a/autoware_utils_testing/include/autoware_utils_testing/runner.hpp b/autoware_utils_testing/include/autoware_utils_testing/runner.hpp deleted file mode 100644 index 80bb819..0000000 --- a/autoware_utils_testing/include/autoware_utils_testing/runner.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2025 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. - -#ifndef AUTOWARE_UTILS_TESTING__RUNNER_HPP_ -#define AUTOWARE_UTILS_TESTING__RUNNER_HPP_ - -namespace autoware_utils_testing -{ - -} // namespace autoware_utils_testing - -#endif // AUTOWARE_UTILS_TESTING__RUNNER_HPP_ diff --git a/autoware_utils_testing/include/autoware_utils_testing/spin_thread.hpp b/autoware_utils_testing/include/autoware_utils_testing/spin_thread.hpp new file mode 100644 index 0000000..6e5cdee --- /dev/null +++ b/autoware_utils_testing/include/autoware_utils_testing/spin_thread.hpp @@ -0,0 +1,54 @@ +// Copyright 2025 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. + +#ifndef AUTOWARE_UTILS_TESTING__SPIN_THREAD_HPP_ +#define AUTOWARE_UTILS_TESTING__SPIN_THREAD_HPP_ + +#include + +#include +#include +#include + +namespace autoware_utils_testing +{ + +template +class SpinThread +{ +public: + void start() + { + thread_ = std::thread([this] { executor_.spin(); }); + while (rclcpp::ok() && !executor_.is_spinning()) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + + void stop() + { + executor_.cancel(); + thread_.join(); + } + + void add_node(std::shared_ptr node) { executor_.add_node(node); } + +private: + Executor executor_; + std::thread thread_; +}; + +} // namespace autoware_utils_testing + +#endif // AUTOWARE_UTILS_TESTING__SPIN_THREAD_HPP_