def test_start_calls_connect_and_consume_for_each_queue(self): self.one_route_fixture['route'] = ["asgard/counts", "asgard/counts/errors"] consumer = Consumer(self.one_route_fixture, *self.connection_parameters) queue_mock = CoroutineMock(consume=CoroutineMock(), connect=CoroutineMock(), is_connected=False) loop = asyncio.get_event_loop() consumer.queue = queue_mock with asynctest.patch.object(consumer, 'keep_runnig', side_effect=[True, False]) as keep_running_mock: loop.run_until_complete(consumer.start()) self.assertEqual(1, queue_mock.connect.await_count) self.assertEqual(2, queue_mock.consume.await_count) self.assertEqual([mock.call(queue_name="asgard/counts"), mock.call(queue_name="asgard/counts/errors")], queue_mock.consume.await_args_list)
def test_start_reconnects_if_connectaion_failed(self): self.one_route_fixture['route'] = ["asgard/counts", "asgard/counts/errors"] consumer = Consumer(self.one_route_fixture, *self.connection_parameters) with unittest.mock.patch.object(consumer, 'keep_runnig', side_effect=[True, True, False]), \ asynctest.patch.object(asyncio, 'sleep') as sleep_mock: is_connected_mock = mock.PropertyMock(side_effect=[False, False, True]) queue_mock = CoroutineMock(consume=CoroutineMock(), connect=CoroutineMock(side_effect=[AioamqpException, True])) type(queue_mock).is_connected = is_connected_mock loop = asyncio.get_event_loop() consumer.queue = queue_mock loop.run_until_complete(consumer.start()) self.assertEqual(1, queue_mock.connect.await_count) self.assertEqual(2, queue_mock.consume.await_count) self.assertEqual([mock.call(queue_name="asgard/counts"), mock.call(queue_name="asgard/counts/errors")], queue_mock.consume.await_args_list) self.assertEqual(2, sleep_mock.await_count)
def test_start_always_calls_sleep(self): """ Regression: O sleep deve ser chamado *sempre*, e não apenas quando há tentativa de conexão. Aqui, tentamos reconectar apenas uma vez, mas mesmo assim o sleep é chamado 3x, pois o loop principal roda 3x. """ self.one_route_fixture['route'] = ["asgard/counts", "asgard/counts/errors"] consumer = Consumer(self.one_route_fixture, *self.connection_parameters) with unittest.mock.patch.object(consumer, 'keep_runnig', side_effect=[True, True, True, False]), \ asynctest.patch.object(asyncio, 'sleep') as sleep_mock: is_connected_mock = mock.PropertyMock(side_effect=[False, True, True, True]) queue_mock = CoroutineMock(consume=CoroutineMock(), connect=CoroutineMock()) type(queue_mock).is_connected = is_connected_mock loop = asyncio.get_event_loop() consumer.queue = queue_mock loop.run_until_complete(consumer.start()) self.assertEqual(3, sleep_mock.await_count)