Skip to content

Commit 5098bfd

Browse files
authored
Make namespace parameter mandatory in LifecycleNode constructor (#157)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 6c1f36a commit 5098bfd

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

launch_ros/examples/lifecycle_pub_sub_launch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def main(argv=sys.argv[1:]):
3636

3737
# Prepare the talker node.
3838
talker_node = launch_ros.actions.LifecycleNode(
39-
node_name='talker',
39+
name='talker', namespace='',
4040
package='lifecycle', executable='lifecycle_talker', output='screen')
4141

4242
# When the talker reaches the 'inactive' state, make it take the 'activate' transition.
@@ -62,7 +62,7 @@ def main(argv=sys.argv[1:]):
6262
launch.actions.LogInfo(
6363
msg="node 'talker' reached the 'active' state, launching 'listener'."),
6464
launch_ros.actions.LifecycleNode(
65-
node_name='listener',
65+
name='listener', namespace='',
6666
package='lifecycle', executable='lifecycle_listener', output='screen'),
6767
],
6868
)

launch_ros/launch_ros/actions/lifecycle_node.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
from typing import cast
2020
from typing import List
2121
from typing import Optional
22-
from typing import Text
2322
import warnings
2423

2524
import launch
25+
from launch import SomeSubstitutionsType
2626
from launch.action import Action
2727
import launch.logging
2828

@@ -42,8 +42,9 @@ class LifecycleNode(Node):
4242
def __init__(
4343
self,
4444
*,
45-
name: Optional[Text] = None,
46-
node_name: Optional[Text] = None,
45+
name: Optional[SomeSubstitutionsType] = None,
46+
namespace: SomeSubstitutionsType,
47+
node_name: Optional[SomeSubstitutionsType] = None,
4748
**kwargs
4849
) -> None:
4950
"""
@@ -87,7 +88,7 @@ def __init__(
8788
# TODO(jacobperron): Remove default value and this check when deprecated API is removed
8889
if name is None:
8990
raise RuntimeError("'name' must not be None.'")
90-
super().__init__(name=name, **kwargs)
91+
super().__init__(name=name, namespace=namespace, **kwargs)
9192
self.__logger = launch.logging.get_logger(__name__)
9293
self.__rclpy_subscription = None
9394
self.__current_state = \
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 LifecycleNode Action."""
16+
17+
from launch import LaunchContext
18+
from launch_ros.actions import LifecycleNode
19+
20+
import pytest
21+
22+
23+
def test_lifecycle_node_constructor():
24+
# Construction without namespace
25+
with pytest.raises(TypeError):
26+
LifecycleNode(
27+
package='asd',
28+
executable='bsd',
29+
name='my_node',
30+
)
31+
# Construction without name
32+
# TODO(ivanpauno): This should raise TypeError.
33+
with pytest.raises(RuntimeError):
34+
LifecycleNode(
35+
package='asd',
36+
executable='bsd',
37+
namespace='my_ns',
38+
)
39+
# Successfull construction
40+
LifecycleNode(
41+
package='asd',
42+
executable='bsd',
43+
name='my_node',
44+
namespace='my_ns',
45+
)
46+
47+
48+
def test_node_name():
49+
node_object = LifecycleNode(
50+
package='asd',
51+
executable='bsd',
52+
name='my_node',
53+
namespace='my_ns',
54+
)
55+
lc = LaunchContext()
56+
node_object._perform_substitutions(lc)
57+
assert node_object.is_node_name_fully_specified() is True

test_launch_ros/test/test_launch_ros/actions/test_node.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import unittest
2020
import warnings
2121

22+
from launch import LaunchContext
2223
from launch import LaunchDescription
2324
from launch import LaunchService
2425
from launch.actions import Shutdown
2526
from launch.substitutions import EnvironmentVariable
2627
import launch_ros.actions.node
28+
import pytest
2729
import yaml
2830

2931

@@ -44,8 +46,6 @@ def _assert_launch_no_errors(self, actions):
4446
def _create_node(self, *, parameters=None, remappings=None):
4547
return launch_ros.actions.Node(
4648
package='demo_nodes_py', executable='talker_qos', output='screen',
47-
# The node name is required for parameter dicts.
48-
# See https://github.com/ros2/launch/issues/139.
4949
name='my_node', namespace='my_ns',
5050
exec_name='my_node_process',
5151
arguments=['--number_of_cycles', '1'],
@@ -300,3 +300,46 @@ def test_launch_node_with_invalid_parameter_dicts(self):
300300
},
301301
},
302302
}])
303+
304+
305+
def get_test_node_name_parameters():
306+
return [
307+
pytest.param(
308+
launch_ros.actions.Node(
309+
package='asd',
310+
executable='bsd',
311+
name='my_node',
312+
),
313+
False,
314+
id='Node without namespace'
315+
),
316+
pytest.param(
317+
launch_ros.actions.Node(
318+
package='asd',
319+
executable='bsd',
320+
namespace='my_ns',
321+
),
322+
False,
323+
id='Node without name'
324+
),
325+
pytest.param(
326+
launch_ros.actions.Node(
327+
package='asd',
328+
executable='bsd',
329+
name='my_node',
330+
namespace='my_ns',
331+
),
332+
True,
333+
id='Node with fully qualified name'
334+
),
335+
]
336+
337+
338+
@pytest.mark.parametrize(
339+
'node_object, expected_result',
340+
get_test_node_name_parameters()
341+
)
342+
def test_node_name(node_object, expected_result):
343+
lc = LaunchContext()
344+
node_object._perform_substitutions(lc)
345+
assert node_object.is_node_name_fully_specified() is expected_result

0 commit comments

Comments
 (0)