-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let the user inject callables after starting subscribers
Signed-off-by: Giorgio Pintaudi <pintaudi@axelspace.com>
- Loading branch information
1 parent
f9ab937
commit fc97542
Showing
4 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,53 @@ | ||
# Copyright 2019 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.count = 0 | ||
self.subscription = self.create_subscription( | ||
String, 'input', self.callback, 10 | ||
) | ||
self.publisher = self.create_publisher(String, 'output', 10) | ||
|
||
def callback(self, input_msg): | ||
self.get_logger().info('I heard: [%s]' % input_msg.data) | ||
output_msg_data = input_msg.data | ||
self.get_logger().info('Publishing: "{0}"'.format(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() |
75 changes: 75 additions & 0 deletions
75
launch_testing_ros/test/examples/wait_for_topic_inject_callback_test.py
This file contains 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,75 @@ | ||
# Copyright 2021 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.actions | ||
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 and remap the topic based on the index provided.""" | ||
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_callback(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 os.name != 'nt': | ||
|
||
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, callback=trigger_callback | ||
) 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 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