Skip to content

Commit

Permalink
Switch to context manager for rclpy tests. (#322)
Browse files Browse the repository at this point in the history
This allows us to use less code, but still properly
clean up when we are done with rclpy.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jul 9, 2024
1 parent 0e201cb commit 8a033c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node

import test_msgs.srv
Expand All @@ -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__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node

import test_msgs.msg
Expand All @@ -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__':
Expand Down

0 comments on commit 8a033c1

Please sign in to comment.