From 4a7f418d9035f69e0a16e8f5a348377e85900516 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Thu, 9 Apr 2020 16:52:03 -0300 Subject: [PATCH 1/4] Add RCL_PUBLIC to be able of using the function externally Signed-off-by: Jorge Perez --- rcl/include/rcl/timer.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rcl/include/rcl/timer.h b/rcl/include/rcl/timer.h index 36a1bfbaa..7c679b552 100644 --- a/rcl/include/rcl/timer.h +++ b/rcl/include/rcl/timer.h @@ -566,6 +566,8 @@ rcl_timer_reset(rcl_timer_t * timer); * \param[inout] timer handle to the timer object * \return pointer to the allocator, or `NULL` if an error occurred */ +RCL_PUBLIC +RCL_WARN_UNUSED const rcl_allocator_t * rcl_timer_get_allocator(const rcl_timer_t * timer); From 2cfaedcabce089d579c2c2fcf1175c436995017b Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Thu, 9 Apr 2020 16:56:22 -0300 Subject: [PATCH 2/4] Add fixture to setup timers before testing them Signed-off-by: Jorge Perez --- rcl/test/rcl/test_timer.cpp | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/rcl/test/rcl/test_timer.cpp b/rcl/test/rcl/test_timer.cpp index 322237f3f..0c3779ab1 100644 --- a/rcl/test/rcl/test_timer.cpp +++ b/rcl/test/rcl/test_timer.cpp @@ -65,6 +65,46 @@ class TestTimerFixture : public ::testing::Test } }; +static uint8_t times_called = 0; +static void callback_function(rcl_timer_t * timer, int64_t last_call) +{ + (void) timer; + (void) last_call; + times_called++; +} +static rcl_timer_callback_t timer_callback_test = &callback_function; + +class TestPreInitTimer : public TestTimerFixture +{ +public: + rcl_clock_t clock; + rcl_allocator_t allocator; + rcl_timer_t timer; + + void SetUp() override + { + TestTimerFixture::SetUp(); + rcl_ret_t ret; + allocator = rcl_get_default_allocator(); + timer = rcl_get_zero_initialized_timer(); + ASSERT_EQ( + RCL_RET_OK, + rcl_clock_init(RCL_ROS_TIME, &clock, &allocator)) << rcl_get_error_string().str; + + ret = rcl_timer_init( + &timer, &clock, this->context_ptr, RCL_S_TO_NS(1), timer_callback_test, + rcl_get_default_allocator()); + ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + } + + void TearDown() override + { + EXPECT_EQ(RCL_RET_OK, rcl_timer_fini(&timer)) << rcl_get_error_string().str; + EXPECT_EQ(RCL_RET_OK, rcl_clock_fini(&clock)) << rcl_get_error_string().str; + TestTimerFixture::TearDown(); + } +}; + TEST_F(TestTimerFixture, test_two_timers) { rcl_ret_t ret; From fb5416e5b22bd9ff628f741c7c4fc6d1c77eaaf4 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Thu, 9 Apr 2020 18:01:04 -0300 Subject: [PATCH 3/4] Add test for rcl_timer_get_allocator function Signed-off-by: Jorge Perez --- rcl/test/rcl/test_timer.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rcl/test/rcl/test_timer.cpp b/rcl/test/rcl/test_timer.cpp index 0c3779ab1..1ab08f108 100644 --- a/rcl/test/rcl/test_timer.cpp +++ b/rcl/test/rcl/test_timer.cpp @@ -552,3 +552,11 @@ TEST_F(TestTimerFixture, test_ros_time_wakes_wait) { EXPECT_TRUE(timer_was_ready); EXPECT_LT(finish - start, std::chrono::milliseconds(100)); } + +TEST_F(TestPreInitTimer, test_timer_get_allocator) { + const rcl_allocator_t * allocator_returned; + allocator_returned = rcl_timer_get_allocator(&timer); + EXPECT_TRUE(rcutils_allocator_is_valid(allocator_returned)); + + EXPECT_EQ(NULL, rcl_timer_get_allocator(nullptr)); +} From 9c5e1020138409f0084e98c5d25ab9d6f8007ef3 Mon Sep 17 00:00:00 2001 From: Jorge Perez Date: Mon, 13 Apr 2020 11:43:01 -0300 Subject: [PATCH 4/4] Add minor style changes Signed-off-by: Jorge Perez --- rcl/test/rcl/test_timer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rcl/test/rcl/test_timer.cpp b/rcl/test/rcl/test_timer.cpp index 1ab08f108..746f575b6 100644 --- a/rcl/test/rcl/test_timer.cpp +++ b/rcl/test/rcl/test_timer.cpp @@ -72,7 +72,6 @@ static void callback_function(rcl_timer_t * timer, int64_t last_call) (void) last_call; times_called++; } -static rcl_timer_callback_t timer_callback_test = &callback_function; class TestPreInitTimer : public TestTimerFixture { @@ -80,6 +79,7 @@ class TestPreInitTimer : public TestTimerFixture rcl_clock_t clock; rcl_allocator_t allocator; rcl_timer_t timer; + rcl_timer_callback_t timer_callback_test = &callback_function; void SetUp() override { @@ -554,8 +554,7 @@ TEST_F(TestTimerFixture, test_ros_time_wakes_wait) { } TEST_F(TestPreInitTimer, test_timer_get_allocator) { - const rcl_allocator_t * allocator_returned; - allocator_returned = rcl_timer_get_allocator(&timer); + const rcl_allocator_t * allocator_returned = rcl_timer_get_allocator(&timer); EXPECT_TRUE(rcutils_allocator_is_valid(allocator_returned)); EXPECT_EQ(NULL, rcl_timer_get_allocator(nullptr));