@@ -114,6 +114,86 @@ class TestPreInitTimer : public TestTimerFixture
114114 }
115115};
116116
117+ TEST_F (TestTimerFixture, test_timer_init_with_invalid_arguments) {
118+ rcl_clock_t clock;
119+ rcl_allocator_t allocator = rcl_get_default_allocator ();
120+ rcl_ret_t ret = rcl_clock_init (RCL_STEADY_TIME, &clock, &allocator);
121+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
122+ rcl_timer_t timer = rcl_get_zero_initialized_timer ();
123+
124+ ret = rcl_timer_init (
125+ nullptr , &clock, this ->context_ptr , RCL_MS_TO_NS (50 ), nullptr , allocator);
126+ EXPECT_EQ (RCL_RET_INVALID_ARGUMENT, ret);
127+ rcl_reset_error ();
128+
129+ ret = rcl_timer_init (
130+ &timer, nullptr , this ->context_ptr , RCL_MS_TO_NS (50 ), nullptr , allocator);
131+ EXPECT_EQ (RCL_RET_INVALID_ARGUMENT, ret);
132+ rcl_reset_error ();
133+
134+ ret = rcl_timer_init (
135+ &timer, &clock, nullptr , RCL_MS_TO_NS (50 ), nullptr , allocator);
136+ EXPECT_EQ (RCL_RET_INVALID_ARGUMENT, ret);
137+ rcl_reset_error ();
138+
139+ ret = rcl_timer_init (
140+ &timer, &clock, this ->context_ptr , -1 , nullptr , allocator);
141+ EXPECT_EQ (RCL_RET_INVALID_ARGUMENT, ret);
142+ rcl_reset_error ();
143+
144+ rcl_allocator_t invalid_allocator = rcutils_get_zero_initialized_allocator ();
145+ ret = rcl_timer_init (
146+ &timer, &clock, this ->context_ptr , RCL_MS_TO_NS (50 ), nullptr , invalid_allocator);
147+ EXPECT_EQ (RCL_RET_INVALID_ARGUMENT, ret);
148+ rcl_reset_error ();
149+ }
150+
151+ TEST_F (TestTimerFixture, test_timer_with_invalid_clock) {
152+ rcl_clock_t clock;
153+ rcl_allocator_t allocator = rcl_get_default_allocator ();
154+ rcl_ret_t ret = rcl_clock_init (RCL_STEADY_TIME, &clock, &allocator);
155+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
156+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT ({
157+ rcl_ret_t ret = rcl_clock_fini (&clock);
158+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
159+ rcl_reset_error ();
160+ });
161+
162+ rcl_timer_t timer = rcl_get_zero_initialized_timer ();
163+ ret = rcl_timer_init (
164+ &timer, &clock, this ->context_ptr , 0 , nullptr , allocator);
165+ ASSERT_EQ (RCL_RET_OK, ret);
166+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT ({
167+ rcl_ret_t ret = rcl_timer_fini (&timer);
168+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
169+ rcl_reset_error ();
170+ });
171+
172+ rcl_clock_t * timer_clock;
173+ ret = rcl_timer_clock (&timer, &timer_clock);
174+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
175+ timer_clock->get_now = nullptr ;
176+
177+ ret = rcl_timer_call (&timer);
178+ EXPECT_EQ (RCL_RET_ERROR, ret);
179+ rcl_reset_error ();
180+
181+ int64_t time_until_next_call;
182+ ret = rcl_timer_get_time_until_next_call (&timer, &time_until_next_call);
183+ EXPECT_EQ (RCL_RET_ERROR, ret);
184+ rcl_reset_error ();
185+
186+ bool ready;
187+ ret = rcl_timer_is_ready (&timer, &ready);
188+ EXPECT_EQ (RCL_RET_ERROR, ret);
189+ rcl_reset_error ();
190+
191+ rcl_time_point_value_t time_since_last_call;
192+ ret = rcl_timer_get_time_since_last_call (&timer, &time_since_last_call);
193+ EXPECT_EQ (RCL_RET_ERROR, ret);
194+ rcl_reset_error ();
195+ }
196+
117197TEST_F (TestTimerFixture, test_two_timers) {
118198 rcl_ret_t ret;
119199
@@ -182,6 +262,7 @@ TEST_F(TestTimerFixture, test_two_timers_ready_before_timeout) {
182262 rcl_timer_t timer = rcl_get_zero_initialized_timer ();
183263 rcl_timer_t timer2 = rcl_get_zero_initialized_timer ();
184264
265+ // Keep the first timer period low enough so that rcl_wait() doesn't timeout too early.
185266 ret = rcl_timer_init (
186267 &timer, &clock, this ->context_ptr , RCL_MS_TO_NS (10 ), nullptr , rcl_get_default_allocator ());
187268 ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
@@ -698,6 +779,12 @@ TEST_F(TestPreInitTimer, test_timer_call) {
698779 EXPECT_EQ (RCL_RET_OK, rcl_timer_get_time_until_next_call (&timer, &next_call_end));
699780 EXPECT_GT (next_call_start, next_call_end);
700781
782+ EXPECT_EQ (RCL_RET_OK, rcl_enable_ros_time_override (&this ->clock )) << rcl_get_error_string ().str ;
783+ EXPECT_EQ (RCL_RET_OK, rcl_set_ros_time_override (&this ->clock , -1 )) << rcl_get_error_string ().str ;
784+ EXPECT_EQ (RCL_RET_ERROR, rcl_timer_call (&timer));
785+ rcl_reset_error ();
786+ EXPECT_EQ (times_called, 4 );
787+
701788 EXPECT_EQ (RCL_RET_OK, rcl_timer_cancel (&timer)) << rcl_get_error_string ().str ;
702789 EXPECT_EQ (RCL_RET_TIMER_CANCELED, rcl_timer_call (&timer));
703790 rcl_reset_error ();
0 commit comments