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))
def test_purge_subscribers(self): c = Channel(1) messages = [] onTimeout = lambda: self._onTimeout(messages) self.assertEqual(0, len(c.subscribers)) c.subscribe(None, onTimeout, timeoutSec = 1) self.assertEqual(1, len(c.subscribers)) c.purgeSubscribers() self.assertEqual(1, len(c.subscribers)) self.assertEqual(0, len(messages)) time.sleep(1) print c.subscribers c.purgeSubscribers() self.assertEqual(0, len(c.subscribers)) self.assertEqual("timeout", messages[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))
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))