async def test_producer_exits_on_exceptions(subscriber_client): mock = MagicMock() async def f(*args, **kwargs): await asyncio.sleep(0) mock(*args, **kwargs) raise RuntimeError subscriber_client.pull = f queue = asyncio.Queue() producer_task = asyncio.ensure_future( producer( 'fake_subscription', queue, subscriber_client, max_messages=1, metrics_client=MagicMock() ) ) await asyncio.sleep(0) await asyncio.sleep(0) await asyncio.sleep(0) await asyncio.sleep(0) await asyncio.sleep(0) mock.assert_called_once() assert queue.qsize() == 0 assert producer_task.done() assert producer_task.exception()
async def test_producer_fetches_messages(subscriber_client): queue = asyncio.Queue() producer_task = asyncio.ensure_future( producer('fake_subscription', queue, subscriber_client, max_messages=1, metrics_client=MagicMock())) message, pulled_at = await asyncio.wait_for(queue.get(), 0.1) producer_task.cancel() assert message assert isinstance(pulled_at, float)
async def test_producer_fetches_once_then_waits_for_consumer( subscriber_client): queue = asyncio.Queue() producer_task = asyncio.ensure_future( producer('fake_subscription', queue, subscriber_client, max_messages=1, metrics_client=MagicMock())) await asyncio.sleep(0) await asyncio.wait_for(queue.get(), 1.0) producer_task.cancel() queue.task_done() await asyncio.sleep(0) assert queue.qsize() == 0
async def test_producer_gracefully_shutsdown(subscriber_client): with patch('time.perf_counter', side_effect=(asyncio.CancelledError, 1)): queue = asyncio.Queue() producer_task = asyncio.ensure_future( producer('fake_subscription', queue, subscriber_client, max_messages=1, metrics_client=MagicMock())) await asyncio.sleep(0) await asyncio.sleep(0) await asyncio.sleep(0) await asyncio.sleep(0) assert queue.qsize() == 1 assert not producer_task.done() await queue.get() queue.task_done() await asyncio.sleep(0) assert producer_task.done()