Skip to content
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

Add a timeout to node spin up. #27

Merged
merged 3 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions pyrostest/ros_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class NoMessage(Exception):
"""
pass

class NoNode(Exception):
"""A node was expected, but could not be found in time.
"""
pass

def _resolve_location(binary_name):
"""Find the location of a mock node to run.
"""
Expand Down Expand Up @@ -129,15 +134,28 @@ def kill_proc():
self._message = msg
return self._message


def _await_node(topic, prefix, rosmaster_uri):
"""Helper to block until a node is availible in ROS.
"""
def _check_is_availible(topic, prefix, rosmaster_uri, event):
no_ns = topic.split('/')[-1]
while not any(nn.split('/')[-1].startswith(''.join([prefix, '_', no_ns]))
for nn in
pyrostest.rostest_utils.my_get_node_names(uri=rosmaster_uri)):
time.sleep(.1)
event.set()


def _await_node(topic, prefix, rosmaster_uri, timeout):
"""Helper to block until a node is availible in ROS.
"""
is_accessed = threading.Event()
bg = threading.Thread(name='wait for node', target=_check_is_availible,
args=(topic, prefix, rosmaster_uri, is_accessed))
bg.start()
is_accessed.wait(timeout)
if not is_accessed.isSet():
raise NoNode('No node was created for "{}", in namespace "{}"').format(
topic, prefix)




class RosTest(unittest.TestCase):
Expand All @@ -153,24 +171,26 @@ def __init__(self, *args, **kwargs):
self.LAUNCHER = dict()

@contextlib.contextmanager
def check_topic(self, topic, rosmsg_type, timeout=10):
def check_topic(self, topic, rosmsg_type, timeout=10, node_timeout=10):
"""Context manager that monitors a rostopic and gets a message sent to
it.
"""
test_node = MockSubscriber(topic, rosmsg_type, timeout=timeout)
_await_node(topic, 'mock_subscribe', self.rosmaster_uri)
_await_node(topic, 'mock_subscribe', self.rosmaster_uri,
timeout=node_timeout)

try:
yield test_node
finally:
test_node.kill()

@contextlib.contextmanager
def mock_pub(self, topic, rosmsg_type, queue_size=1):
def mock_pub(self, topic, rosmsg_type, queue_size=1, node_timeout=10):
"""Mocks a node and cleans it up when done.
"""
pub = MockPublisher(topic, rosmsg_type, queue_size)
_await_node(topic, 'mock_publish', self.rosmaster_uri)
_await_node(topic, 'mock_publish', self.rosmaster_uri,
timeout=node_timeout)
try:
yield pub
finally:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='pyrostest',
version='0.1.10',
version='0.1.11',
description='The most lit ros testing framework',
long_description=long_description,
packages=['pyrostest'],
Expand Down