Skip to content

Commit d78badd

Browse files
committed
Do not use event handler for loading composable nodes (#170)
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 bcb8fac commit d78badd

File tree

3 files changed

+108
-19
lines changed

3 files changed

+108
-19
lines changed

launch_ros/launch_ros/actions/composable_node_container.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from typing import Optional
1919

2020
from launch.action import Action
21-
from launch.actions import RegisterEventHandler
22-
from launch.event_handlers.on_process_start import OnProcessStart
2321
from launch.launch_context import LaunchContext
2422
from launch.some_substitutions_type import SomeSubstitutionsType
2523

@@ -65,16 +63,9 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]:
6563
from .load_composable_nodes import LoadComposableNodes
6664
# Perform load action once the container has started.
6765
load_actions = [
68-
RegisterEventHandler(
69-
event_handler=OnProcessStart(
70-
target_action=self,
71-
on_start=[
72-
LoadComposableNodes(
73-
composable_node_descriptions=self.__composable_node_descriptions,
74-
target_container=self
75-
)
76-
]
77-
)
66+
LoadComposableNodes(
67+
composable_node_descriptions=self.__composable_node_descriptions,
68+
target_container=self
7869
)
7970
]
8071
container_actions = super().execute(context) # type: Optional[List[Action]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
27+
import osrf_pycommon.process_utils
28+
29+
TEST_CONTAINER_NAME = 'test_component_container_node_name'
30+
TEST_CONTAINER_NAMESPACE = 'test_component_container_namespace'
31+
TEST_NODE_NAME = 'test_composable_node_name'
32+
TEST_NODE_NAMESPACE = 'test_composable_node_namespace'
33+
34+
35+
def _assert_launch_no_errors(actions, *, timeout_sec=1):
36+
ld = LaunchDescription(actions)
37+
ls = LaunchService(debug=True)
38+
ls.include_launch_description(ld)
39+
40+
loop = osrf_pycommon.process_utils.get_loop()
41+
launch_task = loop.create_task(ls.run_async())
42+
loop.run_until_complete(asyncio.sleep(timeout_sec))
43+
if not launch_task.done():
44+
loop.create_task(ls.shutdown())
45+
loop.run_until_complete(launch_task)
46+
assert 0 == launch_task.result()
47+
return ls.context
48+
49+
50+
def test_composable_node_container():
51+
"""Nominal test for launching a ComposableNodeContainer."""
52+
actions = [
53+
ComposableNodeContainer(
54+
package='rclcpp_components',
55+
executable='component_container',
56+
name=TEST_CONTAINER_NAME,
57+
namespace=TEST_CONTAINER_NAMESPACE,
58+
composable_node_descriptions=[
59+
ComposableNode(
60+
package='composition',
61+
plugin='composition::Listener',
62+
name=TEST_NODE_NAME,
63+
namespace=TEST_NODE_NAMESPACE,
64+
)
65+
],
66+
),
67+
]
68+
69+
_assert_launch_no_errors(actions)
70+
71+
72+
def test_composable_node_container_in_group_with_launch_configuration_in_description():
73+
"""
74+
Test launch configuration is passed to ComposableNode description inside GroupAction.
75+
76+
This is a regression test for #114.
77+
"""
78+
actions = [
79+
GroupAction([
80+
DeclareLaunchArgument(name='test_arg', default_value='True'),
81+
ComposableNodeContainer(
82+
package='rclcpp_components',
83+
executable='component_container',
84+
name=TEST_CONTAINER_NAME,
85+
namespace=TEST_CONTAINER_NAMESPACE,
86+
composable_node_descriptions=[
87+
ComposableNode(
88+
package='composition',
89+
plugin='composition::Listener',
90+
name=TEST_NODE_NAME,
91+
namespace=TEST_NODE_NAMESPACE,
92+
parameters=[{'use_sim_time': LaunchConfiguration('test_arg')}],
93+
)
94+
],
95+
),
96+
], scoped=True),
97+
]
98+
99+
_assert_launch_no_errors(actions)

test_launch_ros/test/test_launch_ros/actions/test_load_composable_nodes.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from launch import LaunchDescription
2222
from launch import LaunchService
23-
from launch.actions import GroupAction
2423
from launch_ros.actions import LoadComposableNodes
2524
from launch_ros.descriptions import ComposableNode
2625

@@ -101,9 +100,9 @@ def _load_composable_node(
101100
composable_node_descriptions=[
102101
ComposableNode(
103102
package=package,
104-
plugin=plugin,
105-
name=name,
106-
namespace=namespace,
103+
node_plugin=plugin,
104+
node_name=name,
105+
node_namespace=namespace,
107106
parameters=parameters,
108107
remappings=remappings,
109108
)
@@ -119,7 +118,7 @@ def mock_component_container():
119118

120119
def test_load_node(mock_component_container):
121120
"""Test loading a node."""
122-
context = _assert_launch_no_errors([
121+
_assert_launch_no_errors([
123122
_load_composable_node(
124123
package='foo_package',
125124
plugin='bar_plugin',
@@ -142,7 +141,7 @@ def test_load_node(mock_component_container):
142141

143142
def test_load_node_with_remaps(mock_component_container):
144143
"""Test loading a node with remappings."""
145-
context = _assert_launch_no_errors([
144+
_assert_launch_no_errors([
146145
_load_composable_node(
147146
package='foo_package',
148147
plugin='bar_plugin',
@@ -171,7 +170,7 @@ def test_load_node_with_remaps(mock_component_container):
171170

172171
def test_load_node_with_params(mock_component_container):
173172
"""Test loading a node with parameters."""
174-
context = _assert_launch_no_errors([
173+
_assert_launch_no_errors([
175174
_load_composable_node(
176175
package='foo_package',
177176
plugin='bar_plugin',

0 commit comments

Comments
 (0)