Skip to content

Commit

Permalink
Add __enter__ and __exit__ to JumpHandle (#862)
Browse files Browse the repository at this point in the history
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
  • Loading branch information
sloretz authored Dec 10, 2021
1 parent 5c64f2f commit 3af0178
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rclpy/rclpy/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def unregister(self):
self._clock.handle.remove_clock_callback(self)
self._clock = None

def __enter__(self):
return self

def __exit__(self, t, v, tb):
self.unregister()


class Clock:

Expand Down
18 changes: 18 additions & 0 deletions rclpy/test/test_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest
from rclpy.clock import Clock
from rclpy.clock import ClockType
from rclpy.clock import JumpHandle
from rclpy.clock import JumpThreshold
from rclpy.clock import ROSClock
from rclpy.duration import Duration
Expand Down Expand Up @@ -208,3 +209,20 @@ def test_triggered_clock_change_callbacks(self):
handler1.unregister()
handler2.unregister()
handler3.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()

0 comments on commit 3af0178

Please sign in to comment.