Skip to content

Commit

Permalink
call ok() to see if rclpy and context is initialized. (#1198)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoya.Fujita <[email protected]>
  • Loading branch information
fujitatomoya authored Nov 13, 2023
1 parent b4b4702 commit 6cd6fd1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#. Process node callbacks
#. Shutdown
Inititalization is done by calling :func:`init` for a particular :class:`.Context`.
Initialization is done by calling :func:`init` for a particular :class:`.Context`.
This must be done before any ROS nodes can be created.
Creating a ROS node is done by calling :func:`create_node` or by instantiating a
Expand Down
4 changes: 4 additions & 0 deletions rclpy/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
def test_on_shutdown_method():
context = Context()
context.init()
assert context.ok()

callback_called = False

Expand All @@ -31,13 +32,15 @@ def on_shutdown(self):
context.on_shutdown(instance.on_shutdown)

context.shutdown()
assert not context.ok()

assert callback_called


def test_on_shutdown_function():
context = Context()
context.init()
assert context.ok()

callback_called = False

Expand All @@ -48,6 +51,7 @@ def on_shutdown():
context.on_shutdown(on_shutdown)

context.shutdown()
assert not context.ok()

assert callback_called

Expand Down
6 changes: 6 additions & 0 deletions rclpy/test/test_init_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
def test_init():
context = rclpy.context.Context()
rclpy.init(context=context)
assert context.ok()
rclpy.shutdown(context=context)


Expand All @@ -47,15 +48,19 @@ def test_init_with_non_utf8_arguments():
def test_init_shutdown_sequence():
context = rclpy.context.Context()
rclpy.init(context=context)
assert context.ok()
rclpy.shutdown(context=context)
context = rclpy.context.Context() # context cannot be reused but should not interfere
rclpy.init(context=context)
assert context.ok()
rclpy.shutdown(context=context)

# global
rclpy.init()
assert rclpy.ok()
rclpy.shutdown()
rclpy.init()
assert rclpy.ok()
rclpy.shutdown()


Expand All @@ -72,6 +77,7 @@ def test_double_init():
def test_double_shutdown():
context = rclpy.context.Context()
rclpy.init(context=context)
assert context.ok()
rclpy.shutdown(context=context)
with pytest.raises(RuntimeError):
rclpy.shutdown(context=context)
Expand Down

0 comments on commit 6cd6fd1

Please sign in to comment.