|
| 1 | +# Copyright 2020 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for the ComposableNodeContainer Action.""" |
| 16 | + |
| 17 | +import asyncio |
| 18 | + |
| 19 | +from launch import LaunchDescription |
| 20 | +from launch import LaunchService |
| 21 | +from launch.actions import DeclareLaunchArgument |
| 22 | +from launch.actions import GroupAction |
| 23 | +from launch.substitutions import LaunchConfiguration |
| 24 | +from launch_ros.actions import ComposableNodeContainer |
| 25 | +from launch_ros.descriptions import ComposableNode |
| 26 | +from launch_ros.utilities import get_node_name_count |
| 27 | + |
| 28 | +import osrf_pycommon.process_utils |
| 29 | + |
| 30 | +TEST_CONTAINER_NAME = 'test_component_container_node_name' |
| 31 | +TEST_CONTAINER_NAMESPACE = 'test_component_container_namespace' |
| 32 | +TEST_NODE_NAME = 'test_composable_node_name' |
| 33 | +TEST_NODE_NAMESPACE = 'test_composable_node_namespace' |
| 34 | + |
| 35 | + |
| 36 | +def _assert_launch_no_errors(actions, *, timeout_sec=1): |
| 37 | + ld = LaunchDescription(actions) |
| 38 | + ls = LaunchService(debug=True) |
| 39 | + ls.include_launch_description(ld) |
| 40 | + |
| 41 | + loop = osrf_pycommon.process_utils.get_loop() |
| 42 | + launch_task = loop.create_task(ls.run_async()) |
| 43 | + loop.run_until_complete(asyncio.sleep(timeout_sec)) |
| 44 | + if not launch_task.done(): |
| 45 | + loop.create_task(ls.shutdown()) |
| 46 | + loop.run_until_complete(launch_task) |
| 47 | + assert 0 == launch_task.result() |
| 48 | + return ls.context |
| 49 | + |
| 50 | + |
| 51 | +def test_composable_node_container(): |
| 52 | + """Nominal test for launching a ComposableNodeContainer.""" |
| 53 | + actions = [ |
| 54 | + ComposableNodeContainer( |
| 55 | + package='rclcpp_components', |
| 56 | + executable='component_container', |
| 57 | + name=TEST_CONTAINER_NAME, |
| 58 | + namespace=TEST_CONTAINER_NAMESPACE, |
| 59 | + composable_node_descriptions=[ |
| 60 | + ComposableNode( |
| 61 | + package='composition', |
| 62 | + plugin='composition::Listener', |
| 63 | + name=TEST_NODE_NAME, |
| 64 | + namespace=TEST_NODE_NAMESPACE, |
| 65 | + ) |
| 66 | + ], |
| 67 | + ), |
| 68 | + ] |
| 69 | + |
| 70 | + context = _assert_launch_no_errors(actions) |
| 71 | + assert get_node_name_count(context, f'/{TEST_CONTAINER_NAMESPACE}/{TEST_CONTAINER_NAME}') == 1 |
| 72 | + assert get_node_name_count(context, f'/{TEST_NODE_NAMESPACE}/{TEST_NODE_NAME}') == 1 |
| 73 | + |
| 74 | + |
| 75 | +def test_composable_node_container_in_group_with_launch_configuration_in_description(): |
| 76 | + """ |
| 77 | + Test launch configuration is passed to ComposableNode description inside GroupAction. |
| 78 | +
|
| 79 | + This is a regression test for #114. |
| 80 | + """ |
| 81 | + actions = [ |
| 82 | + GroupAction([ |
| 83 | + DeclareLaunchArgument(name='test_arg', default_value='True'), |
| 84 | + ComposableNodeContainer( |
| 85 | + package='rclcpp_components', |
| 86 | + executable='component_container', |
| 87 | + name=TEST_CONTAINER_NAME, |
| 88 | + namespace=TEST_CONTAINER_NAMESPACE, |
| 89 | + composable_node_descriptions=[ |
| 90 | + ComposableNode( |
| 91 | + package='composition', |
| 92 | + plugin='composition::Listener', |
| 93 | + name=TEST_NODE_NAME, |
| 94 | + namespace=TEST_NODE_NAMESPACE, |
| 95 | + parameters=[{'use_sim_time': LaunchConfiguration('test_arg')}], |
| 96 | + ) |
| 97 | + ], |
| 98 | + ), |
| 99 | + ], scoped=True), |
| 100 | + ] |
| 101 | + |
| 102 | + context = _assert_launch_no_errors(actions) |
| 103 | + assert get_node_name_count(context, f'/{TEST_CONTAINER_NAMESPACE}/{TEST_CONTAINER_NAME}') == 1 |
| 104 | + assert get_node_name_count(context, f'/{TEST_NODE_NAMESPACE}/{TEST_NODE_NAME}') == 1 |
0 commit comments