From fcbe64cff4bea3109531254ceb2955dc4b1bb320 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Thu, 26 Oct 2023 00:08:18 -0700 Subject: [PATCH] address rate related flaky tests. (#2329) Signed-off-by: Tomoya Fujita --- rclcpp/src/rclcpp/context.cpp | 2 +- rclcpp/src/rclcpp/rate.cpp | 3 ++- rclcpp/test/rclcpp/test_rate.cpp | 34 +++++++++++++++++++------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/rclcpp/src/rclcpp/context.cpp b/rclcpp/src/rclcpp/context.cpp index 75c451a861..bbc1d29d0f 100644 --- a/rclcpp/src/rclcpp/context.cpp +++ b/rclcpp/src/rclcpp/context.cpp @@ -496,7 +496,7 @@ Context::sleep_for(const std::chrono::nanoseconds & nanoseconds) std::unique_lock lock(interrupt_mutex_); auto start = std::chrono::steady_clock::now(); // this will release the lock while waiting - interrupt_condition_variable_.wait_for(lock, nanoseconds); + interrupt_condition_variable_.wait_for(lock, time_left); time_left -= std::chrono::steady_clock::now() - start; } } while (time_left > std::chrono::nanoseconds::zero() && this->is_valid()); diff --git a/rclcpp/src/rclcpp/rate.cpp b/rclcpp/src/rclcpp/rate.cpp index cd071d2cb0..7c98d6d4fe 100644 --- a/rclcpp/src/rclcpp/rate.cpp +++ b/rclcpp/src/rclcpp/rate.cpp @@ -67,7 +67,8 @@ Rate::sleep() // Calculate the time to sleep auto time_to_sleep = next_interval - now; // Sleep (will get interrupted by ctrl-c, may not sleep full time) - return clock_->sleep_for(time_to_sleep); + clock_->sleep_for(time_to_sleep); + return true; } bool diff --git a/rclcpp/test/rclcpp/test_rate.cpp b/rclcpp/test/rclcpp/test_rate.cpp index 4f067b2bd8..b80789c853 100644 --- a/rclcpp/test/rclcpp/test_rate.cpp +++ b/rclcpp/test/rclcpp/test_rate.cpp @@ -21,12 +21,24 @@ #include "../utils/rclcpp_gtest_macros.hpp" +class TestRate : public ::testing::Test +{ +public: + void SetUp() + { + rclcpp::init(0, nullptr); + } + + void TearDown() + { + rclcpp::shutdown(); + } +}; + /* Basic tests for the Rate and WallRate classes. */ -TEST(TestRate, rate_basics) { - rclcpp::init(0, nullptr); - +TEST_F(TestRate, rate_basics) { auto period = std::chrono::milliseconds(1000); auto offset = std::chrono::milliseconds(500); auto epsilon = std::chrono::milliseconds(100); @@ -79,13 +91,9 @@ TEST(TestRate, rate_basics) { auto five = std::chrono::system_clock::now(); delta = five - four; ASSERT_TRUE(epsilon > delta); - - rclcpp::shutdown(); } -TEST(TestRate, wall_rate_basics) { - rclcpp::init(0, nullptr); - +TEST_F(TestRate, wall_rate_basics) { auto period = std::chrono::milliseconds(100); auto offset = std::chrono::milliseconds(50); auto epsilon = std::chrono::milliseconds(1); @@ -140,14 +148,12 @@ TEST(TestRate, wall_rate_basics) { auto five = std::chrono::steady_clock::now(); delta = five - four; EXPECT_GT(epsilon, delta); - - rclcpp::shutdown(); } /* Basic test for the deprecated GenericRate class. */ -TEST(TestRate, generic_rate) { +TEST_F(TestRate, generic_rate) { auto period = std::chrono::milliseconds(100); auto offset = std::chrono::milliseconds(50); auto epsilon = std::chrono::milliseconds(1); @@ -262,7 +268,7 @@ TEST(TestRate, generic_rate) { } } -TEST(TestRate, from_double) { +TEST_F(TestRate, from_double) { { rclcpp::Rate rate(1.0); EXPECT_EQ(rclcpp::Duration(std::chrono::seconds(1)), rate.period()); @@ -281,7 +287,7 @@ TEST(TestRate, from_double) { } } -TEST(TestRate, clock_types) { +TEST_F(TestRate, clock_types) { { rclcpp::Rate rate(1.0, std::make_shared(RCL_SYSTEM_TIME)); // suppress deprecated warnings @@ -341,7 +347,7 @@ TEST(TestRate, clock_types) { } } -TEST(TestRate, incorrect_constuctor) { +TEST_F(TestRate, incorrect_constuctor) { // Constructor with 0-frequency RCLCPP_EXPECT_THROW_EQ( rclcpp::Rate rate(0.0),