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)))
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)
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)
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()
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))
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)
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)
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()
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)
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)
async def test_publish_no_one_cares(self): sp = AsyncSubPub() recv = await sp.publish('Hans Andersson', 'Water') self.assertFalse(recv)
async def test_unsubscribe_not_subscribed(self): sp = AsyncSubPub() self.assertFalse(await sp.unsubscribe("Happiness"))
async def test_subscribe_twice(self): sp = AsyncSubPub() q1 = await sp.subscribe('EyesOnMe') q2 = await sp.subscribe('EyesOnMe') self.assertIsNot(q1, q2)
async def test_queue_type(self): sp = AsyncSubPub(AsyncExceptionAwareQueue) q = await sp.subscribe('MelodiesOfLife') self.assertIsInstance(q, AsyncExceptionAwareQueue)
def test_custom_queue_factory(self): self.assertIs( AsyncSubPub(AsyncExceptionAwareQueue).queue_factory, AsyncExceptionAwareQueue)
def test_default_queue_factory(self): self.assertIs(AsyncSubPub().queue_factory, asyncio.Queue)