From 8a033c1949595ff3cc01c0609100eb151b45628e Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Tue, 9 Jul 2024 09:43:49 -0400 Subject: [PATCH] Switch to context manager for rclpy tests. (#322) This allows us to use less code, but still properly clean up when we are done with rclpy. Signed-off-by: Chris Lalancette --- .../security/verbs/fixtures/client_service_node.py | 14 +++++--------- .../security/verbs/fixtures/pub_sub_node.py | 14 +++++--------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/sros2/test/sros2/commands/security/verbs/fixtures/client_service_node.py b/sros2/test/sros2/commands/security/verbs/fixtures/client_service_node.py index 520d7ab4..c2e85418 100644 --- a/sros2/test/sros2/commands/security/verbs/fixtures/client_service_node.py +++ b/sros2/test/sros2/commands/security/verbs/fixtures/client_service_node.py @@ -13,6 +13,7 @@ # limitations under the License. import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node import test_msgs.srv @@ -34,17 +35,12 @@ def destroy_node(self): def main(args=None): - rclpy.init(args=args) - - node = ClientServiceNode() - try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(args=args): + node = ClientServiceNode() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): print('node stopped cleanly') - finally: - node.destroy_node() - rclpy.shutdown() if __name__ == '__main__': diff --git a/sros2/test/sros2/commands/security/verbs/fixtures/pub_sub_node.py b/sros2/test/sros2/commands/security/verbs/fixtures/pub_sub_node.py index ac786101..14bc94e7 100644 --- a/sros2/test/sros2/commands/security/verbs/fixtures/pub_sub_node.py +++ b/sros2/test/sros2/commands/security/verbs/fixtures/pub_sub_node.py @@ -13,6 +13,7 @@ # limitations under the License. import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node import test_msgs.msg @@ -37,17 +38,12 @@ def destroy_node(self): def main(args=None): - rclpy.init(args=args) - - node = PubSubNode() - try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(args=args): + node = PubSubNode() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): print('node stopped cleanly') - finally: - node.destroy_node() - rclpy.shutdown() if __name__ == '__main__':