示例#1
0
    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()
示例#2
0
    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()
示例#3
0
    def test_clock_change(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        post_cb = Mock()
        threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=post_cb)

        time_source.ros_time_is_active = False
        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_DEACTIVATED

        pre_cb.reset_mock()
        post_cb.reset_mock()

        time_source.ros_time_is_active = True
        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_ACTIVATED
        handler.unregister()
示例#4
0
def test_invalid_jump_threshold():
    with pytest.raises(ValueError, match='.*min_forward.*'):
        JumpThreshold(min_forward=Duration(nanoseconds=0),
                      min_backward=Duration(nanoseconds=-1))

    with pytest.raises(ValueError, match='.*min_forward.*'):
        JumpThreshold(min_forward=Duration(nanoseconds=-1),
                      min_backward=Duration(nanoseconds=-1))

    with pytest.raises(ValueError, match='.*min_backward.*'):
        JumpThreshold(min_forward=Duration(nanoseconds=1),
                      min_backward=Duration(nanoseconds=0))

    with pytest.raises(ValueError, match='.*min_backward.*'):
        JumpThreshold(min_forward=Duration(nanoseconds=1),
                      min_backward=Duration(nanoseconds=1))

    with pytest.raises(ValueError, match='.*must be enabled.*'):
        JumpThreshold(min_forward=None,
                      min_backward=None,
                      on_clock_change=False)
示例#5
0
    def test_no_post_callback(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=None)

        time_source.ros_time_is_active = False
        pre_cb.assert_called_once()
        handler.unregister()
示例#6
0
    def test_no_pre_callback(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        assert self.set_use_sim_time_parameter(True)

        post_cb = Mock()
        threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True)
        handler = clock.create_jump_callback(
            threshold, pre_callback=None, post_callback=post_cb)

        assert self.set_use_sim_time_parameter(False)
        post_cb.assert_called_once()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_DEACTIVATED
        handler.unregister()
示例#7
0
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()
示例#8
0
    def test_backwards_jump(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        post_cb = Mock()
        threshold = JumpThreshold(
            min_forward=None, min_backward=Duration(seconds=-0.5), on_clock_change=False)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=post_cb)

        self.publish_reversed_clock_messages()

        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_NO_CHANGE
        handler.unregister()