Skip to content

Commit 453281c

Browse files
authored
Enforce non-null now_fn in TimeControllerClock (#731)
Signed-off-by: Emerson Knapp <[email protected]>
1 parent 9d42372 commit 453281c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

rosbag2_cpp/src/rosbag2_cpp/clocks/time_controller_clock.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ TimeControllerClock::TimeControllerClock(
122122
std::chrono::milliseconds sleep_time_while_paused)
123123
: impl_(std::make_unique<TimeControllerClockImpl>(now_fn, sleep_time_while_paused))
124124
{
125+
if (now_fn == nullptr) {
126+
throw std::invalid_argument("TimeControllerClock now_fn must be non-empty.");
127+
}
125128
std::lock_guard<std::mutex> lock(impl_->state_mutex);
126129
impl_->reference.ros = starting_time;
127130
impl_->reference.steady = impl_->now_fn();

rosbag2_cpp/test/rosbag2_cpp/test_time_controller_clock.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class TimeControllerClockTest : public Test
4343
rcutils_time_point_value_t ros_start_time = 0;
4444
};
4545

46+
TEST_F(TimeControllerClockTest, must_provide_now_fn)
47+
{
48+
NowFunction empty_now;
49+
EXPECT_THROW(
50+
rosbag2_cpp::TimeControllerClock(ros_start_time, 1.0, empty_now),
51+
std::invalid_argument);
52+
}
53+
4654
TEST_F(TimeControllerClockTest, steadytime_precision)
4755
{
4856
const double playback_rate = 1.0;

0 commit comments

Comments
 (0)