示例#1
0
    def test_purge_message(self):
        c = Channel(1)

        self.assertEqual(0, len(c.messageQueue))
        c.publish("data")
        self.assertEqual(1, len(c.messageQueue))
        c.purgeMessageQueue()
        self.assertEqual(1, len(c.messageQueue))

        time.sleep(1)
        c.purgeMessageQueue()
        self.assertEqual(0, len(c.messageQueue))
示例#2
0
    def test_subscriber_notification(self):
        messages = []
        Timeout = True
        onReceive = lambda m: self._onReceive(messages, m)
        onTimeout = lambda: self._onTimeout(messages)

        c = Channel(10)

        self.assertEqual(0, len(c.subscribers))
        c.subscribe(onReceive, None, timeoutSec = 1)
        self.assertEqual(1, len(c.subscribers))

        c.publish("data", False)
        self.assertEqual("[(0, 'data')]", str(messages))
        self.assertEqual(0, len(c.subscribers))
示例#3
0
    def test_subscribe_with_id(self):
        messages = []
        onReceive = lambda m: self._onReceive(messages, m)

        c = Channel(10)
        c.publish("data1")
        c.publish("data2")
        c.publish("data3")

        c.subscribe(onReceive, None)
        self.assertEqual(3, len(messages))

        c.subscribe(onReceive, None, minId = 1)
        self.assertEqual(2, len(messages))

        c.subscribe(onReceive, None, minId = 2)
        self.assertEqual(1, len(messages))

        c.subscribe(onReceive, None, minId = 3)
        self.assertEqual(0, len(messages))
示例#4
0
    def test_publish(self):
        messages = []
        onReceive = lambda m: self._onReceive(messages, m)

        c = Channel(10)
        self.assertEqual(0, len(c.messageQueue))
        c.subscribe(onReceive, None)
        self.assertEqual(0, len(messages))

        c.publish("data1")
        c.publish("data2")
        self.assertEqual(1, len(c.messageQueue))
        c.subscribe(onReceive, None)
        self.assertEqual(2, len(messages))

        time.sleep(1)
        c.publish("data3")
        self.assertEqual(2, len(c.messageQueue))
        c.subscribe(onReceive, None)
        self.assertEqual(3, len(messages))
        self.assertEqual("[(0, 'data1'), (1, 'data2'), (2, 'data3')]", str(messages))