Skip to content

Commit 29fdb24

Browse files
Replace deprecated rclcpp::spin_some
1 parent 791eef0 commit 29fdb24

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

topic_tools/test/test_demux.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16+
#include <future>
1617
#include <memory>
1718
#include <string>
1819

@@ -57,7 +58,16 @@ class DemuxTest : public TestTopicToolMultiPub
5758
}
5859

5960
auto result = srv_client_->async_send_request(request);
60-
rclcpp::spin_some(target_node_);
61+
const auto timeout = std::chrono::steady_clock::now() + 1s;
62+
while (result.wait_for(0s) != std::future_status::ready &&
63+
std::chrono::steady_clock::now() < timeout)
64+
{
65+
executor_->spin_node_all(target_node_, std::chrono::nanoseconds(0));
66+
executor_->spin_some();
67+
}
68+
69+
ASSERT_EQ(result.wait_for(0s), std::future_status::ready);
70+
ASSERT_TRUE(result.get()->success);
6171
}
6272

6373
void publish_and_check(std::string msg_content)

topic_tools/test/test_mux.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <memory>
1616
#include <string>
1717
#include <chrono>
18+
#include <future>
1819
#include "gtest/gtest.h"
1920
#include "rclcpp/rclcpp.hpp"
2021
#include "std_msgs/msg/string.hpp"
@@ -62,7 +63,16 @@ class MuxTest : public TestTopicToolMultiSub
6263
}
6364

6465
auto result = srv_client_->async_send_request(request);
65-
rclcpp::spin_some(target_node_);
66+
const auto timeout = std::chrono::steady_clock::now() + 1s;
67+
while (result.wait_for(0s) != std::future_status::ready &&
68+
std::chrono::steady_clock::now() < timeout)
69+
{
70+
executor_->spin_node_all(target_node_, std::chrono::nanoseconds(0));
71+
executor_->spin_some();
72+
}
73+
74+
ASSERT_EQ(result.wait_for(0s), std::future_status::ready);
75+
ASSERT_TRUE(result.get()->success);
6676
}
6777

6878
void publish_and_check(

topic_tools/test/test_topic_tool.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class TestTopicToolSingleSub : public TestTopicTool
7171
const std::string test_name =
7272
::testing::UnitTest::GetInstance()->current_test_info()->name();
7373
test_node_ = rclcpp::Node::make_shared(test_name);
74+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
75+
executor_->add_node(test_node_);
7476
target_input_topic_ = "/" + test_name + "/input";
7577
target_output_topic_ = "/" + test_name + "/output";
7678
subscription_ = test_node_->create_subscription<std_msgs::msg::String>(
@@ -94,8 +96,8 @@ class TestTopicToolSingleSub : public TestTopicTool
9496
auto message = std_msgs::msg::String();
9597
message.data = msg_content;
9698
publisher_->publish(message);
97-
rclcpp::spin_some(target_node);
98-
rclcpp::spin_some(test_node_);
99+
executor_->spin_node_all(target_node, std::chrono::nanoseconds(0));
100+
executor_->spin_some();
99101
}
100102

101103
std::string get_target_input_topic()
@@ -112,6 +114,7 @@ class TestTopicToolSingleSub : public TestTopicTool
112114
std::shared_ptr<rclcpp::Node> test_node_;
113115
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
114116
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
117+
std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> executor_;
115118
std::string target_input_topic_;
116119
std::string target_output_topic_;
117120
};
@@ -125,6 +128,8 @@ class TestTopicToolMultiSub : public TestTopicTool
125128
const std::string test_name =
126129
::testing::UnitTest::GetInstance()->current_test_info()->name();
127130
test_node_ = rclcpp::Node::make_shared(test_name);
131+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
132+
executor_->add_node(test_node_);
128133
target_input_topic_prefix_ = "/" + test_name + "/input";
129134
target_output_topic_ = "/" + test_name + "/output";
130135
subscription_ = test_node_->create_subscription<std_msgs::msg::String>(
@@ -155,8 +160,8 @@ class TestTopicToolMultiSub : public TestTopicTool
155160
message.data = msg_content;
156161
assert(publisher_index < num_target_input_topics_);
157162
publishers_[publisher_index]->publish(message);
158-
rclcpp::spin_some(target_node);
159-
rclcpp::spin_some(test_node_);
163+
executor_->spin_node_all(target_node, std::chrono::nanoseconds(0));
164+
executor_->spin_some();
160165
}
161166

162167
std::vector<std::string> get_target_input_topics()
@@ -175,6 +180,7 @@ class TestTopicToolMultiSub : public TestTopicTool
175180

176181
protected:
177182
std::shared_ptr<rclcpp::Node> test_node_;
183+
std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> executor_;
178184

179185
private:
180186
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
@@ -193,6 +199,8 @@ class TestTopicToolMultiPub : public TestTopicTool
193199
using std::placeholders::_1;
194200
const std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
195201
test_node_ = rclcpp::Node::make_shared(test_name);
202+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
203+
executor_->add_node(test_node_);
196204
target_input_topic_ = "/" + test_name + "/input";
197205
target_output_topic_prefix_ = "/" + test_name + "/output";
198206
publisher_ = test_node_->create_publisher<std_msgs::msg::String>(target_input_topic_, 10);
@@ -219,8 +227,8 @@ class TestTopicToolMultiPub : public TestTopicTool
219227
auto message = std_msgs::msg::String();
220228
message.data = msg_content;
221229
publisher_->publish(message);
222-
rclcpp::spin_some(target_node);
223-
rclcpp::spin_some(test_node_);
230+
executor_->spin_node_all(target_node, std::chrono::nanoseconds(0));
231+
executor_->spin_some();
224232
}
225233

226234
std::vector<std::string> get_target_output_topics()
@@ -236,6 +244,7 @@ class TestTopicToolMultiPub : public TestTopicTool
236244

237245
protected:
238246
std::shared_ptr<rclcpp::Node> test_node_;
247+
std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> executor_;
239248

240249
private:
241250
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;

0 commit comments

Comments
 (0)