Skip to content

Commit

Permalink
Add a more extensive sample code
Browse files Browse the repository at this point in the history
Signed-off-by: Pintaudi Giorgio <[email protected]>
  • Loading branch information
LastStarDust committed Mar 13, 2023
1 parent e9a28bc commit 5aa51d7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
53 changes: 53 additions & 0 deletions launch_testing_ros/test/examples/repeater.py
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()
67 changes: 67 additions & 0 deletions launch_testing_ros/test/examples/wait_for_topic_inject_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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 unittest

import launch
import launch.actions
import launch_ros.actions
import launch_testing.actions
import launch_testing.markers
import pytest
from std_msgs.msg import String

from axel_launch_testing_ros import WaitForTopics


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():
os.system(f'ros2 topic pub --once /input std_msgs/msg/String "data: Hello World"')


@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=2000.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()

0 comments on commit 5aa51d7

Please sign in to comment.