Skip to content

Commit

Permalink
Test fix: initialize the global context to avoid runtime_error upon d…
Browse files Browse the repository at this point in the history
…estruction (#128)
  • Loading branch information
saikishor authored Nov 13, 2023
1 parent e2f14c1 commit 5e7c300
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions test/realtime_clock_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ using realtime_tools::RealtimeClock;

TEST(RealtimeClock, get_system_time)
{
// initialize the global context
rclcpp::init(0, nullptr);
const int ATTEMPTS = 10;
const std::chrono::milliseconds DELAY(1);

rclcpp::Clock::SharedPtr clock(new rclcpp::Clock());
RealtimeClock rt_clock(clock);
// Wait for time to be available
rclcpp::Time last_rt_time;
for (int i = 0; i < ATTEMPTS && rclcpp::Time() == last_rt_time; ++i) {
std::this_thread::sleep_for(DELAY);
last_rt_time = rt_clock.now(rclcpp::Time());
}
ASSERT_NE(rclcpp::Time(), last_rt_time);
{
RealtimeClock rt_clock(clock);
// Wait for time to be available
rclcpp::Time last_rt_time;
for (int i = 0; i < ATTEMPTS && rclcpp::Time() == last_rt_time; ++i) {
std::this_thread::sleep_for(DELAY);
last_rt_time = rt_clock.now(rclcpp::Time());
}
ASSERT_NE(rclcpp::Time(), last_rt_time);

// This test assumes system time will not jump backwards during it
EXPECT_GT(rt_clock.now(last_rt_time), last_rt_time);
// This test assumes system time will not jump backwards during it
EXPECT_GT(rt_clock.now(last_rt_time), last_rt_time);
}
rclcpp::shutdown();
}

0 comments on commit 5e7c300

Please sign in to comment.