async def test_nacker_handles_exception(subscriber_client): mock = MagicMock() async def f(*args, **kwargs): await asyncio.sleep(0) mock(*args, **kwargs) raise RuntimeError subscriber_client.modify_ack_deadline = f queue = asyncio.Queue() nacker_task = asyncio.ensure_future( nacker( 'fake_subscription', queue, subscriber_client, 0.0, MagicMock() ) ) await queue.put('ack_id') await asyncio.sleep(0) await asyncio.sleep(0) mock.assert_called_once() assert queue.qsize() == 0 assert not nacker_task.done() nacker_task.cancel()
async def test_nacker_batches_are_retried_next_time(subscriber_client): mock = MagicMock() async def f(*args, **kwargs): await asyncio.sleep(0) mock(*args, **kwargs) raise TimeoutError subscriber_client.modify_ack_deadline = f queue = asyncio.Queue() nacker_task = asyncio.ensure_future( nacker( 'fake_subscription', queue, subscriber_client, 0.1, MagicMock() ) ) await queue.put('ack_id_1') await queue.put('ack_id_2') await asyncio.sleep(0.3) nacker_task.cancel() assert queue.qsize() == 0 mock.assert_has_calls( [ call('fake_subscription', ack_ids=['ack_id_1', 'ack_id_2'], ack_deadline_seconds=0), call('fake_subscription', ack_ids=['ack_id_1', 'ack_id_2'], ack_deadline_seconds=0), ] )
async def test_nacker_does_modify_ack_deadline(subscriber_client): queue = asyncio.Queue() nacker_task = asyncio.ensure_future( nacker('fake_subscription', queue, subscriber_client, 0.0, MagicMock())) await queue.put('ack_id') await queue.join() subscriber_client.modify_ack_deadline.assert_called_once_with( 'fake_subscription', ack_ids=['ack_id'], ack_deadline_seconds=0) assert queue.qsize() == 0 nacker_task.cancel()
async def test_nacker_batches_not_retried_on_400(caplog, subscriber_client): caplog.set_level(logging.WARNING, logger='gcloud.aio.pubsub.subscriber') mock = MagicMock() async def f(*args, **kwargs): await asyncio.sleep(0) mock(*args, **kwargs) raise aiohttp.client_exceptions.ClientResponseError( None, None, status=400) subscriber_client.modify_ack_deadline = f queue = asyncio.Queue() nacker_task = asyncio.ensure_future( nacker( 'fake_subscription', queue, subscriber_client, 0.1, MagicMock() ) ) await queue.put('ack_id_1') await queue.put('ack_id_2') await asyncio.sleep(0.3) nacker_task.cancel() assert queue.qsize() == 0 mock.assert_has_calls( [ call('fake_subscription', ack_ids=['ack_id_1', 'ack_id_2'], ack_deadline_seconds=0), call('fake_subscription', ack_ids=['ack_id_1'], ack_deadline_seconds=0), call('fake_subscription', ack_ids=['ack_id_2'], ack_deadline_seconds=0), ] ) assert ('gcloud.aio.pubsub.subscriber', logging.WARNING, 'Nack failed for ack_id=ack_id_1') in caplog.record_tuples assert ('gcloud.aio.pubsub.subscriber', logging.WARNING, 'Nack failed for ack_id=ack_id_2') in caplog.record_tuples