Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __enter__ and __exit__ to JumpHandle #862

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rclpy/rclpy/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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 @@ -18,6 +18,7 @@

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 @@ -179,3 +180,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()