Skip to content

Commit bd8e60c

Browse files
committed
Fix threading problem in unit test
1 parent 1913f62 commit bd8e60c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

fuse_optimizers/test/launch_tests/test_fixed_lag_ignition.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
*/
3434
#include <gtest/gtest.h>
3535

36+
#include <memory>
37+
#include <mutex>
38+
#include <thread>
39+
3640
#include <fuse_msgs/srv/set_pose.hpp>
3741
#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
3842
#include <nav_msgs/msg/odometry.hpp>
@@ -67,12 +71,20 @@ class FixedLagIgnitionFixture : public ::testing::Test
6771

6872
void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg)
6973
{
74+
std::lock_guard lock(received_odom_mutex_);
7075
received_odom_msg_ = msg;
7176
}
7277

78+
nav_msgs::msg::Odometry::SharedPtr get_last_odom_msg()
79+
{
80+
std::lock_guard lock(received_odom_mutex_);
81+
return received_odom_msg_;
82+
}
83+
7384
std::thread spinner_; //!< Internal thread for spinning the executor
7485
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
7586
nav_msgs::msg::Odometry::SharedPtr received_odom_msg_;
87+
std::mutex received_odom_mutex_;
7688
};
7789

7890
TEST_F(FixedLagIgnitionFixture, SetInitialState)
@@ -162,20 +174,23 @@ TEST_F(FixedLagIgnitionFixture, SetInitialState)
162174

163175
// Wait for the optimizer to process all queued transactions and publish the last odometry msg
164176
rclcpp::Time result_timeout = node->now() + rclcpp::Duration::from_seconds(1.0);
165-
while ((!received_odom_msg_ || received_odom_msg_->header.stamp != rclcpp::Time(3, 0,
177+
auto odom_msg = nav_msgs::msg::Odometry::SharedPtr();
178+
while ((!odom_msg || odom_msg->header.stamp != rclcpp::Time(3, 0,
166179
RCL_ROS_TIME)) && (node->now() < result_timeout))
167180
{
168181
rclcpp::sleep_for(std::chrono::milliseconds(100));
182+
odom_msg = this->get_last_odom_msg();
169183
}
170-
ASSERT_EQ(rclcpp::Time(received_odom_msg_->header.stamp), rclcpp::Time(3, 0, RCL_ROS_TIME));
184+
ASSERT_TRUE(static_cast<bool>(odom_msg));
185+
ASSERT_EQ(rclcpp::Time(odom_msg->header.stamp), rclcpp::Time(3, 0, RCL_ROS_TIME));
171186

172187
// The optimizer is configured for 0 iterations, so it should return the initial variable values
173188
// If we did our job correctly, the initial variable values should be the same as the service call
174189
// state, give or take the motion model forward prediction.
175-
EXPECT_NEAR(100.1, received_odom_msg_->pose.pose.position.x, 0.10);
176-
EXPECT_NEAR(100.2, received_odom_msg_->pose.pose.position.y, 0.10);
177-
EXPECT_NEAR(0.8660, received_odom_msg_->pose.pose.orientation.z, 0.10);
178-
EXPECT_NEAR(0.5000, received_odom_msg_->pose.pose.orientation.w, 0.10);
190+
EXPECT_NEAR(100.1, odom_msg->pose.pose.position.x, 0.10);
191+
EXPECT_NEAR(100.2, odom_msg->pose.pose.position.y, 0.10);
192+
EXPECT_NEAR(0.8660, odom_msg->pose.pose.orientation.z, 0.10);
193+
EXPECT_NEAR(0.5000, odom_msg->pose.pose.orientation.w, 0.10);
179194
}
180195

181196
// NOTE(CH3): This main is required because the test is manually run by a launch test

0 commit comments

Comments
 (0)