Skip to content

Commit

Permalink
Use rclpy.init context manager where we can. (#547)
Browse files Browse the repository at this point in the history
This allows us to cleanup, while using a lot less code
to try and track it.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette authored Jul 9, 2024
1 parent 3da1bb1 commit fe5fe5b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 154 deletions.
20 changes: 11 additions & 9 deletions test_cli/test/initial_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
import sys

import rclpy
from rclpy.executors import ExternalShutdownException


def main(argv=sys.argv):
rclpy.init(args=argv)
try:
with rclpy.init(args=argv):
node = rclpy.create_node(
'initial_params_node',
namespace='/',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True)
rclpy.spin(node)
except (KeyboardInterrupt, ExternalShutdownException):
pass

node = rclpy.create_node(
'initial_params_node',
namespace='/',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True)
rclpy.spin(node)

rclpy.shutdown()
return 0


Expand Down
14 changes: 5 additions & 9 deletions test_cli/test/test_params_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@
@pytest.fixture(scope='module', params=CLIENT_LIBRARY_EXECUTABLES)
def node_fixture(request):
"""Create a fixture with a node and helper executable."""
rclpy.init()
node = rclpy.create_node(
'tests_yaml',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True)
try:
with rclpy.init():
node = rclpy.create_node(
'tests_yaml',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True)
yield {
'node': node,
'executable': request.param,
}
finally:
node.destroy_node()
rclpy.shutdown()


def get_params(node, node_name, param_names, wfs_timeout=5.0):
Expand Down
15 changes: 5 additions & 10 deletions test_cli_remapping/test/name_maker.py
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
import rclpy.node
from test_msgs.msg import Empty as EmptyMsg
from test_msgs.srv import Empty as EmptySrv
Expand All @@ -35,15 +36,9 @@ def __init__(self):


if __name__ == '__main__':
rclpy.init()

node = NameMaker()

try:
rclpy.spin(node)
except KeyboardInterrupt:
with rclpy.init():
node = NameMaker()
rclpy.spin(node)
except (KeyboardInterrupt, ExternalShutdownException):
print('Shutting down name_maker.py')
else:
rclpy.shutdown()
finally:
node.destroy_node()
64 changes: 24 additions & 40 deletions test_communication/test/action_server_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import sys
import time

import rclpy
from rclpy.action import ActionServer
from rclpy.executors import ExternalShutdownException

from test_msgs.action import Fibonacci
Expand All @@ -32,8 +34,6 @@ def execute_goal(self, goal_handle):


def receive_goals(node, action_type, expected_goals):
from rclpy.action import ActionServer

def execute_callback(goal_handle):
for expected_goal in expected_goals:
if expected_goal.is_goal_expected(goal_handle.request):
Expand All @@ -48,8 +48,6 @@ def execute_callback(goal_handle):


def generate_expected_fibonacci_goals():
import rclpy

expected_goals = []

def is_goal_expected(goal):
Expand Down Expand Up @@ -101,8 +99,6 @@ def execute_goal(goal_handle):


def generate_expected_nested_message_goals():
import rclpy

expected_goals = []

def is_goal_expected(goal):
Expand Down Expand Up @@ -151,39 +147,27 @@ def execute_goal(goal_handle):


if __name__ == '__main__':
import rclpy

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('action_type', help='type of the ROS action')
parser.add_argument('namespace', help='namespace of the ROS node')
args = parser.parse_args()

rclpy.init(args=[])
node = rclpy.create_node('action_server', namespace=args.namespace)

if 'Fibonacci' == args.action_type:
action_server = receive_goals(node, Fibonacci, generate_expected_fibonacci_goals())
elif 'NestedMessage' == args.action_type:
action_server = receive_goals(
node,
NestedMessage,
generate_expected_nested_message_goals(),
)
else:
print('Unknown action type {!r}'.format(args.action_type), file=sys.stderr)
node.destroy_node()
rclpy.shutdown()
sys.exit(1)

try:
rclpy.spin(node)
except KeyboardInterrupt:
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('action_type', help='type of the ROS action')
parser.add_argument('namespace', help='namespace of the ROS node')
args = parser.parse_args()

with rclpy.init(args=[]):
node = rclpy.create_node('action_server', namespace=args.namespace)

if 'Fibonacci' == args.action_type:
action_server = receive_goals(node, Fibonacci, generate_expected_fibonacci_goals())
elif 'NestedMessage' == args.action_type:
action_server = receive_goals(
node,
NestedMessage,
generate_expected_nested_message_goals(),
)
else:
print('Unknown action type {!r}'.format(args.action_type), file=sys.stderr)
sys.exit(1)

rclpy.spin(node)
except (KeyboardInterrupt, ExternalShutdownException):
print('Action server stopped cleanly')
except ExternalShutdownException:
sys.exit(1)
except BaseException:
print('Exception in action server:', file=sys.stderr)
raise
finally:
rclpy.try_shutdown()
node.destroy_node()
43 changes: 19 additions & 24 deletions test_communication/test/replier_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import argparse
import functools
import importlib
import sys

import rclpy
from rclpy.executors import ExternalShutdownException

from test_msgs.service_fixtures import get_test_srv


def replier_callback(request, response, srv_fixtures):
Expand All @@ -27,33 +31,27 @@ def replier_callback(request, response, srv_fixtures):


def replier(service_name, number_of_cycles, namespace):
from test_msgs.service_fixtures import get_test_srv
import rclpy

service_pkg = 'test_msgs'
module = importlib.import_module(service_pkg + '.srv')
srv_mod = getattr(module, service_name)

rclpy.init(args=[])

node = rclpy.create_node('replier', namespace=namespace)
with rclpy.init(args=[]):
node = rclpy.create_node('replier', namespace=namespace)

srv_fixtures = get_test_srv(service_name)
srv_fixtures = get_test_srv(service_name)

chatter_callback = functools.partial(
replier_callback, srv_fixtures=srv_fixtures)
chatter_callback = functools.partial(
replier_callback, srv_fixtures=srv_fixtures)

node.create_service(
srv_mod, 'test/service/' + service_name, chatter_callback)
node.create_service(
srv_mod, 'test/service/' + service_name, chatter_callback)

spin_count = 1
print('replier: beginning loop')
while rclpy.ok() and spin_count < number_of_cycles:
rclpy.spin_once(node, timeout_sec=2)
spin_count += 1
print('spin_count: ' + str(spin_count))
node.destroy_node()
rclpy.shutdown()
spin_count = 1
print('replier: beginning loop')
while rclpy.ok() and spin_count < number_of_cycles:
rclpy.spin_once(node, timeout_sec=2)
spin_count += 1
print('spin_count: ' + str(spin_count))


if __name__ == '__main__':
Expand All @@ -69,8 +67,5 @@ def replier(service_name, number_of_cycles, namespace):
number_of_cycles=args.number_of_cycles,
namespace=args.namespace
)
except KeyboardInterrupt:
except (KeyboardInterrupt, ExternalShutdownException):
print('server stopped cleanly')
except BaseException:
print('exception in server:', file=sys.stderr)
raise
56 changes: 24 additions & 32 deletions test_communication/test/requester_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

import argparse
import importlib
import sys

import rclpy
from rclpy.executors import ExternalShutdownException

from test_msgs.service_fixtures import get_test_srv

def requester(service_name, namespace):
import rclpy
from test_msgs.service_fixtures import get_test_srv

def requester(service_name, namespace):
# Import the service
service_pkg = 'test_msgs'
module = importlib.import_module(service_pkg + '.srv')
Expand All @@ -29,31 +30,25 @@ def requester(service_name, namespace):
srv_fixtures = get_test_srv(service_name)
service_name = 'test/service/' + service_name

rclpy.init(args=[])
try:
with rclpy.init(args=[]):
node = rclpy.create_node('requester', namespace=namespace)
try:
# wait for the service to be available
client = node.create_client(srv_mod, service_name)
tries = 15
while rclpy.ok() and not client.wait_for_service(timeout_sec=1.0) and tries > 0:
print('service not available, waiting again...')
tries -= 1
assert tries > 0, 'service still not available, aborting test'

print('requester: beginning request')
# Make one call to that service
for req, resp in srv_fixtures:
future = client.call_async(req)
rclpy.spin_until_future_complete(node, future)
assert repr(future.result()) == repr(resp), \
'unexpected response %r\n\nwas expecting %r' % (future.result(), resp)
print('received reply #%d of %d' % (
srv_fixtures.index([req, resp]) + 1, len(srv_fixtures)))
finally:
node.destroy_node()
finally:
rclpy.shutdown()
# wait for the service to be available
client = node.create_client(srv_mod, service_name)
tries = 15
while rclpy.ok() and not client.wait_for_service(timeout_sec=1.0) and tries > 0:
print('service not available, waiting again...')
tries -= 1
assert tries > 0, 'service still not available, aborting test'

print('requester: beginning request')
# Make one call to that service
for req, resp in srv_fixtures:
future = client.call_async(req)
rclpy.spin_until_future_complete(node, future)
assert repr(future.result()) == repr(resp), \
'unexpected response %r\n\nwas expecting %r' % (future.result(), resp)
print('received reply #%d of %d' % (
srv_fixtures.index([req, resp]) + 1, len(srv_fixtures)))


if __name__ == '__main__':
Expand All @@ -63,8 +58,5 @@ def requester(service_name, namespace):
args = parser.parse_args()
try:
requester(service_name=args.service_name, namespace=args.namespace)
except KeyboardInterrupt:
except (KeyboardInterrupt, ExternalShutdownException):
print('requester stopped cleanly')
except BaseException:
print('exception in requester:', file=sys.stderr)
raise
Loading

0 comments on commit fe5fe5b

Please sign in to comment.