示例#1
0
 async def test_with_coroutines(self):
     sp = AsyncSubPub()
     q = await sp.subscribe(r'\d+')
     for i in range(10):
         asyncio.create_task(sp.publish(str(i), i))
     self.assertEqual([(await q.get())[1] for _ in range(10)],
                      list(range(10)))
示例#2
0
 async def test_publish_block_and_timeout(self):
     sp = AsyncSubPub()
     topic = 'Saturn'
     q = await sp.subscribe(topic, queue=asyncio.Queue(1))
     await sp.publish(topic, 'Pollux')
     with self.assertRaises(asyncio.QueueFull):
         await sp.publish(topic, 'Castor', timeout=0.001)
示例#3
0
 async def test_subscribe_simple(self):
     sp = AsyncSubPub()
     q = await sp.subscribe("Aerith/Theme")
     recv = await sp.publish("Aerith/Theme", 81611)
     self.assertTrue(recv)
     match, data = await q.get()
     self.assertTrue(match)
     self.assertEqual(data, 81611)
示例#4
0
 async def test_publish_remove_retained(self):
     sp = AsyncSubPub()
     topic = 'JudgmentDay'
     await sp.publish(topic, 'anything but None', retain=True)
     (await sp.subscribe(topic)).get_nowait()
     await sp.publish(topic, None, retain=True)
     with self.assertRaises(asyncio.QueueEmpty):
         (await sp.subscribe(topic)).get_nowait()
示例#5
0
 async def test_mqtt_style(self):
     sp = AsyncSubPub()
     topic = MqttTopic('Daniel/+/+/+/#')
     q = await sp.subscribe(topic)
     await sp.publish('Daniel/2005/12/18/02:45:00', 'So help me God')
     match, data = await q.get()
     self.assertEqual(match.groups(), ('2005', '12', '18', '02:45:00'))
     self.assertEqual(data, 'So help me God')
     self.assertTrue(await sp.unsubscribe(topic))
示例#6
0
 async def test_publish_retained(self):
     sp = AsyncSubPub()
     topic = 'Sober'
     data_in = 'Nope'
     recv = await sp.publish(topic, data_in, retain=True)
     self.assertFalse(recv)
     q = await sp.subscribe(topic)
     match, data_out = q.get_nowait()
     self.assertEqual(match.re.pattern, topic)
     self.assertIs(data_out, data_in)
示例#7
0
 async def test_unsubscribe_all(self):
     sp = AsyncSubPub()
     self.assertEqual(await sp.unsubscribe_all(), 0)
     await sp.subscribe("Chalmers")
     await sp.subscribe("GU")
     self.assertEqual(await sp.unsubscribe_all(), 0)
     # Keep references
     q1 = await sp.subscribe("Chalmers")
     q2 = await sp.subscribe("GU")
     self.assertEqual(await sp.unsubscribe_all(), 2)
示例#8
0
 async def test_unsubscribe(self):
     sp = AsyncSubPub()
     topic = "Olofsbäcks Gård"
     q = await sp.subscribe(topic)
     await sp.publish(topic, 81611)
     self.assertTrue(q.get_nowait())
     ok = await sp.unsubscribe(topic)
     self.assertTrue(ok)
     with self.assertRaises(asyncio.QueueEmpty):
         q.get_nowait()
示例#9
0
 async def test_subscribe_custom_queue(self):
     sp = AsyncSubPub()
     q = asyncio.PriorityQueue()
     q_out = await sp.subscribe('', queue=q)
     self.assertIs(q_out, q)
     q_out = await sp.subscribe('', queue=q)
     self.assertIs(q_out, q)
     q_out = await sp.subscribe('')
     self.assertIsInstance(q_out, asyncio.Queue)
     q_out = await sp.subscribe('', queue=asyncio.LifoQueue())
     self.assertIsInstance(q_out, asyncio.LifoQueue)
示例#10
0
    async def test_unsubscribe_gc(self):
        sp = AsyncSubPub()

        async def f():
            q = await sp.subscribe('Trädgårdsgatan/23')
            recv = await sp.publish('Trädgårdsgatan/23', 404)
            self.assertTrue(recv)

        await f()
        recv = await sp.publish('Trädgårdsgatan/23', 404)
        # q garbage collected => automatic unsubscribe
        self.assertFalse(recv)
示例#11
0
 async def test_publish_no_one_cares(self):
     sp = AsyncSubPub()
     recv = await sp.publish('Hans Andersson', 'Water')
     self.assertFalse(recv)
示例#12
0
 async def test_unsubscribe_not_subscribed(self):
     sp = AsyncSubPub()
     self.assertFalse(await sp.unsubscribe("Happiness"))
示例#13
0
 async def test_subscribe_twice(self):
     sp = AsyncSubPub()
     q1 = await sp.subscribe('EyesOnMe')
     q2 = await sp.subscribe('EyesOnMe')
     self.assertIsNot(q1, q2)
示例#14
0
 async def test_queue_type(self):
     sp = AsyncSubPub(AsyncExceptionAwareQueue)
     q = await sp.subscribe('MelodiesOfLife')
     self.assertIsInstance(q, AsyncExceptionAwareQueue)
示例#15
0
 def test_custom_queue_factory(self):
     self.assertIs(
         AsyncSubPub(AsyncExceptionAwareQueue).queue_factory,
         AsyncExceptionAwareQueue)
示例#16
0
 def test_default_queue_factory(self):
     self.assertIs(AsyncSubPub().queue_factory, asyncio.Queue)