Skip to content

Commit

Permalink
Switch to using the rclpy.init context manager. (#920)
Browse files Browse the repository at this point in the history
This ensures that we properly cleanup after outselves

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jul 24, 2024
1 parent 6e743c8 commit 8807652
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 64 deletions.
95 changes: 46 additions & 49 deletions ros2cli/test/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,57 +52,54 @@

@pytest.fixture(autouse=True, scope='module')
def local_node():
rclpy.init()
node = rclpy.create_node(
node_name=TEST_NODE_NAME, namespace=TEST_NODE_NAMESPACE
)
publisher = node.create_publisher(
msg_type=test_msgs.msg.Empty,
topic=TEST_TOPIC_NAME,
qos_profile=TEST_TOPIC_PUBLISHER_QOS
)
subscription = node.create_subscription(
msg_type=test_msgs.msg.Empty,
topic=TEST_TOPIC_NAME,
callback=(lambda msg: None),
qos_profile=TEST_TOPIC_SUBSCRIPTION_QOS
)
service = node.create_service(
srv_type=test_msgs.srv.Empty,
srv_name=TEST_SERVICE_NAME,
callback=(lambda req, res: res)
)
client = node.create_client(
srv_type=test_msgs.srv.Empty,
srv_name=TEST_SERVICE_NAME
)

def noop_execute_callback(goal_handle):
goal_handle.succeed()
return test_msgs.action.Fibonacci.Result()
with rclpy.init():
node = rclpy.create_node(
node_name=TEST_NODE_NAME, namespace=TEST_NODE_NAMESPACE
)
publisher = node.create_publisher(
msg_type=test_msgs.msg.Empty,
topic=TEST_TOPIC_NAME,
qos_profile=TEST_TOPIC_PUBLISHER_QOS
)
publisher # to avoid "assigned by never used" warning
subscription = node.create_subscription(
msg_type=test_msgs.msg.Empty,
topic=TEST_TOPIC_NAME,
callback=(lambda msg: None),
qos_profile=TEST_TOPIC_SUBSCRIPTION_QOS
)
subscription # to avoid "assigned by never used" warning
service = node.create_service(
srv_type=test_msgs.srv.Empty,
srv_name=TEST_SERVICE_NAME,
callback=(lambda req, res: res)
)
service # to avoid "assigned by never used" warning
client = node.create_client(
srv_type=test_msgs.srv.Empty,
srv_name=TEST_SERVICE_NAME
)
client # to avoid "assigned by never used" warning

def noop_execute_callback(goal_handle):
goal_handle.succeed()
return test_msgs.action.Fibonacci.Result()

action_server = rclpy.action.ActionServer(
node=node,
action_type=test_msgs.action.Fibonacci,
action_name=TEST_ACTION_NAME,
execute_callback=noop_execute_callback
)
action_server # to avoid "assigned by never used" warning
action_client = rclpy.action.ActionClient(
node=node,
action_type=test_msgs.action.Fibonacci,
action_name=TEST_ACTION_NAME
)
action_client # to avoid "assigned by never used" warning

action_server = rclpy.action.ActionServer(
node=node,
action_type=test_msgs.action.Fibonacci,
action_name=TEST_ACTION_NAME,
execute_callback=noop_execute_callback
)
action_client = rclpy.action.ActionClient(
node=node,
action_type=test_msgs.action.Fibonacci,
action_name=TEST_ACTION_NAME
)
try:
yield node
finally:
action_client.destroy()
action_server.destroy()
node.destroy_client(client)
node.destroy_service(service)
node.destroy_subscription(subscription)
node.destroy_publisher(publisher)
node.destroy_node()
rclpy.shutdown()


@pytest.fixture(scope='module')
Expand Down
24 changes: 9 additions & 15 deletions ros2service/test/fixtures/introspectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,18 @@ def timer_callback(self):


def main(args=None):
rclpy.init(args=args)

service_node = IntrospectableService()
client_node = IntrospectableClient()
try:
with rclpy.init(args=args):
service_node = IntrospectableService()
client_node = IntrospectableClient()

executor = SingleThreadedExecutor()
executor.add_node(service_node)
executor.add_node(client_node)
executor = SingleThreadedExecutor()
executor.add_node(service_node)
executor.add_node(client_node)

try:
executor.spin()
executor.spin()
except (KeyboardInterrupt, ExternalShutdownException):
executor.remove_node(client_node)
executor.remove_node(service_node)
executor.shutdown()
service_node.destroy_node()
client_node.destroy_node()
rclpy.try_shutdown()
pass


if __name__ == '__main__':
Expand Down

0 comments on commit 8807652

Please sign in to comment.