def __init__(self):
     Controllable.__init__(self, 'Coordinator')
     self._components = list()
     MessengerFactory._messenger = SimpleMessenger(
     )  # TODO: configure this properly (directly in plumberjack)
     configManager = ConfigManager()
     self._components.append(configManager)
     self._components.append(PluginManager(configManager))
     self._components.append(DeviceManager(configManager))
     self._loopInterval = 1
 def setUp(self):
     self.target = SimpleMessenger()
     self.TOPIC = 'a'
     self.TOPIC2 = 'b'
     self.MESSAGE = 'This is a test'
示例#3
0
 def setUp(self):
     self.target = SimpleMessenger()
     self.TOPIC = 'a'
     self.TOPIC2 = 'b'
     self.MESSAGE = 'This is a test'
class SimpleMessengerTests(unittest.TestCase):
    def setUp(self):
        self.target = SimpleMessenger()
        self.TOPIC = 'a'
        self.TOPIC2 = 'b'
        self.MESSAGE = 'This is a test'

    def testRegisterTopic(self):
        self.target.registerTopic(self.TOPIC)
        self.assertTrue(self.TOPIC in self.target.getTopics())

    def testPublish_unknownTopic(self):
        try:
            self.target.publish(self, self.TOPIC2, self.MESSAGE)
            self.fail('expected exception')
        except MessengerException:
            pass # this is expected
        except Exception:
            self.fail('expected MessengerException')

    def testSubscribe_unknownTopic(self):
        try:
            receiver = mock.Mock()
            self.target.subscribe(receiver, self.TOPIC2)
            self.fail('expected exception')
        except MessengerException:
            pass # this is expected
        except Exception:
            self.fail('expected MessengerException')

    def testPublish_knownTopic(self):
        self.target.registerTopic(self.TOPIC)
        receiver = mock.Mock()
        receiver.messengerCallback = mock.Mock()
        self.target.subscribe(receiver, self.TOPIC)
        self.target.publish(self, self.TOPIC, self.MESSAGE)
        receiver.messengerCallback.assert_called_once_with(self.TOPIC, self.MESSAGE)

    def testPublish_knownTopic_multiRecipients(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver1.messengerCallback = mock.Mock()
        receiver2 = mock.Mock()
        receiver2.messengerCallback = mock.Mock()
        receiver3 = mock.Mock()
        receiver3.messengerCallback = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.subscribe(receiver3, self.TOPIC)
        self.target.publish(self, self.TOPIC, self.MESSAGE)
        receiver1.messengerCallback.assert_called_once_with(self.TOPIC, self.MESSAGE)
        self.assertFalse(receiver2.messengerCallback.called)
        receiver3.messengerCallback.assert_called_once_with(self.TOPIC, self.MESSAGE)

    def testPublish_knownTopic_multiRecipients_senderNotIncluded(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver1.messengerCallback = mock.Mock()
        receiver2 = mock.Mock()
        receiver2.messengerCallback = mock.Mock()
        receiver3 = mock.Mock()
        receiver3.messengerCallback = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.subscribe(receiver3, self.TOPIC)
        self.target.publish(receiver1, self.TOPIC, self.MESSAGE)
        self.assertFalse(receiver1.messengerCallback.called)
        self.assertFalse(receiver2.messengerCallback.called)
        receiver3.messengerCallback.assert_called_once_with(self.TOPIC, self.MESSAGE)

    def testSubscribe(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.assertTrue(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testUnsubscribe(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.unsubscribe(receiver1, self.TOPIC)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testRemoveSubscriber(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.removeSubscriber(receiver1)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertFalse(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testRemoveSubscriber_multi(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver2 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.subscribe(receiver2, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.removeSubscriber(receiver1)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertFalse(self.target.isSubscribed(self.TOPIC2, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC, receiver2))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver2))
示例#5
0
class SimpleMessengerTests(unittest.TestCase):
    def setUp(self):
        self.target = SimpleMessenger()
        self.TOPIC = 'a'
        self.TOPIC2 = 'b'
        self.MESSAGE = 'This is a test'

    def testRegisterTopic(self):
        self.target.registerTopic(self.TOPIC)
        self.assertTrue(self.TOPIC in self.target.getTopics())

    def testPublish_unknownTopic(self):
        try:
            self.target.publish(self, self.TOPIC2, self.MESSAGE)
            self.fail('expected exception')
        except MessengerException:
            pass  # this is expected
        except Exception:
            self.fail('expected MessengerException')

    def testSubscribe_unknownTopic(self):
        try:
            receiver = mock.Mock()
            self.target.subscribe(receiver, self.TOPIC2)
            self.fail('expected exception')
        except MessengerException:
            pass  # this is expected
        except Exception:
            self.fail('expected MessengerException')

    def testPublish_knownTopic(self):
        self.target.registerTopic(self.TOPIC)
        receiver = mock.Mock()
        receiver.messengerCallback = mock.Mock()
        self.target.subscribe(receiver, self.TOPIC)
        self.target.publish(self, self.TOPIC, self.MESSAGE)
        receiver.messengerCallback.assert_called_once_with(
            self.TOPIC, self.MESSAGE)

    def testPublish_knownTopic_multiRecipients(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver1.messengerCallback = mock.Mock()
        receiver2 = mock.Mock()
        receiver2.messengerCallback = mock.Mock()
        receiver3 = mock.Mock()
        receiver3.messengerCallback = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.subscribe(receiver3, self.TOPIC)
        self.target.publish(self, self.TOPIC, self.MESSAGE)
        receiver1.messengerCallback.assert_called_once_with(
            self.TOPIC, self.MESSAGE)
        self.assertFalse(receiver2.messengerCallback.called)
        receiver3.messengerCallback.assert_called_once_with(
            self.TOPIC, self.MESSAGE)

    def testPublish_knownTopic_multiRecipients_senderNotIncluded(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver1.messengerCallback = mock.Mock()
        receiver2 = mock.Mock()
        receiver2.messengerCallback = mock.Mock()
        receiver3 = mock.Mock()
        receiver3.messengerCallback = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.subscribe(receiver3, self.TOPIC)
        self.target.publish(receiver1, self.TOPIC, self.MESSAGE)
        self.assertFalse(receiver1.messengerCallback.called)
        self.assertFalse(receiver2.messengerCallback.called)
        receiver3.messengerCallback.assert_called_once_with(
            self.TOPIC, self.MESSAGE)

    def testSubscribe(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.assertTrue(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testUnsubscribe(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.unsubscribe(receiver1, self.TOPIC)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testRemoveSubscriber(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.removeSubscriber(receiver1)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertFalse(self.target.isSubscribed(self.TOPIC2, receiver1))

    def testRemoveSubscriber_multi(self):
        self.target.registerTopic(self.TOPIC)
        self.target.registerTopic(self.TOPIC2)
        receiver1 = mock.Mock()
        receiver2 = mock.Mock()
        self.target.subscribe(receiver1, self.TOPIC)
        self.target.subscribe(receiver1, self.TOPIC2)
        self.target.subscribe(receiver2, self.TOPIC)
        self.target.subscribe(receiver2, self.TOPIC2)
        self.target.removeSubscriber(receiver1)
        self.assertFalse(self.target.isSubscribed(self.TOPIC, receiver1))
        self.assertFalse(self.target.isSubscribed(self.TOPIC2, receiver1))
        self.assertTrue(self.target.isSubscribed(self.TOPIC, receiver2))
        self.assertTrue(self.target.isSubscribed(self.TOPIC2, receiver2))