"""Utilities that deal with creating fake rosnodes for testing.

Contains tools to send and recieve data from fake nodes and topics.
"""

import contextlib
import cPickle as pickle
import subprocess
import time
import threading
from StringIO import StringIO

import rosnode


class TimeoutError(Exception):
    """Py3 shim, represents no response from a syscall.
    """
    pass


class NoMessage(Exception):
    """Exception for a lack of message in a Node.
    """
    pass

class MockPublisher(object):
    """Mock of a node object for testing.
    """
    def __init__(self, topic, msg_type, queue_size):
        self.topic = topic
        self.msg_type = msg_type
        pub_data = pickle.dumps((topic, msg_type, queue_size))
        # this might be fragile if not run from the ci_scripts/unittest command
        location = './tests/test_utils/publisher.py'
        self.proc = subprocess.Popen([location, pub_data],
                stdin=subprocess.PIPE)

    def send(self, value):
        """Sends data to be publoshed to the mocked topic.
        """
        value.serialize(self.proc.stdin)
        # Just long enough to prevent out of order
        time.sleep(.2)

    def kill(self):
        """Kills the publisher ros node process.
        """
        self.proc.kill()


@contextlib.contextmanager
def mock_pub(topic, rosmsg_type, queue_size=1):
    """Mocks a node and cleans it up when done.
    """
    pub = MockPublisher(topic, rosmsg_type, queue_size)
    no_ns = topic.split('/')[-1]
    while not any(nn.split('/')[-1].startswith(
        ''.join(['mock_publish_', no_ns])) for nn in rosnode.get_node_names()):
        time.sleep(.1)
    try:
        yield pub
    finally:
        pub.kill()


class MockListener(object):
    """Wrapper around a node used for testing.
    """

    def __init__(self, topic, msg_type, timeout=10):
        self.timeout = timeout
        self.topic = topic
        self.msg_type = msg_type
        self.killed = False

        # this might be fragile if not run from the ci_scripts/unittest command
        location = './tests/test_utils/listener.py'
        self.proc = subprocess.Popen([location,
            pickle.dumps((topic, msg_type))], stdout=subprocess.PIPE)
        self._message = None

    def kill(self):
        """Kills the subscriber ros node process.
        """
        self.proc.kill()
        self.proc.wait()

    @property
    def message(self):
        """Getter for the message property.

        Makes sure you've actually recieved a message before providing one.
        """
        if  not self._message:
            msg = self.msg_type()
            sio = StringIO()
            msg.serialize(sio)
            # This weird business with StringIO is to make sure we read the
            # correct number of bytes. Essentially, a serialized ros message
            # is a fixed length string sent over the channel, so we serialize
            # the default message of a type and then read that number of bytes
            # so that we don't encounter any issues with reading too much or
            # too little data.

            def kill_proc():
                """Kills the proccess and sets a flag to raise an error.
                """
                self.proc.kill()
                self.killed = True

            timer = threading.Timer(self.timeout, kill_proc)
            try:
                timer.start()
                data = self.proc.stdout.read(sio.len)
            finally:
                timer.cancel()

            if self.killed:
                raise TimeoutError('The node did not return any data within'
                ' {} seconds'.format(self.timeout))
            msg.deserialize(data)
            self._message = msg
        return self._message


@contextlib.contextmanager
def check_topic(topic, rosmsg_type, timeout=10):
    """Context manager that monitors a rostopic and gets a message sent to it.
    """
    test_node = MockListener(topic, rosmsg_type, timeout=timeout)
    no_ns = topic.split('/')[-1]
    while not any(nn.split('/')[-1].startswith(
        ''.join(['mock_listen_', no_ns])) for nn in rosnode.get_node_names()):
        time.sleep(.1)


    try:
        yield test_node
    finally:
        test_node.kill()

@@ -3570,49 +3570,53 @@
 or('
-The node did not return any data within'%0A
+No message published on %7B%7D within %7B%7D '%0A      
     
@@ -3631,13 +3631,11 @@
     
-' %7B%7D 
+  '
 seco
@@ -3646,16 +3646,28 @@
 .format(
+self.topic, 
 self.tim

Improve error message on timeout.