Skip to content

Commit 2a803c2

Browse files
Replace rclcpp::spin_some and rclcpp::spin_all (#5521)
* Get rid of rclcpp spin all and spin some Signed-off-by: mini-1235 <[email protected]> * Linting Signed-off-by: mini-1235 <[email protected]> * Vector object server Signed-off-by: mini-1235 <[email protected]> * Linting Signed-off-by: mini-1235 <[email protected]> * Update nav2_rviz_plugins/src/utils.cpp Signed-off-by: Steve Macenski <[email protected]> * Error log Signed-off-by: Maurice <[email protected]> --------- Signed-off-by: mini-1235 <[email protected]> Signed-off-by: Steve Macenski <[email protected]> Signed-off-by: Maurice <[email protected]> Co-authored-by: Steve Macenski <[email protected]>
1 parent b45fca2 commit 2a803c2

File tree

49 files changed

+384
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+384
-191
lines changed

nav2_behavior_tree/test/plugins/action/test_bt_action_node.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,19 @@ class BTActionNodeTestFixture : public ::testing::Test
202202
action_server_ = std::make_shared<FibonacciActionServer>();
203203
server_thread_ = std::make_shared<std::thread>(
204204
[]() {
205+
rclcpp::executors::SingleThreadedExecutor executor;
206+
executor.add_node(action_server_);
205207
while (rclcpp::ok() && BTActionNodeTestFixture::action_server_ != nullptr) {
206-
rclcpp::spin_some(BTActionNodeTestFixture::action_server_);
208+
executor.spin_some();
207209
std::this_thread::sleep_for(100ns);
208210
}
209211
});
210212
}
211213

212214
void TearDown() override
213215
{
216+
// Sleep for some time to avoid race condition
217+
std::this_thread::sleep_for(std::chrono::milliseconds(80));
214218
action_server_.reset();
215219
tree_.reset();
216220
server_thread_->join();

nav2_behavior_tree/test/plugins/action/test_controller_selector_node.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ControllerSelectorTestFixture : public ::testing::Test
3131
static void SetUpTestCase()
3232
{
3333
node_ = std::make_shared<nav2::LifecycleNode>("controller_selector_test_fixture");
34+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
35+
executor_->add_node(node_->get_node_base_interface());
3436

3537
// Configure and activate the lifecycle node
3638
node_->configure();
@@ -67,6 +69,7 @@ class ControllerSelectorTestFixture : public ::testing::Test
6769
config_ = nullptr;
6870
node_.reset();
6971
factory_.reset();
72+
executor_.reset();
7073
}
7174

7275
void TearDown() override
@@ -76,12 +79,15 @@ class ControllerSelectorTestFixture : public ::testing::Test
7679

7780
protected:
7881
static nav2::LifecycleNode::SharedPtr node_;
82+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7983
static BT::NodeConfiguration * config_;
8084
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
8185
static std::shared_ptr<BT::Tree> tree_;
8286
};
8387

8488
nav2::LifecycleNode::SharedPtr ControllerSelectorTestFixture::node_ = nullptr;
89+
rclcpp::executors::SingleThreadedExecutor::SharedPtr ControllerSelectorTestFixture::executor_ =
90+
nullptr;
8591

8692
BT::NodeConfiguration * ControllerSelectorTestFixture::config_ = nullptr;
8793
std::shared_ptr<BT::BehaviorTreeFactory> ControllerSelectorTestFixture::factory_ = nullptr;
@@ -126,7 +132,7 @@ TEST_F(ControllerSelectorTestFixture, test_custom_topic)
126132
tree_->rootNode()->executeTick();
127133
controller_selector_pub->publish(selected_controller_cmd);
128134

129-
rclcpp::spin_some(node_->get_node_base_interface());
135+
executor_->spin_some();
130136
}
131137

132138
// check controller updated
@@ -173,7 +179,7 @@ TEST_F(ControllerSelectorTestFixture, test_default_topic)
173179
tree_->rootNode()->executeTick();
174180
controller_selector_pub->publish(selected_controller_cmd);
175181

176-
rclcpp::spin_some(node_->get_node_base_interface());
182+
executor_->spin_some();
177183
}
178184

179185
// check controller updated

nav2_behavior_tree/test/plugins/action/test_goal_checker_selector_node.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class GoalCheckerSelectorTestFixture : public ::testing::Test
3131
static void SetUpTestCase()
3232
{
3333
node_ = std::make_shared<nav2::LifecycleNode>("goal_checker_selector_test_fixture");
34+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
35+
executor_->add_node(node_->get_node_base_interface());
3436

3537
// Configure and activate the lifecycle node
3638
node_->configure();
@@ -67,6 +69,7 @@ class GoalCheckerSelectorTestFixture : public ::testing::Test
6769
config_ = nullptr;
6870
node_.reset();
6971
factory_.reset();
72+
executor_.reset();
7073
}
7174

7275
void TearDown() override
@@ -76,12 +79,15 @@ class GoalCheckerSelectorTestFixture : public ::testing::Test
7679

7780
protected:
7881
static nav2::LifecycleNode::SharedPtr node_;
82+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7983
static BT::NodeConfiguration * config_;
8084
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
8185
static std::shared_ptr<BT::Tree> tree_;
8286
};
8387

8488
nav2::LifecycleNode::SharedPtr GoalCheckerSelectorTestFixture::node_ = nullptr;
89+
rclcpp::executors::SingleThreadedExecutor::SharedPtr GoalCheckerSelectorTestFixture::executor_ =
90+
nullptr;
8591

8692
BT::NodeConfiguration * GoalCheckerSelectorTestFixture::config_ = nullptr;
8793
std::shared_ptr<BT::BehaviorTreeFactory> GoalCheckerSelectorTestFixture::factory_ = nullptr;
@@ -127,7 +133,7 @@ TEST_F(GoalCheckerSelectorTestFixture, test_custom_topic)
127133
tree_->rootNode()->executeTick();
128134
goal_checker_selector_pub->publish(selected_goal_checker_cmd);
129135

130-
rclcpp::spin_some(node_->get_node_base_interface());
136+
executor_->spin_some();
131137
}
132138

133139
// check goal_checker updated
@@ -175,7 +181,7 @@ TEST_F(GoalCheckerSelectorTestFixture, test_default_topic)
175181
tree_->rootNode()->executeTick();
176182
goal_checker_selector_pub->publish(selected_goal_checker_cmd);
177183

178-
rclcpp::spin_some(node_->get_node_base_interface());
184+
executor_->spin_some();
179185
}
180186

181187
// check goal_checker updated

nav2_behavior_tree/test/plugins/action/test_planner_selector_node.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class PlannerSelectorTestFixture : public ::testing::Test
3131
static void SetUpTestCase()
3232
{
3333
node_ = std::make_shared<nav2::LifecycleNode>("planner_selector_test_fixture");
34+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
35+
executor_->add_node(node_->get_node_base_interface());
3436

3537
// Configure and activate the lifecycle node
3638
node_->configure();
@@ -65,6 +67,7 @@ class PlannerSelectorTestFixture : public ::testing::Test
6567
config_ = nullptr;
6668
node_.reset();
6769
factory_.reset();
70+
executor_.reset();
6871
}
6972

7073
void TearDown() override
@@ -74,12 +77,15 @@ class PlannerSelectorTestFixture : public ::testing::Test
7477

7578
protected:
7679
static nav2::LifecycleNode::SharedPtr node_;
80+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7781
static BT::NodeConfiguration * config_;
7882
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
7983
static std::shared_ptr<BT::Tree> tree_;
8084
};
8185

8286
nav2::LifecycleNode::SharedPtr PlannerSelectorTestFixture::node_ = nullptr;
87+
rclcpp::executors::SingleThreadedExecutor::SharedPtr PlannerSelectorTestFixture::executor_ =
88+
nullptr;
8389

8490
BT::NodeConfiguration * PlannerSelectorTestFixture::config_ = nullptr;
8591
std::shared_ptr<BT::BehaviorTreeFactory> PlannerSelectorTestFixture::factory_ = nullptr;
@@ -125,7 +131,7 @@ TEST_F(PlannerSelectorTestFixture, test_custom_topic)
125131
tree_->rootNode()->executeTick();
126132
planner_selector_pub->publish(selected_planner_cmd);
127133

128-
rclcpp::spin_some(node_->get_node_base_interface());
134+
executor_->spin_some();
129135
}
130136

131137
// check planner updated
@@ -173,7 +179,7 @@ TEST_F(PlannerSelectorTestFixture, test_default_topic)
173179
tree_->rootNode()->executeTick();
174180
planner_selector_pub->publish(selected_planner_cmd);
175181

176-
rclcpp::spin_some(node_->get_node_base_interface());
182+
executor_->spin_some();
177183
}
178184

179185
// check planner updated

nav2_behavior_tree/test/plugins/action/test_progress_checker_selector_node.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ProgressCheckerSelectorTestFixture : public ::testing::Test
3030
static void SetUpTestCase()
3131
{
3232
node_ = std::make_shared<nav2::LifecycleNode>("progress_checker_selector_test_fixture");
33+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
34+
executor_->add_node(node_->get_node_base_interface());
3335

3436
// Configure and activate the lifecycle node
3537
node_->configure();
@@ -66,6 +68,7 @@ class ProgressCheckerSelectorTestFixture : public ::testing::Test
6668
config_ = nullptr;
6769
node_.reset();
6870
factory_.reset();
71+
executor_.reset();
6972
}
7073

7174
void TearDown() override
@@ -75,12 +78,15 @@ class ProgressCheckerSelectorTestFixture : public ::testing::Test
7578

7679
protected:
7780
static nav2::LifecycleNode::SharedPtr node_;
81+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7882
static BT::NodeConfiguration * config_;
7983
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
8084
static std::shared_ptr<BT::Tree> tree_;
8185
};
8286

8387
nav2::LifecycleNode::SharedPtr ProgressCheckerSelectorTestFixture::node_ = nullptr;
88+
rclcpp::executors::SingleThreadedExecutor::SharedPtr ProgressCheckerSelectorTestFixture::
89+
executor_ = nullptr;
8490

8591
BT::NodeConfiguration * ProgressCheckerSelectorTestFixture::config_ = nullptr;
8692
std::shared_ptr<BT::BehaviorTreeFactory> ProgressCheckerSelectorTestFixture::factory_ = nullptr;
@@ -129,7 +135,7 @@ TEST_F(ProgressCheckerSelectorTestFixture, test_custom_topic)
129135
tree_->rootNode()->executeTick();
130136
progress_checker_selector_pub->publish(selected_progress_checker_cmd);
131137

132-
rclcpp::spin_some(node_->get_node_base_interface());
138+
executor_->spin_some();
133139
}
134140

135141
// check progress_checker updated
@@ -179,7 +185,7 @@ TEST_F(ProgressCheckerSelectorTestFixture, test_default_topic)
179185
tree_->rootNode()->executeTick();
180186
progress_checker_selector_pub->publish(selected_progress_checker_cmd);
181187

182-
rclcpp::spin_some(node_->get_node_base_interface());
188+
executor_->spin_some();
183189
}
184190

185191
// check goal_checker updated

nav2_behavior_tree/test/plugins/action/test_smoother_selector_node.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class SmootherSelectorTestFixture : public ::testing::Test
3131
static void SetUpTestCase()
3232
{
3333
node_ = std::make_shared<nav2::LifecycleNode>("smoother_selector_test_fixture");
34+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
35+
executor_->add_node(node_->get_node_base_interface());
3436

3537
// Configure and activate the lifecycle node
3638
node_->configure();
@@ -67,6 +69,7 @@ class SmootherSelectorTestFixture : public ::testing::Test
6769
config_ = nullptr;
6870
node_.reset();
6971
factory_.reset();
72+
executor_.reset();
7073
}
7174

7275
void TearDown() override
@@ -76,12 +79,15 @@ class SmootherSelectorTestFixture : public ::testing::Test
7679

7780
protected:
7881
static nav2::LifecycleNode::SharedPtr node_;
82+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7983
static BT::NodeConfiguration * config_;
8084
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
8185
static std::shared_ptr<BT::Tree> tree_;
8286
};
8387

8488
nav2::LifecycleNode::SharedPtr SmootherSelectorTestFixture::node_ = nullptr;
89+
rclcpp::executors::SingleThreadedExecutor::SharedPtr SmootherSelectorTestFixture::executor_ =
90+
nullptr;
8591

8692
BT::NodeConfiguration * SmootherSelectorTestFixture::config_ = nullptr;
8793
std::shared_ptr<BT::BehaviorTreeFactory> SmootherSelectorTestFixture::factory_ = nullptr;
@@ -127,7 +133,7 @@ TEST_F(SmootherSelectorTestFixture, test_custom_topic)
127133
tree_->rootNode()->executeTick();
128134
smoother_selector_pub->publish(selected_smoother_cmd);
129135

130-
rclcpp::spin_some(node_->get_node_base_interface());
136+
executor_->spin_some();
131137
}
132138

133139
// check smoother updated
@@ -175,7 +181,7 @@ TEST_F(SmootherSelectorTestFixture, test_default_topic)
175181
tree_->rootNode()->executeTick();
176182
smoother_selector_pub->publish(selected_smoother_cmd);
177183

178-
rclcpp::spin_some(node_->get_node_base_interface());
184+
executor_->spin_some();
179185
}
180186

181187
// check smoother updated

nav2_behavior_tree/test/plugins/condition/test_is_battery_charging.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class IsBatteryChargingConditionTestFixture : public ::testing::Test
2929
static void SetUpTestCase()
3030
{
3131
node_ = std::make_shared<nav2::LifecycleNode>("test_is_battery_charging");
32+
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
33+
executor_->add_node(node_->get_node_base_interface());
3234
factory_ = std::make_shared<BT::BehaviorTreeFactory>();
3335

3436
config_ = new BT::NodeConfiguration();
@@ -58,17 +60,21 @@ class IsBatteryChargingConditionTestFixture : public ::testing::Test
5860
battery_pub_.reset();
5961
node_.reset();
6062
factory_.reset();
63+
executor_.reset();
6164
}
6265

6366
protected:
6467
static nav2::LifecycleNode::SharedPtr node_;
68+
static rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
6569
static BT::NodeConfiguration * config_;
6670
static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
6771
static nav2::Publisher<sensor_msgs::msg::BatteryState>::SharedPtr
6872
battery_pub_;
6973
};
7074

7175
nav2::LifecycleNode::SharedPtr IsBatteryChargingConditionTestFixture::node_ = nullptr;
76+
rclcpp::executors::SingleThreadedExecutor::SharedPtr IsBatteryChargingConditionTestFixture::
77+
executor_ = nullptr;
7278
BT::NodeConfiguration * IsBatteryChargingConditionTestFixture::config_ = nullptr;
7379
std::shared_ptr<BT::BehaviorTreeFactory> IsBatteryChargingConditionTestFixture::factory_ = nullptr;
7480
nav2::Publisher<sensor_msgs::msg::BatteryState>::SharedPtr
@@ -90,32 +96,32 @@ TEST_F(IsBatteryChargingConditionTestFixture, test_behavior_power_supply_status)
9096
battery_msg.power_supply_status = sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_UNKNOWN;
9197
battery_pub_->publish(battery_msg);
9298
std::this_thread::sleep_for(std::chrono::milliseconds(100));
93-
rclcpp::spin_some(node_->get_node_base_interface());
99+
executor_->spin_some();
94100
EXPECT_EQ(tree.tickOnce(), BT::NodeStatus::FAILURE);
95101

96102
battery_msg.power_supply_status = sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_CHARGING;
97103
battery_pub_->publish(battery_msg);
98104
std::this_thread::sleep_for(std::chrono::milliseconds(100));
99-
rclcpp::spin_some(node_->get_node_base_interface());
105+
executor_->spin_some();
100106
EXPECT_EQ(tree.tickOnce(), BT::NodeStatus::SUCCESS);
101107

102108
battery_msg.power_supply_status = sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_DISCHARGING;
103109
battery_pub_->publish(battery_msg);
104110
std::this_thread::sleep_for(std::chrono::milliseconds(100));
105-
rclcpp::spin_some(node_->get_node_base_interface());
111+
executor_->spin_some();
106112
EXPECT_EQ(tree.tickOnce(), BT::NodeStatus::FAILURE);
107113

108114
battery_msg.power_supply_status =
109115
sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_NOT_CHARGING;
110116
battery_pub_->publish(battery_msg);
111117
std::this_thread::sleep_for(std::chrono::milliseconds(100));
112-
rclcpp::spin_some(node_->get_node_base_interface());
118+
executor_->spin_some();
113119
EXPECT_EQ(tree.tickOnce(), BT::NodeStatus::FAILURE);
114120

115121
battery_msg.power_supply_status = sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_FULL;
116122
battery_pub_->publish(battery_msg);
117123
std::this_thread::sleep_for(std::chrono::milliseconds(100));
118-
rclcpp::spin_some(node_->get_node_base_interface());
124+
executor_->spin_some();
119125
EXPECT_EQ(tree.tickOnce(), BT::NodeStatus::FAILURE);
120126
}
121127

0 commit comments

Comments
 (0)