Skip to content

Commit

Permalink
Add visibility to rcl_timer_get_allocator (#610)
Browse files Browse the repository at this point in the history
* Add RCL_PUBLIC to be able of using the function externally
* Add fixture to setup timers before testing them
* Add test for rcl_timer_get_allocator function
* Add minor style changes

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>
  • Loading branch information
Blast545 committed Apr 14, 2020
1 parent 3142616 commit b714f41
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rcl/include/rcl/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
47 changes: 47 additions & 0 deletions rcl/test/rcl/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

class TestPreInitTimer : public TestTimerFixture
{
public:
rcl_clock_t clock;
rcl_allocator_t allocator;
rcl_timer_t timer;
rcl_timer_callback_t timer_callback_test = &callback_function;

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;

Expand Down Expand Up @@ -512,3 +552,10 @@ 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 = rcl_timer_get_allocator(&timer);
EXPECT_TRUE(rcutils_allocator_is_valid(allocator_returned));

EXPECT_EQ(NULL, rcl_timer_get_allocator(nullptr));
}

0 comments on commit b714f41

Please sign in to comment.