async def test_already_done(self): async def wrapped(): pass x = StampedeWrapper(wrapped) x.fut = done_future('foo') assert await x() == 'foo'
async def test_raises_cancel(self): async def wrapped(): raise asyncio.CancelledError() x = StampedeWrapper(wrapped) with pytest.raises(asyncio.CancelledError): await x()
async def test_sequential(self): t = Mock() async def wrapped(): return t() x = StampedeWrapper(wrapped) for _ in range(10): assert await x() is t.return_value assert t.call_count == 10
async def test_concurrent(self): t = Mock() async def wrapped(): await asyncio.sleep(0.5) return t() x = StampedeWrapper(wrapped) async def caller(): return await x() assert all( ret is t.return_value for ret in await asyncio.gather(*[caller() for i in range(10)])) t.assert_called_once_with()
async def _create_topic(self, owner: Service, client: aiokafka.AIOKafkaClient, topic: str, partitions: int, replication: int, **kwargs: Any) -> None: assert topic is not None try: wrap = self._topic_waiters[topic] except KeyError: wrap = self._topic_waiters[topic] = StampedeWrapper( self._really_create_topic, owner, client, topic, partitions, replication, loop=self.loop, **kwargs) try: await wrap() except Exception: self._topic_waiters.pop(topic, None) raise