Exemplo n.º 1
0
    def test_buffer(self):
        """Test ScheduleBroadcasts() multiprocessing target function."""

        # Alias multi-process target function.
        schedule = ScheduleBroadcasts._ScheduleBroadcasts__inject

        # Create inputs to process.
        run_event = multiprocessing.Event()
        queue = multiprocessing.Queue()
        speed = 1.0

        # Ensure process can exist when the run_event() is clear.
        run_event.clear()
        schedule(run_event,
                 queue,
                 speed)

        # Listen for message broadcasts.
        data_buffer = list()
        listener = MessageListener(UnitTestMessageA)
        listener.subscribe(lambda data: data_buffer.append(data))

        # Create data for testing.
        data = {'elapsed_time': 0.0,
                'topic': '',
                'payload': UnitTestMessageA(data=0.0)}

        # Ensure process can exist when the run_event() is clear.
        queue.put(data)
        run_event.set()
        schedule(run_event,
                 queue,
                 speed)
        self.assertEqual(len(data_buffer), 1)
        self.assertDictEqual(data['payload'], data_buffer[0]['payload'])
Exemplo n.º 2
0
    def test_buffer(self):
        """Test ScheduleBroadcasts() multiprocessing target function."""

        # Alias multi-process target function.
        schedule = ScheduleBroadcasts._ScheduleBroadcasts__inject

        # Create inputs to process.
        run_event = multiprocessing.Event()
        queue = multiprocessing.Queue()
        speed = 1.0

        # Ensure process can exist when the run_event() is clear.
        run_event.clear()
        schedule(run_event, queue, speed)

        # Listen for message broadcasts.
        data_buffer = list()
        listener = MessageListener(UnitTestMessageA)
        listener.subscribe(lambda data: data_buffer.append(data))

        # Create data for testing.
        data = {
            'elapsed_time': 0.0,
            'topic': '',
            'payload': UnitTestMessageA(data=0.0)
        }

        # Ensure process can exist when the run_event() is clear.
        queue.put(data)
        run_event.set()
        schedule(run_event, queue, speed)
        self.assertEqual(len(data_buffer), 1)
        self.assertDictEqual(data['payload'], data_buffer[0]['payload'])
Exemplo n.º 3
0
    def schedule(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data):
            buffer_A.append(data)

        def callback_B(data):
            buffer_B.append(data)

        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for reading and scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        if not speed:
            speed = 1.0
            scheduler = ScheduleBroadcasts(data.queue)
        else:
            scheduler = ScheduleBroadcasts(data.queue, speed=speed)
        scheduler.start()

        # Wait for broadcasts to end.
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if scheduler.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.1)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)
Exemplo n.º 4
0
Arquivo: common.py Projeto: acfr/mcl
    def test_message_init(self):
        """Test %s MessageListener() initialisation."""

        # Ensure non-Message() inputs are caught.
        with self.assertRaises(TypeError):
            MessageListener(dict)
        with self.assertRaises(TypeError):
            MessageListener(0)

        # Ensure errors are propagated.
        with self.assertRaises(Exception):
            MessageListener(self.BadMessage)

        # Create an instance of MessageListener() with defaults.
        listener = MessageListener(self.Message)
        self.assertEqual(listener.topics, None)
        self.assertTrue(listener.is_open)
        listener.close()

        # Create an instance of MessageListener() with a specific topic.
        listener = MessageListener(self.Message, topics=TOPIC)
        self.assertEqual(listener.topics, TOPIC)
        self.assertTrue(listener.is_open)
        listener.close()

        # Ensure non-string topics are caught.
        with self.assertRaises(TypeError):
            MessageListener(self.Message, topics=False)
Exemplo n.º 5
0
    def replay(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data):
            buffer_A.append(data)

        def callback_B(data):
            buffer_B.append(data)

        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for replaying data.
        reader = ReadDirectory(LOG_PATH, message=True)
        if not speed:
            speed = 1.0
            replay = Replay(reader)
        else:
            replay = Replay(reader, speed=speed)

        # Start replay and Wait for broadcasts to end.
        replay.start()
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if replay.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.05)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)

        # Ensure replay can be restarted successfully after finishing a replay.
        self.assertTrue(replay.start())
        time.sleep(0.05)
        self.assertTrue(replay.is_alive())
        self.assertTrue(replay.stop())
Exemplo n.º 6
0
    def schedule(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data): buffer_A.append(data)
        def callback_B(data): buffer_B.append(data)
        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for reading and scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        if not speed:
            speed = 1.0
            scheduler = ScheduleBroadcasts(data.queue)
        else:
            scheduler = ScheduleBroadcasts(data.queue, speed=speed)
        scheduler.start()

        # Wait for broadcasts to end.
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if scheduler.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.1)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)
Exemplo n.º 7
0
    def replay(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data): buffer_A.append(data)
        def callback_B(data): buffer_B.append(data)
        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for replaying data.
        reader = ReadDirectory(LOG_PATH, message=True)
        if not speed:
            speed = 1.0
            replay = Replay(reader)
        else:
            replay = Replay(reader, speed=speed)

        # Start replay and Wait for broadcasts to end.
        replay.start()
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if replay.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.05)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)

        # Ensure replay can be restarted successfully after finishing a replay.
        self.assertTrue(replay.start())
        time.sleep(0.05)
        self.assertTrue(replay.is_alive())
        self.assertTrue(replay.stop())
Exemplo n.º 8
0
Arquivo: common.py Projeto: acfr/mcl
    def test_message_send_receive(self):
        """Test %s with MessageBroadcaster/Listener() objects."""

        # NOTE: this test listens for multiple topics from many. Rather than
        #       sending raw data (a previous test), Message() objects are
        #       sent. This tests all the functionality of the
        #       MessageBroadcaster() and MessageListener() objects.

        # Send multiple topics, receive SOME topics.
        send_topics = ['topic A', 'topic B', 'topic C', 'topic D', 'topic E']
        listen_topics = ['topic A', 'topic C', 'topic E']

        # Create broadcaster.
        broadcaster = MessageBroadcaster(self.Message)

        # Catch messages with a specific topic.
        topic_buffer = list()
        listener_topic = MessageListener(self.Message, topics=listen_topics)

        # Subscribe callback.
        def callback(data): topic_buffer.append(data)
        self.assertTrue(listener_topic.subscribe(callback))
        self.assertTrue(listener_topic.is_subscribed(callback))
        self.assertEqual(listener_topic.num_subscriptions(), 1)

        # Catch all messages. This ensures the unit-test does not time out
        # waiting for messages that are filtered out by topic.
        message_buffer = list()
        listener_message = MessageListener(self.Message)

        # Ensure network objects are open.
        self.assertTrue(broadcaster.is_open)
        self.assertTrue(listener_topic.is_open)
        self.assertTrue(listener_message.is_open)

        # Publish messages with different topics.
        messages = list()
        for (i, topic) in enumerate(send_topics):
            messages.append(self.Message())
            messages[-1]['text'] = '%s: %1.8f' % (topic, time.time())

            # Perform test.
            message_buffer = self.publish(broadcaster,
                                          listener_message,
                                          messages[-1],
                                          topic=topic,
                                          received_buffer=message_buffer)

        # Close connections.
        broadcaster.close()
        listener_topic.close()
        listener_message.close()
        self.assertFalse(broadcaster.is_open)
        self.assertFalse(listener_topic.is_open)
        self.assertFalse(listener_message.is_open)

        # Ensure all topics were received.
        self.assertEqual(len(topic_buffer), len(listen_topics))
        for i, topic in enumerate(listen_topics):
            self.assertEqual(topic_buffer[i]['payload'],
                             messages[send_topics.index(topic)])
Exemplo n.º 9
0
    def test_message_send_receive(self):
        """Test %s with MessageBroadcaster/Listener() objects."""

        # NOTE: this test listens for multiple topics from many. Rather than
        #       sending raw data (a previous test), Message() objects are
        #       sent. This tests all the functionality of the
        #       MessageBroadcaster() and MessageListener() objects.

        # Send multiple topics, receive SOME topics.
        send_topics = ['topic A', 'topic B', 'topic C', 'topic D', 'topic E']
        listen_topics = ['topic A', 'topic C', 'topic E']

        # Create broadcaster.
        broadcaster = MessageBroadcaster(self.Message)

        # Catch messages with a specific topic.
        topic_buffer = list()
        listener_topic = MessageListener(self.Message, topics=listen_topics)

        # Subscribe callback.
        def callback(data):
            topic_buffer.append(data)

        self.assertTrue(listener_topic.subscribe(callback))
        self.assertTrue(listener_topic.is_subscribed(callback))
        self.assertEqual(listener_topic.num_subscriptions(), 1)

        # Catch all messages. This ensures the unit-test does not time out
        # waiting for messages that are filtered out by topic.
        message_buffer = list()
        listener_message = MessageListener(self.Message)

        # Ensure network objects are open.
        self.assertTrue(broadcaster.is_open)
        self.assertTrue(listener_topic.is_open)
        self.assertTrue(listener_message.is_open)

        # Publish messages with different topics.
        messages = list()
        for (i, topic) in enumerate(send_topics):
            messages.append(self.Message())
            messages[-1]['text'] = '%s: %1.8f' % (topic, time.time())

            # Perform test.
            message_buffer = self.publish(broadcaster,
                                          listener_message,
                                          messages[-1],
                                          topic=topic,
                                          received_buffer=message_buffer)

        # Close connections.
        broadcaster.close()
        listener_topic.close()
        listener_message.close()
        self.assertFalse(broadcaster.is_open)
        self.assertFalse(listener_topic.is_open)
        self.assertFalse(listener_message.is_open)

        # Ensure all topics were received.
        self.assertEqual(len(topic_buffer), len(listen_topics))
        for i, topic in enumerate(listen_topics):
            self.assertEqual(topic_buffer[i]['payload'],
                             messages[send_topics.index(topic)])
Exemplo n.º 10
0
    def test_message_init(self):
        """Test %s MessageListener() initialisation."""

        # Ensure non-Message() inputs are caught.
        with self.assertRaises(TypeError):
            MessageListener(dict)
        with self.assertRaises(TypeError):
            MessageListener(0)

        # Ensure errors are propagated.
        with self.assertRaises(Exception):
            MessageListener(self.BadMessage)

        # Create an instance of MessageListener() with defaults.
        listener = MessageListener(self.Message)
        self.assertEqual(listener.topics, None)
        self.assertTrue(listener.is_open)
        listener.close()

        # Create an instance of MessageListener() with a specific topic.
        listener = MessageListener(self.Message, topics=TOPIC)
        self.assertEqual(listener.topics, TOPIC)
        self.assertTrue(listener.is_open)
        listener.close()

        # Ensure non-string topics are caught.
        with self.assertRaises(TypeError):
            MessageListener(self.Message, topics=False)