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_time_source_not_using_sim_time(self): time_source = TimeSource(node=self.node) clock = ROSClock() time_source.attach_clock(clock) # When not using sim time, ROS time should look like system time now = clock.now() system_now = Clock(clock_type=ClockType.SYSTEM_TIME).now() assert (system_now.nanoseconds - now.nanoseconds) < 1e9 # Presence of clock publisher should not affect the clock self.publish_clock_messages() self.assertFalse(clock.ros_time_is_active) now = clock.now() system_now = Clock(clock_type=ClockType.SYSTEM_TIME).now() assert (system_now.nanoseconds - now.nanoseconds) < 1e9 # Whether or not an attached clock is using ROS time should be determined by the time # source managing it. self.assertFalse(time_source.ros_time_is_active) clock2 = ROSClock() clock2._set_ros_time_is_active(True) time_source.attach_clock(clock2) self.assertFalse(clock2.ros_time_is_active) assert time_source._clock_sub is None
def test_sleep_for_ros_time_toggled(default_context, ros_time_enabled): clock = ROSClock() clock._set_ros_time_is_active(not ros_time_enabled) retval = None def run(): nonlocal retval retval = clock.sleep_for(Duration(seconds=10)) t = threading.Thread(target=run) t.start() # wait for thread to get inside sleep_for call time.sleep(0.2) clock._set_ros_time_is_active(ros_time_enabled) # wait for thread to exit start = clock.now() t.join() stop = clock.now() assert stop - start < A_SMALL_AMOUNT_OF_TIME assert retval is False
def test_triggered_clock_change_callbacks(self): one_second = Duration(seconds=1) negative_one_second = Duration(seconds=-1) threshold1 = JumpThreshold(min_forward=one_second, min_backward=negative_one_second, on_clock_change=False) threshold2 = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True) threshold3 = JumpThreshold(min_forward=one_second, min_backward=negative_one_second, on_clock_change=True) pre_callback1 = Mock() post_callback1 = Mock() pre_callback2 = Mock() post_callback2 = Mock() pre_callback3 = Mock() post_callback3 = 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) handler3 = clock.create_jump_callback(threshold3, pre_callback=pre_callback3, post_callback=post_callback3) clock._set_ros_time_is_active(True) pre_callback1.assert_not_called() post_callback1.assert_not_called() pre_callback2.assert_called() post_callback2.assert_called() pre_callback3.assert_called() post_callback3.assert_called() pre_callback1.reset_mock() post_callback1.reset_mock() pre_callback2.reset_mock() post_callback2.reset_mock() pre_callback3.reset_mock() post_callback3.reset_mock() 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() pre_callback3.assert_not_called() post_callback3.assert_not_called() handler1.unregister() handler2.unregister() handler3.unregister()
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()
def test_with_jump_handle(): clock = ROSClock() clock._set_ros_time_is_active(False) post_callback = Mock() threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True) with clock.create_jump_callback( threshold, post_callback=post_callback) as jump_handler: assert isinstance(jump_handler, JumpHandle) clock._set_ros_time_is_active(True) post_callback.assert_called_once() post_callback.reset_mock() clock._set_ros_time_is_active(False) post_callback.assert_not_called()
def test_ros_time_is_active(self): clock = ROSClock() clock._set_ros_time_is_active(True) assert clock.ros_time_is_active clock._set_ros_time_is_active(False) assert not clock.ros_time_is_active