示例#1
0
 async def test_emit_swallow_ex(self):
     sub = SubscriptionConsumer(
         Application(),
         Subscription("foo", lambda record: 1, "group"),
         event_handlers={"event": [AsyncMock(side_effect=Exception)]},
     )
     await sub.emit("event", "foo", "bar")
示例#2
0
 async def test_emit_raises_stop(self):
     sub = SubscriptionConsumer(
         Application(),
         Subscription("foo", lambda record: 1, "group"),
         event_handlers={"event": [AsyncMock(side_effect=StopConsumer)]},
     )
     with pytest.raises(StopConsumer):
         await sub.emit("event", "foo", "bar")
示例#3
0
 async def test_emit(self):
     probe = AsyncMock()
     sub = SubscriptionConsumer(
         Application(),
         Subscription("foo", lambda record: 1, "group"),
         event_handlers={"event": [probe]},
     )
     await sub.emit("event", "foo", "bar")
     probe.assert_called_with("foo", "bar")
示例#4
0
    async def test_finalize_handles_exceptions(self):
        sub = SubscriptionConsumer(
            Application(),
            Subscription("foo", lambda record: 1, "group"),
        )
        consumer = AsyncMock()
        consumer.stop.side_effect = Exception
        consumer.commit.side_effect = Exception
        retry_policy = AsyncMock()
        retry_policy.finalize.side_effect = Exception

        sub._consumer = consumer
        sub._retry_policy = retry_policy
        await sub.finalize()

        consumer.stop.assert_called_once()
        consumer.commit.assert_called_once()
        retry_policy.finalize.assert_called_once()
示例#5
0
 async def test_retries_on_connection_failure(self):
     sub = SubscriptionConsumer(
         Application(),
         Subscription("foo", lambda record: 1, "group"),
     )
     run_mock = AsyncMock()
     sleep = AsyncMock()
     run_mock.side_effect = [aiokafka.errors.KafkaConnectionError, StopConsumer]
     with patch.object(sub, "initialize", AsyncMock()), patch.object(
         sub, "finalize", AsyncMock()
     ), patch.object(sub, "_run", run_mock), patch("kafkaesk.subscription.asyncio.sleep", sleep):
         await sub()
         sleep.assert_called_once()
         assert len(run_mock.mock_calls) == 2
示例#6
0
    async def test_run_exits_when_fut_closed_fut(self):
        sub = SubscriptionConsumer(
            Application(),
            Subscription("foo", lambda record: 1, "group"),
        )
        consumer = AsyncMock()
        consumer.getmany.return_value = {"": [record_factory() for _ in range(10)]}
        sub._consumer = consumer
        sub._running = True

        async def _handle_message(record):
            await asyncio.sleep(0.03)

        with patch.object(sub, "_handle_message", _handle_message):
            task = asyncio.create_task(sub._run())
            await asyncio.sleep(0.01)
            stop_task = asyncio.create_task(sub.stop())
            await asyncio.sleep(0.01)
            sub._close_fut.set_result(None)

            await asyncio.wait([stop_task, task])
示例#7
0
def subscription():
    yield SubscriptionConsumer(
        Application(kafka_servers=["foobar"]), Subscription("foo", lambda record: 1, "group")
    )