|
| 1 | +# pylint: disable=protected-access |
| 2 | +# pylint: disable=redefined-outer-name |
| 3 | +# pylint: disable=too-many-arguments |
| 4 | +# pylint: disable=unused-argument |
| 5 | +# pylint: disable=unused-variable |
| 6 | + |
| 7 | +from collections.abc import AsyncIterator |
| 8 | + |
| 9 | +import pytest |
| 10 | +import servicelib.fastapi.rabbitmq_lifespan |
| 11 | +from asgi_lifespan import LifespanManager as ASGILifespanManager |
| 12 | +from fastapi import FastAPI |
| 13 | +from fastapi_lifespan_manager import LifespanManager, State |
| 14 | +from pydantic import Field |
| 15 | +from pytest_mock import MockerFixture, MockType |
| 16 | +from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
| 17 | +from pytest_simcore.helpers.typing_env import EnvVarsDict |
| 18 | +from servicelib.fastapi.rabbitmq_lifespan import ( |
| 19 | + RabbitMQConfigurationError, |
| 20 | + RabbitMQLifespanState, |
| 21 | + rabbitmq_connectivity_lifespan, |
| 22 | +) |
| 23 | +from settings_library.application import BaseApplicationSettings |
| 24 | +from settings_library.rabbit import RabbitSettings |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def mock_rabbitmq_connection(mocker: MockerFixture) -> MockType: |
| 29 | + return mocker.patch.object( |
| 30 | + servicelib.fastapi.rabbitmq_lifespan, |
| 31 | + "wait_till_rabbitmq_responsive", |
| 32 | + return_value=mocker.AsyncMock(), |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def app_environment(monkeypatch: pytest.MonkeyPatch) -> EnvVarsDict: |
| 38 | + return setenvs_from_dict( |
| 39 | + monkeypatch, RabbitSettings.model_json_schema()["examples"][0] |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def app_lifespan( |
| 45 | + app_environment: EnvVarsDict, |
| 46 | + mock_rabbitmq_connection: MockType, |
| 47 | +) -> LifespanManager: |
| 48 | + assert app_environment |
| 49 | + |
| 50 | + class AppSettings(BaseApplicationSettings): |
| 51 | + RABBITMQ: RabbitSettings = Field( |
| 52 | + ..., json_schema_extra={"auto_default_from_env": True} |
| 53 | + ) |
| 54 | + |
| 55 | + async def my_app_settings(app: FastAPI) -> AsyncIterator[State]: |
| 56 | + app.state.settings = AppSettings.create_from_envs() |
| 57 | + |
| 58 | + yield RabbitMQLifespanState( |
| 59 | + RABBIT_SETTINGS=app.state.settings.RABBITMQ, |
| 60 | + ).model_dump() |
| 61 | + |
| 62 | + app_lifespan = LifespanManager() |
| 63 | + app_lifespan.add(my_app_settings) |
| 64 | + app_lifespan.add(rabbitmq_connectivity_lifespan) |
| 65 | + |
| 66 | + assert not mock_rabbitmq_connection.called |
| 67 | + |
| 68 | + return app_lifespan |
| 69 | + |
| 70 | + |
| 71 | +async def test_lifespan_rabbitmq_in_an_app( |
| 72 | + is_pdb_enabled: bool, |
| 73 | + app_environment: EnvVarsDict, |
| 74 | + mock_rabbitmq_connection: MockType, |
| 75 | + app_lifespan: LifespanManager, |
| 76 | +): |
| 77 | + app = FastAPI(lifespan=app_lifespan) |
| 78 | + |
| 79 | + async with ASGILifespanManager( |
| 80 | + app, |
| 81 | + startup_timeout=None if is_pdb_enabled else 10, |
| 82 | + shutdown_timeout=None if is_pdb_enabled else 10, |
| 83 | + ) as asgi_manager: |
| 84 | + # Verify that RabbitMQ responsiveness was checked |
| 85 | + mock_rabbitmq_connection.assert_called_once_with( |
| 86 | + app.state.settings.RABBITMQ.dsn |
| 87 | + ) |
| 88 | + |
| 89 | + # Verify that RabbitMQ settings are in the lifespan manager state |
| 90 | + assert app.state.settings.RABBITMQ |
| 91 | + |
| 92 | + # No explicit shutdown logic for RabbitMQ in this case |
| 93 | + |
| 94 | + |
| 95 | +async def test_lifespan_rabbitmq_with_invalid_settings( |
| 96 | + is_pdb_enabled: bool, |
| 97 | +): |
| 98 | + async def my_app_settings(app: FastAPI) -> AsyncIterator[State]: |
| 99 | + yield {"RABBIT_SETTINGS": None} |
| 100 | + |
| 101 | + app_lifespan = LifespanManager() |
| 102 | + app_lifespan.add(my_app_settings) |
| 103 | + app_lifespan.add(rabbitmq_connectivity_lifespan) |
| 104 | + |
| 105 | + app = FastAPI(lifespan=app_lifespan) |
| 106 | + |
| 107 | + with pytest.raises(RabbitMQConfigurationError, match="Invalid RabbitMQ") as excinfo: |
| 108 | + async with ASGILifespanManager( |
| 109 | + app, |
| 110 | + startup_timeout=None if is_pdb_enabled else 10, |
| 111 | + shutdown_timeout=None if is_pdb_enabled else 10, |
| 112 | + ): |
| 113 | + ... |
| 114 | + |
| 115 | + exception = excinfo.value |
| 116 | + assert isinstance(exception, RabbitMQConfigurationError) |
| 117 | + assert exception.validation_error |
| 118 | + assert exception.state["RABBIT_SETTINGS"] is None |
0 commit comments