@@ -63,10 +63,10 @@ class RemoveInCollisionGoalsFailureService : public TestService<nav2_msgs::srv::
6363 }
6464};
6565
66- class RemoveInCollisionGoalsTestFixture : public ::testing::Test
66+ class BTTestEnv
6767{
6868public:
69- static void SetUpTestCase ()
69+ BTTestEnv ()
7070 {
7171 node_ = std::make_shared<rclcpp::Node>(" in_collision_goals_test_fixture" );
7272 factory_ = std::make_shared<BT::BehaviorTreeFactory>();
@@ -100,7 +100,7 @@ class RemoveInCollisionGoalsTestFixture : public ::testing::Test
100100 " RemoveInCollisionGoals" , builder);
101101 }
102102
103- static void TearDownTestCase ()
103+ ~BTTestEnv ()
104104 {
105105 delete config_;
106106 config_ = nullptr ;
@@ -109,31 +109,16 @@ class RemoveInCollisionGoalsTestFixture : public ::testing::Test
109109 factory_.reset ();
110110 }
111111
112- void TearDown () override
113- {
114- tree_.reset ();
115- }
116- static std::shared_ptr<RemoveInCollisionGoalsSuccessService> success_server_;
117- static std::shared_ptr<RemoveInCollisionGoalsFailureService> failure_server_;
118-
119- protected:
120- static rclcpp::Node::SharedPtr node_;
121- static BT::NodeConfiguration * config_;
122- static std::shared_ptr<BT::BehaviorTreeFactory> factory_;
123- static std::shared_ptr<BT::Tree> tree_;
112+ std::shared_ptr<RemoveInCollisionGoalsSuccessService> success_server_;
113+ std::shared_ptr<RemoveInCollisionGoalsFailureService> failure_server_;
114+ rclcpp::Node::SharedPtr node_;
115+ BT::NodeConfiguration * config_;
116+ std::shared_ptr<BT::BehaviorTreeFactory> factory_;
124117};
125118
126- rclcpp::Node::SharedPtr RemoveInCollisionGoalsTestFixture::node_ = nullptr ;
127-
128- BT::NodeConfiguration * RemoveInCollisionGoalsTestFixture::config_ = nullptr ;
129- std::shared_ptr<RemoveInCollisionGoalsSuccessService>
130- RemoveInCollisionGoalsTestFixture::success_server_ = nullptr ;
131- std::shared_ptr<RemoveInCollisionGoalsFailureService>
132- RemoveInCollisionGoalsTestFixture::failure_server_ = nullptr ;
133- std::shared_ptr<BT::BehaviorTreeFactory> RemoveInCollisionGoalsTestFixture::factory_ = nullptr ;
134- std::shared_ptr<BT::Tree> RemoveInCollisionGoalsTestFixture::tree_ = nullptr ;
119+ BTTestEnv * test_env = nullptr ;
135120
136- TEST_F (RemoveInCollisionGoalsTestFixture , test_tick_remove_in_collision_goals_success)
121+ TEST (RemoveInCollisionGoalsTest , test_tick_remove_in_collision_goals_success)
137122{
138123 // create tree
139124 std::string xml_txt =
@@ -144,7 +129,7 @@ TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_su
144129 </BehaviorTree>
145130 </root>)" ;
146131
147- tree_ = std::make_shared< BT::Tree>( factory_->createTreeFromText (xml_txt, config_->blackboard ) );
132+ BT::Tree tree_ = test_env-> factory_ ->createTreeFromText (xml_txt, test_env-> config_ ->blackboard );
148133
149134 // create new goal and set it on blackboard
150135 geometry_msgs::msg::PoseStampedArray poses;
@@ -161,26 +146,26 @@ TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_su
161146 poses.poses [3 ].pose .position .x = 2.0 ;
162147 poses.poses [3 ].pose .position .y = 0.0 ;
163148
164- config_->blackboard ->set (" goals" , poses);
149+ test_env-> config_ ->blackboard ->set (" goals" , poses);
165150
166151 // tick until node is not running
167- tree_-> rootNode ()->executeTick ();
168- while (tree_-> rootNode ()->status () == BT::NodeStatus::RUNNING) {
169- tree_-> rootNode ()->executeTick ();
152+ tree_. rootNode ()->executeTick ();
153+ while (tree_. rootNode ()->status () == BT::NodeStatus::RUNNING) {
154+ tree_. rootNode ()->executeTick ();
170155 }
171156
172- EXPECT_EQ (tree_-> rootNode ()->status (), BT::NodeStatus::SUCCESS);
157+ EXPECT_EQ (tree_. rootNode ()->status (), BT::NodeStatus::SUCCESS);
173158 // check that it removed the point in range
174159 geometry_msgs::msg::PoseStampedArray output_poses;
175- EXPECT_TRUE (config_->blackboard ->get (" goals" , output_poses));
160+ EXPECT_TRUE (test_env-> config_ ->blackboard ->get (" goals" , output_poses));
176161
177162 EXPECT_EQ (output_poses.poses .size (), 3u );
178163 EXPECT_EQ (output_poses.poses [0 ], poses.poses [0 ]);
179164 EXPECT_EQ (output_poses.poses [1 ], poses.poses [1 ]);
180165 EXPECT_EQ (output_poses.poses [2 ], poses.poses [2 ]);
181166}
182167
183- TEST_F (RemoveInCollisionGoalsTestFixture , test_tick_remove_in_collision_goals_fail)
168+ TEST (RemoveInCollisionGoalsTest , test_tick_remove_in_collision_goals_fail)
184169{
185170 // create tree
186171 std::string xml_txt =
@@ -191,7 +176,7 @@ TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_fa
191176 </BehaviorTree>
192177 </root>)" ;
193178
194- tree_ = std::make_shared< BT::Tree>( factory_->createTreeFromText (xml_txt, config_->blackboard ) );
179+ BT::Tree tree_ = test_env-> factory_ ->createTreeFromText (xml_txt, test_env-> config_ ->blackboard );
195180
196181 // create new goal and set it on blackboard
197182 geometry_msgs::msg::PoseStampedArray poses;
@@ -208,18 +193,18 @@ TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_fa
208193 poses.poses [3 ].pose .position .x = 2.0 ;
209194 poses.poses [3 ].pose .position .y = 0.0 ;
210195
211- config_->blackboard ->set (" goals" , poses);
196+ test_env-> config_ ->blackboard ->set (" goals" , poses);
212197
213198 // tick until node is not running
214- tree_-> rootNode ()->executeTick ();
215- while (tree_-> rootNode ()->status () == BT::NodeStatus::RUNNING) {
216- tree_-> rootNode ()->executeTick ();
199+ tree_. rootNode ()->executeTick ();
200+ while (tree_. rootNode ()->status () == BT::NodeStatus::RUNNING) {
201+ tree_. rootNode ()->executeTick ();
217202 }
218203
219204 // check that it failed and returned the original goals
220- EXPECT_EQ (tree_-> rootNode ()->status (), BT::NodeStatus::FAILURE);
205+ EXPECT_EQ (tree_. rootNode ()->status (), BT::NodeStatus::FAILURE);
221206 geometry_msgs::msg::PoseStampedArray output_poses;
222- EXPECT_TRUE (config_->blackboard ->get (" goals" , output_poses));
207+ EXPECT_TRUE (test_env-> config_ ->blackboard ->get (" goals" , output_poses));
223208
224209 EXPECT_EQ (output_poses.poses .size (), 4u );
225210 EXPECT_EQ (output_poses.poses [0 ], poses.poses [0 ]);
@@ -235,25 +220,41 @@ int main(int argc, char ** argv)
235220 // initialize ROS
236221 rclcpp::init (argc, argv);
237222
223+ // initialize test environment
224+ test_env = new BTTestEnv ();
225+
226+ // stop signal
227+ std::atomic_bool stop_thread (false );
228+
238229 // initialize service and spin on new thread
239- RemoveInCollisionGoalsTestFixture:: success_server_ =
230+ test_env-> success_server_ =
240231 std::make_shared<RemoveInCollisionGoalsSuccessService>();
241- std::thread success_server_thread ([]() {
242- rclcpp::spin (RemoveInCollisionGoalsTestFixture::success_server_);
232+ std::thread success_server_thread ([&stop_thread]() {
233+ while (!stop_thread.load ()) {
234+ rclcpp::spin_some (test_env->success_server_ );
235+ }
243236 });
244237
245- RemoveInCollisionGoalsTestFixture:: failure_server_ =
238+ test_env-> failure_server_ =
246239 std::make_shared<RemoveInCollisionGoalsFailureService>();
247- std::thread failure_server_thread ([]() {
248- rclcpp::spin (RemoveInCollisionGoalsTestFixture::failure_server_);
240+ std::thread failure_server_thread ([&stop_thread]() {
241+ while (!stop_thread.load ()) {
242+ rclcpp::spin_some (test_env->failure_server_ );
243+ }
249244 });
250245
251246 int all_successful = RUN_ALL_TESTS ();
252247
253- // shutdown ROS
254- rclcpp::shutdown ( );
248+ // stop the threads
249+ stop_thread. store ( true );
255250 success_server_thread.join ();
256251 failure_server_thread.join ();
257252
253+ // clean the test environment
254+ delete test_env;
255+
256+ // shutdown ROS
257+ rclcpp::shutdown ();
258+
258259 return all_successful;
259260}
0 commit comments