|
| 1 | +import typing |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from faststream.kafka import KafkaBroker, TestKafkaBroker |
| 5 | + |
| 6 | +from faststream_concurrent_aiokafka.healthcheck import is_kafka_handler_healthy |
| 7 | +from faststream_concurrent_aiokafka.middleware import ( |
| 8 | + initialize_concurrent_processing, |
| 9 | + stop_concurrent_processing, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +async def test_healthy_when_handler_is_running() -> None: |
| 14 | + broker: typing.Final = KafkaBroker("localhost:9092") |
| 15 | + async with TestKafkaBroker(broker) as test_broker: |
| 16 | + await initialize_concurrent_processing(context=test_broker.context) |
| 17 | + try: |
| 18 | + assert is_kafka_handler_healthy(test_broker.context) is True |
| 19 | + finally: |
| 20 | + await stop_concurrent_processing(test_broker.context) |
| 21 | + |
| 22 | + |
| 23 | +async def test_unhealthy_when_no_handler_in_context() -> None: |
| 24 | + broker: typing.Final = KafkaBroker("localhost:9092") |
| 25 | + async with TestKafkaBroker(broker) as test_broker: |
| 26 | + assert is_kafka_handler_healthy(test_broker.context) is False |
| 27 | + |
| 28 | + |
| 29 | +async def test_unhealthy_when_handler_stopped() -> None: |
| 30 | + broker: typing.Final = KafkaBroker("localhost:9092") |
| 31 | + async with TestKafkaBroker(broker) as test_broker: |
| 32 | + await initialize_concurrent_processing(context=test_broker.context) |
| 33 | + await stop_concurrent_processing(test_broker.context) |
| 34 | + assert is_kafka_handler_healthy(test_broker.context) is False |
| 35 | + |
| 36 | + |
| 37 | +async def test_unhealthy_when_is_healthy_returns_false() -> None: |
| 38 | + broker: typing.Final = KafkaBroker("localhost:9092") |
| 39 | + async with TestKafkaBroker(broker) as test_broker: |
| 40 | + mock_handler: typing.Final = MagicMock() |
| 41 | + mock_handler.is_healthy = False |
| 42 | + test_broker.context.set_global("concurrent_processing", mock_handler) |
| 43 | + assert is_kafka_handler_healthy(test_broker.context) is False |
0 commit comments