-
Notifications
You must be signed in to change notification settings - Fork 91
WaitForTopics: let the user inject a callaback to be executed after starting the subscribers
#356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
christophebedard
merged 11 commits into
ros2:rolling
from
LastStarDust:feature/inject-callables
Apr 15, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
36a895a
Let the user inject callables after starting subscribers
LastStarDust e59c8e8
Fixes docstring for WaitForTopics
LastStarDust f5d59d5
Apply suggestions from code review
LastStarDust d2c1951
Renames private attribute for clarity
LastStarDust 945ff8d
Renames 'callback' to 'trigger' for clarity
LastStarDust 3a208bc
Passes timeout to publisher connection wait
LastStarDust 67b17b5
Adds namespace support to wait_for_topics
LastStarDust c7e9f72
Apply suggestions from code review
LastStarDust 6d53d29
Apply suggestions from code review
LastStarDust aa6fc44
Rename wait_for_topic_inject_callback_test.py to wait_for_topic_injec…
LastStarDust 3c52fdd
Adds a blank line for code style consistency
LastStarDust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Copyright 2025 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import rclpy | ||
| from rclpy.node import Node | ||
|
|
||
| from std_msgs.msg import String | ||
|
|
||
|
|
||
| class Repeater(Node): | ||
|
|
||
| def __init__(self): | ||
| super().__init__('repeater') | ||
| self.subscription = self.create_subscription( | ||
| String, 'input', self.callback, 10 | ||
LastStarDust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| self.publisher = self.create_publisher(String, 'output', 10) | ||
|
|
||
| def callback(self, input_msg): | ||
| self.get_logger().info(f'I heard: [{input_msg.data}]') | ||
| output_msg_data = input_msg.data | ||
| self.get_logger().info(f'Publishing: "{output_msg_data}"') | ||
| self.publisher.publish(String(data=output_msg_data)) | ||
|
|
||
|
|
||
| def main(args=None): | ||
| rclpy.init(args=args) | ||
|
|
||
| node = Repeater() | ||
|
|
||
| try: | ||
| rclpy.spin(node) | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| node.destroy_node() | ||
| rclpy.shutdown() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
78 changes: 78 additions & 0 deletions
78
launch_testing_ros/test/examples/wait_for_topic_inject_trigger_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2025 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import sys | ||
| import time | ||
| import unittest | ||
|
|
||
| import launch | ||
| import launch_ros.actions | ||
| import launch_testing.actions | ||
| import launch_testing.markers | ||
| from launch_testing_ros import WaitForTopics | ||
| import pytest | ||
| from std_msgs.msg import String | ||
|
|
||
|
|
||
| def generate_node(): | ||
| """Return node.""" | ||
| path_to_test = os.path.dirname(__file__) | ||
| return launch_ros.actions.Node( | ||
| executable=sys.executable, | ||
| arguments=[os.path.join(path_to_test, 'repeater.py')], | ||
| name='demo_node', | ||
| additional_env={'PYTHONUNBUFFERED': '1'}, | ||
| ) | ||
|
|
||
|
|
||
| def trigger_function(node): | ||
| if not hasattr(node, 'my_publisher'): | ||
| node.my_publisher = node.create_publisher(String, 'input', 10) | ||
| while node.my_publisher.get_subscription_count() == 0: | ||
| time.sleep(0.1) | ||
| msg = String() | ||
| msg.data = 'Hello World' | ||
| node.my_publisher.publish(msg) | ||
| print('Published message') | ||
|
|
||
|
|
||
| @pytest.mark.launch_test | ||
| @launch_testing.markers.keep_alive | ||
| def generate_test_description(): | ||
| description = [generate_node(), launch_testing.actions.ReadyToTest()] | ||
| return launch.LaunchDescription(description) | ||
|
|
||
|
|
||
| # TODO: Test cases fail on Windows debug builds | ||
| # https://github.com/ros2/launch_ros/issues/292 | ||
| if sys.platform.startswith('win'): | ||
| pytest.skip( | ||
| 'CLI tests can block for a pathological amount of time on Windows.', | ||
| allow_module_level=True) | ||
|
|
||
|
|
||
| class TestFixture(unittest.TestCase): | ||
|
|
||
| def test_topics_successful(self): | ||
| """All the supplied topics should be read successfully.""" | ||
| topic_list = [('output', String)] | ||
| expected_topics = {'output'} | ||
|
|
||
| # Method 1 : Using the magic methods and 'with' keyword | ||
| with WaitForTopics( | ||
| topic_list, timeout=10.0, trigger=trigger_function | ||
| ) as wait_for_node_object_1: | ||
| assert wait_for_node_object_1.topics_received() == expected_topics | ||
| assert wait_for_node_object_1.topics_not_received() == set() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.