def test_sleep_for_ros_time_enabled(default_context): clock = ROSClock() clock._set_ros_time_is_active(True) start_time = Time(seconds=1, clock_type=ClockType.ROS_TIME) sleep_duration = Duration(seconds=10) stop_time = start_time + sleep_duration clock.set_ros_time_override(start_time) retval = None def run(): nonlocal retval retval = clock.sleep_for(sleep_duration) t = threading.Thread(target=run) t.start() # wait for thread to get inside sleep_for call time.sleep(0.2) clock.set_ros_time_override(stop_time) # wait for thread to exit start = clock.now() t.join() stop = clock.now() assert stop - start < A_SMALL_AMOUNT_OF_TIME assert retval
def test_log_throttle_ros_clock(self): message_was_logged = [] ros_clock = ROSClock() time_source = TimeSource() time_source.attach_clock(ros_clock) time_source.ros_time_is_active = True for i in range(5): message_was_logged.append( rclpy.logging._root_logger.log( 'message_' + inspect.stack()[0][3] + '_' + str(i), LoggingSeverity.INFO, throttle_duration_sec=1, throttle_time_source_type=ros_clock, )) time.sleep(0.4) self.assertEqual( message_was_logged, [ False, # t=0, throttled False, # t=0.4, throttled False, # t=0.8, throttled False, # t=1.2, throttled False # t=1.6, throttled ]) message_was_logged = [] for i in range(5): message_was_logged.append( rclpy.logging._root_logger.log( 'message_' + inspect.stack()[0][3] + '_' + str(i), LoggingSeverity.INFO, throttle_duration_sec=2, throttle_time_source_type=ros_clock, )) ros_clock.set_ros_time_override( Time( seconds=i + 1, nanoseconds=0, clock_type=ros_clock.clock_type, )) self.assertEqual( message_was_logged, [ False, # t=0, throttled False, # t=1.0, throttled True, # t=2.0, not throttled False, # t=3.0, throttled True # t=4.0, not throttled ])
def test_triggered_time_jump_callbacks(self): one_second = Duration(seconds=1) half_second = Duration(seconds=0.5) negative_half_second = Duration(seconds=-0.5) negative_one_second = Duration(seconds=-1) threshold1 = JumpThreshold(min_forward=one_second, min_backward=negative_half_second, on_clock_change=False) threshold2 = JumpThreshold(min_forward=half_second, min_backward=negative_one_second, on_clock_change=False) pre_callback1 = Mock() post_callback1 = Mock() pre_callback2 = Mock() post_callback2 = Mock() clock = ROSClock() handler1 = clock.create_jump_callback(threshold1, pre_callback=pre_callback1, post_callback=post_callback1) handler2 = clock.create_jump_callback(threshold2, pre_callback=pre_callback2, post_callback=post_callback2) clock.set_ros_time_override(Time(seconds=1)) clock._set_ros_time_is_active(True) pre_callback1.assert_not_called() post_callback1.assert_not_called() pre_callback2.assert_not_called() post_callback2.assert_not_called() # forward jump clock.set_ros_time_override(Time(seconds=1.75)) pre_callback1.assert_not_called() post_callback1.assert_not_called() pre_callback2.assert_called() post_callback2.assert_called() pre_callback1.reset_mock() post_callback1.reset_mock() pre_callback2.reset_mock() post_callback2.reset_mock() # backwards jump clock.set_ros_time_override(Time(seconds=1)) pre_callback1.assert_called() post_callback1.assert_called() pre_callback2.assert_not_called() post_callback2.assert_not_called() handler1.unregister() handler2.unregister()