From e1a87205a95b99c0e36d7f8aed03d48a607221f1 Mon Sep 17 00:00:00 2001 From: Sai Kishor Kothakota Date: Tue, 7 Nov 2023 13:34:23 +0100 Subject: [PATCH 1/2] initialize the global context to avoid runtime_error upon destruction --- test/realtime_clock_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/realtime_clock_tests.cpp b/test/realtime_clock_tests.cpp index 81faa2d0..fd1eb9a9 100644 --- a/test/realtime_clock_tests.cpp +++ b/test/realtime_clock_tests.cpp @@ -38,6 +38,8 @@ 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); From 4e7e5740ee8b2852c145cc52da1f043c216eb7aa Mon Sep 17 00:00:00 2001 From: Sai Kishor Kothakota Date: Tue, 7 Nov 2023 14:07:33 +0100 Subject: [PATCH 2/2] scope the RealtimeClock to have a valid rclcpp global context upon destruction --- test/realtime_clock_tests.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/test/realtime_clock_tests.cpp b/test/realtime_clock_tests.cpp index fd1eb9a9..43be3370 100644 --- a/test/realtime_clock_tests.cpp +++ b/test/realtime_clock_tests.cpp @@ -44,15 +44,18 @@ TEST(RealtimeClock, get_system_time) 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(); }