Example #1
0
File: common.py Project: acfr/mcl
    def test_message_publish(self):
        """Test %s MessageBroadcaster() publish."""

        # Test publish fails if the input is not a Message().
        with self.assertRaises(TypeError):
            MessageBroadcaster(dict)
        with self.assertRaises(TypeError):
            MessageBroadcaster(0)

        # Create an instance of MessageBroadcaster().
        message = self.Message()
        broadcaster = MessageBroadcaster(self.Message)

        # Test publish succeeds with default topic.
        broadcaster.publish(message)

        # Ensure attempts to publish a non-message type raises an exception.
        with self.assertRaises(TypeError):
            broadcaster.publish(False)

        # Test setting topic at publish.
        self.assertEqual(broadcaster.topic, None)
        broadcaster.publish(message, topic='topic')
        with self.assertRaises(TypeError):
            broadcaster(message, topic=5)

        # Ensure attempts to publish on a closed connection raises an
        # exception.
        broadcaster.close()
        with self.assertRaises(IOError):
            broadcaster.publish(message)
Example #2
0
File: common.py Project: acfr/mcl
    def test_message_init(self):
        """Test %s MessageBroadcaster() initialisation."""

        # Ensure non-Message() inputs are caught.
        with self.assertRaises(TypeError):
            MessageBroadcaster(None)

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

        # Create an instance of MessageBroadcaster() with defaults.
        broadcaster = MessageBroadcaster(self.Message)
        self.assertEqual(broadcaster.topic, None)
        self.assertTrue(broadcaster.is_open)
        broadcaster.close()

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

        # Ensure non-string topics are caught.
        with self.assertRaises(TypeError):
            MessageBroadcaster(self.Message, topic=False)
Example #3
0
    def test_message_publish(self):
        """Test %s MessageBroadcaster() publish."""

        # Test publish fails if the input is not a Message().
        with self.assertRaises(TypeError):
            MessageBroadcaster(dict)
        with self.assertRaises(TypeError):
            MessageBroadcaster(0)

        # Create an instance of MessageBroadcaster().
        message = self.Message()
        broadcaster = MessageBroadcaster(self.Message)

        # Test publish succeeds with default topic.
        broadcaster.publish(message)

        # Ensure attempts to publish a non-message type raises an exception.
        with self.assertRaises(TypeError):
            broadcaster.publish(False)

        # Test setting topic at publish.
        self.assertEqual(broadcaster.topic, None)
        broadcaster.publish(message, topic='topic')
        with self.assertRaises(TypeError):
            broadcaster(message, topic=5)

        # Ensure attempts to publish on a closed connection raises an
        # exception.
        broadcaster.close()
        with self.assertRaises(IOError):
            broadcaster.publish(message)
Example #4
0
    def test_message_init(self):
        """Test %s MessageBroadcaster() initialisation."""

        # Ensure non-Message() inputs are caught.
        with self.assertRaises(TypeError):
            MessageBroadcaster(None)

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

        # Create an instance of MessageBroadcaster() with defaults.
        broadcaster = MessageBroadcaster(self.Message)
        self.assertEqual(broadcaster.topic, None)
        self.assertTrue(broadcaster.is_open)
        broadcaster.close()

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

        # Ensure non-string topics are caught.
        with self.assertRaises(TypeError):
            MessageBroadcaster(self.Message, topic=False)
Example #5
0
File: common.py Project: 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)])
Example #6
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)])