Skip to content
This repository has been archived by the owner on Jan 27, 2020. It is now read-only.

Commit

Permalink
apex_launchtest_ros add better example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete Baughman committed Mar 19, 2019
1 parent f8457a4 commit e2ae424
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 461 deletions.
2 changes: 2 additions & 0 deletions apex_launchtest_ros/apex_launchtest_ros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# limitations under the License.


from .data_republisher import DataRepublisher
from .message_pump import MessagePump


__all__ = [
'DataRepublisher',
'MessagePump',
]
79 changes: 79 additions & 0 deletions apex_launchtest_ros/apex_launchtest_ros/data_republisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2019 Apex.AI, 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.


class DataRepublisher:
"""Republish mesasges with a transform function applied."""

def __init__(self, node, listen_topic, publish_topic, msg_type, transform_fn):
"""
Create a DataRepublisher.
:param node: A rclpy node that will run the publisher and subscriber
:param listen_topic: The topic to listen for incoming messages on
:param publish_topic: The topic to republish messages on
:param msg_type: The type of ROS msg to receive and republish
:param transfor_fn: A function that takes one mesasge and returns a new message to
republish or None to drop the message
"""
self.__num_received = 0
self.__republished_list = []

self.__node = node
self.__subscriber = node.create_subscription(
msg_type,
listen_topic,
callback=self.__cb
)

self.__publisher = node.create_publisher(
msg_type,
publish_topic
)

self.__transform_fn = transform_fn

def shutdown(self):
"""Stop republishing messages."""
self.__node.destroy_subscription(self.__subscriber)
self.__node.destroy_publisher(self.__publisher)

def get_num_received(self):
"""Get the number of messages received on the listen_topic."""
return self.__num_received

def get_num_republished(self):
"""
Get the number of messages published on publish_topic.
This may be lower than get_num_received if the transform_fn indicated a message
should be dropped
"""
return len(self.__republished_list)

def get_republished(self):
"""Get a list of all of the transformed messages republished."""
return self.__republished_list

def __cb(self, msg):
self.__num_received += 1
repub = self.__transform_fn(msg)

if repub:
self.__republished_list.append(msg)
self.__publisher.publish(repub)
33 changes: 0 additions & 33 deletions apex_launchtest_ros/example_nodes/dying_node

This file was deleted.

42 changes: 0 additions & 42 deletions apex_launchtest_ros/example_nodes/echo_node

This file was deleted.

16 changes: 0 additions & 16 deletions apex_launchtest_ros/example_nodes/empty_node

This file was deleted.

23 changes: 0 additions & 23 deletions apex_launchtest_ros/example_nodes/exception_node

This file was deleted.

62 changes: 0 additions & 62 deletions apex_launchtest_ros/example_nodes/message_counter

This file was deleted.

20 changes: 0 additions & 20 deletions apex_launchtest_ros/example_nodes/terminating_node

This file was deleted.

29 changes: 26 additions & 3 deletions apex_launchtest_ros/examples/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
# Examples

## message_counter.launch.py
## `talker_listener.test.py`

Usage:
> apex_launchtest examples/message_counter.launch.py
> apex_launchtest examples/talker_listener.test.py
Launches a message counter node and runs a few tests to interact with the node
This test launchtes the talker and listener example nodes from demo_nodes_py and interacts
with them via their ROS interfaces. Remaps are used so that one of the tests can sit in
between the talker and the listener and change the data on the fly.

Node that in the setUpClass method, the test makes sure that the listener is subscribed and
republishing messages. Since the listener process provides no synchronization mechanism to
inform the outside world that it's up and running, this step is necessary especially in resource
constrained environments where process startup may take a non negligable amount of time. This
is often the cause of "flakyness" in tests on CI systems. A more robust design of the talker and
listener processes might provide some positive feedback that the node is up and running, but these
are simple example nodes.

#### test_fuzzy_data
This test gives an example of what a test that fuzzes data might look like. A ROS subscriber
and publisher pair encapsulated in a `DataRepublisher` object changes the string "Hello World" to
"Aloha World" as it travles between the talker and the listener

#### test_listener_receives
This test publishes unique messages on the /chatter topic and asserts that the same messages
go to the stdout of the listener node

#### test_talker_transmits
This test subscribes to the remapped /talker_chatter topic and makes sure the talker node also
writes the data it's transmitting to stdout
61 changes: 0 additions & 61 deletions apex_launchtest_ros/examples/dying_node.test.py

This file was deleted.

Loading

0 comments on commit e2ae424

Please sign in to comment.