Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions rclcpp/src/rclcpp/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,11 @@ TimerBase::TimerBase(
rcl_clock_t * clock_handle = clock_->get_clock_handle();
{
std::lock_guard<std::mutex> clock_guard(clock_->get_clock_mutex());
if (
rcl_timer_init(
timer_handle_.get(), clock_handle, rcl_context.get(), period.count(), nullptr,
rcl_get_default_allocator()) != RCL_RET_OK)
{
RCUTILS_LOG_ERROR_NAMED(
"rclcpp",
"Couldn't initialize rcl timer handle: %s\n", rcl_get_error_string().str);
rcl_reset_error();
rcl_ret_t ret = rcl_timer_init(
timer_handle_.get(), clock_handle, rcl_context.get(), period.count(), nullptr,
rcl_get_default_allocator());
if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(ret, "Couldn't initialize rcl timer handle");
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions rclcpp/test/rclcpp/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <chrono>
#include <exception>
#include <memory>
#include <utility>

#include "rcl/timer.h"

Expand Down Expand Up @@ -151,3 +152,43 @@ TEST_F(TestTimer, test_run_cancel_timer)
EXPECT_TRUE(has_timer_run.load());
EXPECT_TRUE(timer->is_canceled());
}

TEST_F(TestTimer, test_bad_arguments) {
auto callback = []() {};
auto node_base = rclcpp::node_interfaces::get_node_base_interface(test_node);
auto context = node_base->get_context();

auto steady_clock = std::make_shared<rclcpp::Clock>(RCL_STEADY_TIME);

// Negative period
EXPECT_THROW(
rclcpp::GenericTimer<decltype(callback)>(steady_clock, -1ms, std::move(callback), context),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner here and elsewhere, no need for std::move:

Suggested change
rclcpp::GenericTimer<decltype(callback)>(steady_clock, -1ms, std::move(callback), context),
rclcpp::GenericTimer<decltype(callback)>(steady_clock, -1ms, callback, context),

Sorry if it seemed like I was suggesting so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An rvalue reference is required to pass callback and I was using the decltype() to be lazy about the template instantiation. But I'm not sure what the expected behavior is of passing std::move on an empty callback multiple times. Switching template type to actual type, and passing in an empty callback.

rclcpp::exceptions::RCLInvalidArgument);

// Very negative period
constexpr auto nanoseconds_min = std::chrono::nanoseconds::min();
EXPECT_THROW(
rclcpp::GenericTimer<decltype(callback)>(
steady_clock, nanoseconds_min, std::move(callback), context),
rclcpp::exceptions::RCLInvalidArgument);

// nanoseconds max, should be ok
constexpr auto nanoseconds_max = std::chrono::nanoseconds::max();
EXPECT_NO_THROW(
rclcpp::GenericTimer<decltype(callback)>(
steady_clock, nanoseconds_max, std::move(callback), context));

// 0 duration period, should be ok
EXPECT_NO_THROW(
rclcpp::GenericTimer<decltype(callback)>(steady_clock, 0ms, std::move(callback), context));

// context is null, which resorts to default
EXPECT_NO_THROW(
rclcpp::GenericTimer<decltype(callback)>(steady_clock, 1ms, std::move(callback), nullptr));

// Clock is unitialized
auto unitialized_clock = std::make_shared<rclcpp::Clock>(RCL_CLOCK_UNINITIALIZED);
EXPECT_THROW(
rclcpp::GenericTimer<decltype(callback)>(unitialized_clock, 1us, std::move(callback), context),
rclcpp::exceptions::RCLError);
}