Skip to content

Commit 1e33f0a

Browse files
authored
WaitForTopics: let the user inject a trigger function to be executed after starting the subscribers (#356)
Signed-off-by: Giorgio Pintaudi <[email protected]>
1 parent eb34cbe commit 1e33f0a

File tree

4 files changed

+194
-5
lines changed

4 files changed

+194
-5
lines changed

launch_testing_ros/launch_testing_ros/wait_for_topics.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from threading import Thread
2020

2121
import rclpy
22+
from rclpy.event_handler import QoSSubscriptionMatchedInfo
23+
from rclpy.event_handler import SubscriptionEventCallbacks
2224
from rclpy.executors import SingleThreadedExecutor
2325
from rclpy.node import Node
2426

@@ -50,12 +52,32 @@ def method_2():
5052
print(wait_for_topics.topics_received()) # Should be {'topic_1', 'topic_2'}
5153
print(wait_for_topics.messages_received('topic_1')) # Should be [message_1, ...]
5254
wait_for_topics.shutdown()
55+
56+
# Method3, calling a trigger function before the wait. The trigger function takes
57+
# the WaitForTopics node object as the first argument. Any additional arguments have
58+
# to be passed to the wait(*args, **kwargs) method directly.
59+
def trigger_function(node, arg=""):
60+
node.get_logger().info('Trigger function called with argument: ' + arg)
61+
62+
def method_3():
63+
topic_list = [('topic_1', String), ('topic_2', String)]
64+
wait_for_topics = WaitForTopics(topic_list, timeout=5.0, trigger=trigger_function)
65+
# The trigger function will be called inside the wait() method after the
66+
# subscribers are created and before the publishers are connected.
67+
assert wait_for_topics.wait("Hello World!")
68+
print('Given topics are receiving messages !')
69+
wait_for_topics.shutdown()
5370
"""
5471

55-
def __init__(self, topic_tuples, timeout=5.0, messages_received_buffer_length=10) -> None:
72+
def __init__(self, topic_tuples, timeout=5.0, messages_received_buffer_length=10,
73+
trigger=None, node_namespace=None) -> None:
5674
self.topic_tuples = topic_tuples
5775
self.timeout = timeout
5876
self.messages_received_buffer_length = messages_received_buffer_length
77+
self.trigger = trigger
78+
self.node_namespace = node_namespace
79+
if self.trigger is not None and not callable(self.trigger):
80+
raise TypeError('The passed trigger is not callable')
5981
self.__ros_context = rclpy.Context()
6082
rclpy.init(context=self.__ros_context)
6183
self.__ros_executor = SingleThreadedExecutor(context=self.__ros_context)
@@ -80,11 +102,15 @@ def _prepare_ros_node(self):
80102
name=node_name,
81103
node_context=self.__ros_context,
82104
messages_received_buffer_length=self.messages_received_buffer_length,
105+
node_namespace=self.node_namespace
83106
)
84107
self.__ros_executor.add_node(self.__ros_node)
85108

86-
def wait(self):
109+
def wait(self, *args, **kwargs):
87110
self.__ros_node.start_subscribers(self.topic_tuples)
111+
if self.trigger:
112+
self.trigger(self.__ros_node, *args, **kwargs)
113+
self.__ros_node.any_publisher_connected.wait(self.timeout)
88114
return self.__ros_node.msg_event_object.wait(self.timeout)
89115

90116
def shutdown(self):
@@ -121,16 +147,26 @@ class _WaitForTopicsNode(Node):
121147
"""Internal node used for subscribing to a set of topics."""
122148

123149
def __init__(
124-
self, name='test_node', node_context=None, messages_received_buffer_length=None
150+
self, name='test_node',
151+
node_context=None,
152+
messages_received_buffer_length=None,
153+
node_namespace=None
125154
) -> None:
126-
super().__init__(node_name=name, context=node_context) # type: ignore
155+
super().__init__(node_name=name, context=node_context, namespace=node_namespace)
127156
self.msg_event_object = Event()
128157
self.messages_received_buffer_length = messages_received_buffer_length
129158
self.subscriber_list = []
130159
self.topic_tuples = []
131160
self.expected_topics = set()
132161
self.received_topics = set()
133162
self.received_messages_buffer = {}
163+
self.any_publisher_connected = Event()
164+
165+
def _sub_matched_event_callback(self, info: QoSSubscriptionMatchedInfo):
166+
if info.current_count != 0:
167+
self.any_publisher_connected.set()
168+
else:
169+
self.any_publisher_connected.clear()
134170

135171
def _reset(self):
136172
self.msg_event_object.clear()
@@ -149,12 +185,16 @@ def start_subscribers(self, topic_tuples):
149185
maxlen=self.messages_received_buffer_length
150186
)
151187
# Create a subscriber
188+
sub_event_callback = SubscriptionEventCallbacks(
189+
matched=self._sub_matched_event_callback
190+
)
152191
self.subscriber_list.append(
153192
self.create_subscription(
154193
topic_type,
155194
topic_name,
156195
self.callback_template(topic_name),
157-
10
196+
10,
197+
event_callbacks=sub_event_callback,
158198
)
159199
)
160200

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2025 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+
import rclpy
16+
from rclpy.node import Node
17+
18+
from std_msgs.msg import String
19+
20+
21+
class Repeater(Node):
22+
23+
def __init__(self):
24+
super().__init__('repeater')
25+
self.subscription = self.create_subscription(
26+
String, 'input', self.callback, 10
27+
)
28+
self.publisher = self.create_publisher(String, 'output', 10)
29+
30+
def callback(self, input_msg):
31+
self.get_logger().info(f'I heard: [{input_msg.data}]')
32+
output_msg_data = input_msg.data
33+
self.get_logger().info(f'Publishing: "{output_msg_data}"')
34+
self.publisher.publish(String(data=output_msg_data))
35+
36+
37+
def main(args=None):
38+
rclpy.init(args=args)
39+
40+
node = Repeater()
41+
42+
try:
43+
rclpy.spin(node)
44+
except KeyboardInterrupt:
45+
pass
46+
finally:
47+
node.destroy_node()
48+
rclpy.shutdown()
49+
50+
51+
if __name__ == '__main__':
52+
main()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright 2025 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+
import os
16+
import sys
17+
import time
18+
import unittest
19+
20+
import launch
21+
import launch_ros.actions
22+
import launch_testing.actions
23+
import launch_testing.markers
24+
from launch_testing_ros import WaitForTopics
25+
import pytest
26+
from std_msgs.msg import String
27+
28+
29+
def generate_node():
30+
"""Return node."""
31+
path_to_test = os.path.dirname(__file__)
32+
return launch_ros.actions.Node(
33+
executable=sys.executable,
34+
arguments=[os.path.join(path_to_test, 'repeater.py')],
35+
name='demo_node',
36+
additional_env={'PYTHONUNBUFFERED': '1'},
37+
)
38+
39+
40+
def trigger_function(node):
41+
if not hasattr(node, 'my_publisher'):
42+
node.my_publisher = node.create_publisher(String, 'input', 10)
43+
while node.my_publisher.get_subscription_count() == 0:
44+
time.sleep(0.1)
45+
msg = String()
46+
msg.data = 'Hello World'
47+
node.my_publisher.publish(msg)
48+
print('Published message')
49+
50+
51+
@pytest.mark.launch_test
52+
@launch_testing.markers.keep_alive
53+
def generate_test_description():
54+
description = [generate_node(), launch_testing.actions.ReadyToTest()]
55+
return launch.LaunchDescription(description)
56+
57+
58+
# TODO: Test cases fail on Windows debug builds
59+
# https://github.com/ros2/launch_ros/issues/292
60+
if sys.platform.startswith('win'):
61+
pytest.skip(
62+
'CLI tests can block for a pathological amount of time on Windows.',
63+
allow_module_level=True)
64+
65+
66+
class TestFixture(unittest.TestCase):
67+
68+
def test_topics_successful(self):
69+
"""All the supplied topics should be read successfully."""
70+
topic_list = [('output', String)]
71+
expected_topics = {'output'}
72+
73+
# Method 1 : Using the magic methods and 'with' keyword
74+
with WaitForTopics(
75+
topic_list, timeout=10.0, trigger=trigger_function
76+
) as wait_for_node_object_1:
77+
assert wait_for_node_object_1.topics_received() == expected_topics
78+
assert wait_for_node_object_1.topics_not_received() == set()

launch_testing_ros/test/examples/wait_for_topic_launch_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,22 @@ def test_topics_unsuccessful(self, count: int):
104104
assert wait_for_node_object.topics_received() == expected_topics
105105
assert wait_for_node_object.topics_not_received() == {'invalid_topic'}
106106
wait_for_node_object.shutdown()
107+
108+
def test_trigger_function(self, count):
109+
topic_list = [('chatter_' + str(i), String) for i in range(count)]
110+
expected_topics = {'chatter_' + str(i) for i in range(count)}
111+
112+
# Method 3 : Using a trigger function
113+
114+
# Using a list to store the trigger function's argument as it is mutable
115+
is_trigger_called = [False]
116+
117+
def trigger_function(node, arg):
118+
node.get_logger().info(f'Trigger function called with argument: {arg[0]}')
119+
arg[0] = True
120+
121+
wait_for_node_object = WaitForTopics(topic_list, timeout=2.0, trigger=trigger_function)
122+
assert wait_for_node_object.wait(is_trigger_called)
123+
assert wait_for_node_object.topics_received() == expected_topics
124+
assert wait_for_node_object.topics_not_received() == set()
125+
assert is_trigger_called[0]

0 commit comments

Comments
 (0)