Skip to content

Commit dcb2637

Browse files
committed
Do not use event handler for loading composable nodes
Fixes #114. Due to the asynchronous nature of the LoadComposableNodes action, an event handler causes the launch configuration is popped if ComposableNodeContainer appears inside a group or include action. It seems to me we can simply return the load node action, which will get executed after the ComposableNodeContainer action. The use of an event handler is vestigial of a refactoring done in #16, and doesn't appear to be necessary. Signed-off-by: Jacob Perron <[email protected]>
1 parent 8c05ebb commit dcb2637

File tree

2 files changed

+107
-10
lines changed

2 files changed

+107
-10
lines changed

launch_ros/launch_ros/actions/composable_node_container.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,9 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]:
9292
from .load_composable_nodes import LoadComposableNodes
9393
# Perform load action once the container has started.
9494
load_actions = [
95-
RegisterEventHandler(
96-
event_handler=OnProcessStart(
97-
target_action=self,
98-
on_start=[
99-
LoadComposableNodes(
100-
composable_node_descriptions=self.__composable_node_descriptions,
101-
target_container=self
102-
)
103-
]
104-
)
95+
LoadComposableNodes(
96+
composable_node_descriptions=self.__composable_node_descriptions,
97+
target_container=self
10598
)
10699
]
107100
container_actions = super().execute(context) # type: Optional[List[Action]]
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)