Skip to content

Commit

Permalink
Fix sim time test
Browse files Browse the repository at this point in the history
Partial backport of #1380.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron committed May 11, 2022
1 parent e1f31d2 commit 4e8ccd1
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions gazebo_ros/test/test_sim_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ TEST_F(TestSimTime, TestClock)
executor.add_node(node);

std::vector<ClockMsg::SharedPtr> msgs;
std::vector<rclcpp::Time> times;
auto sub = node->create_subscription<ClockMsg>(
"/clock", rclcpp::SystemDefaultsQoS(),
"/clock", rclcpp::ClockQoS(),
[&](ClockMsg::SharedPtr _msg) {
msgs.push_back(_msg);
times.push_back(node->now());
});

unsigned int sleep{0};
Expand All @@ -75,25 +73,17 @@ TEST_F(TestSimTime, TestClock)
}
EXPECT_LT(sleep, max_sleep);
EXPECT_EQ(5u, msgs.size());
EXPECT_EQ(5u, times.size());

// Check node starts at time zero
for (int i = 0; i < 5; ++i) {
// The SIM time should be close to zero (start of simulation)
EXPECT_LT(rclcpp::Time(msgs[i]->clock), rclcpp::Time(1, 0, RCL_ROS_TIME));
EXPECT_LT(times[i], rclcpp::Time(1, 0, RCL_ROS_TIME));

// Time should go forward
if (i > 0) {
EXPECT_GT(rclcpp::Time(msgs[i]->clock), rclcpp::Time(msgs[i - 1]->clock)) <<
rclcpp::Time(msgs[i]->clock).nanoseconds() << " " <<
rclcpp::Time(msgs[i - 1]->clock).nanoseconds();
EXPECT_GT(times[i], times[i - 1]) << times[i].nanoseconds() << " " <<
times[i - 1].nanoseconds();

// The message from the clock should be close to the node's time
// We can't guarantee they match exactly because the node may process clock messages late
EXPECT_NEAR(times[i].nanoseconds(), rclcpp::Time(msgs[i]->clock).nanoseconds(), 150000000);
}
}
}
Expand Down

0 comments on commit 4e8ccd1

Please sign in to comment.