|
| 1 | +from collections.abc import AsyncIterator, Awaitable, Callable |
| 2 | +from functools import partial |
| 3 | +from typing import Final |
| 4 | + |
| 5 | +import pytest |
| 6 | +from asgi_lifespan import LifespanManager |
| 7 | +from celery import Celery |
| 8 | +from celery.contrib.testing.worker import TestWorkController, start_worker |
| 9 | +from celery.signals import worker_init, worker_shutdown |
| 10 | +from celery.worker.worker import WorkController |
| 11 | +from celery_library import setup_celery_client |
| 12 | +from celery_library.routes.rpc import router as async_jobs_router |
| 13 | +from celery_library.signals import on_worker_init, on_worker_shutdown |
| 14 | +from celery_library.utils import get_celery_worker |
| 15 | +from celery_library.worker import CeleryTaskWorker |
| 16 | +from faker import Faker |
| 17 | +from fastapi import FastAPI |
| 18 | +from models_library.products import ProductName |
| 19 | +from models_library.rabbitmq_basic_types import RPCNamespace |
| 20 | +from models_library.users import UserID |
| 21 | +from pydantic import TypeAdapter |
| 22 | +from servicelib.rabbitmq import RabbitMQRPCClient |
| 23 | +from settings_library.celery import CelerySettings |
| 24 | +from settings_library.rabbit import RabbitSettings |
| 25 | +from settings_library.redis import RedisSettings |
| 26 | + |
| 27 | +pytest_simcore_core_services_selection = [ |
| 28 | + "rabbit", |
| 29 | + "redis", |
| 30 | + "postgres", |
| 31 | +] |
| 32 | + |
| 33 | +pytest_plugins = [ |
| 34 | + "pytest_simcore.docker_compose", |
| 35 | + "pytest_simcore.docker_swarm", |
| 36 | + "pytest_simcore.rabbit_service", |
| 37 | + "pytest_simcore.redis_service", |
| 38 | + "pytest_simcore.repository_paths", |
| 39 | +] |
| 40 | + |
| 41 | +_LIFESPAN_TIMEOUT: Final[int] = 10 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def rpc_namespace() -> RPCNamespace: |
| 46 | + return TypeAdapter(RPCNamespace).validate_python("test") |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def celery_settings( |
| 51 | + rabbit_service: RabbitSettings, |
| 52 | + redis_service: RedisSettings, |
| 53 | +) -> CelerySettings: |
| 54 | + return CelerySettings.create_from_envs() |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +async def initialized_fast_api( |
| 59 | + rabbitmq_rpc_client: Callable[[str], Awaitable[RabbitMQRPCClient]], |
| 60 | + celery_settings: CelerySettings, |
| 61 | + rpc_namespace: RPCNamespace, |
| 62 | +) -> AsyncIterator[FastAPI]: |
| 63 | + app = FastAPI( |
| 64 | + title="master_fastapi_app", |
| 65 | + description="Service that manages osparc storage backend", |
| 66 | + version="0.0.0", |
| 67 | + ) |
| 68 | + |
| 69 | + setup_celery_client(app, celery_settings=celery_settings) |
| 70 | + rpc_client = await rabbitmq_rpc_client("celery_test_client") |
| 71 | + app.state.rabbitmq_rpc_client = rpc_client |
| 72 | + |
| 73 | + async def startup() -> None: |
| 74 | + rpc_server = app.state.rabbitmq_rpc_client |
| 75 | + assert isinstance(rpc_server, RabbitMQRPCClient) |
| 76 | + await rpc_server.register_router(async_jobs_router, rpc_namespace, app) |
| 77 | + |
| 78 | + app.add_event_handler("startup", startup) |
| 79 | + |
| 80 | + async with LifespanManager( |
| 81 | + app, startup_timeout=_LIFESPAN_TIMEOUT, shutdown_timeout=_LIFESPAN_TIMEOUT |
| 82 | + ): |
| 83 | + yield app |
| 84 | + |
| 85 | + |
| 86 | +@pytest.fixture |
| 87 | +def register_celery_tasks() -> Callable[[Celery], None]: |
| 88 | + """override if tasks are needed""" |
| 89 | + |
| 90 | + def _(celery_app: Celery) -> None: ... |
| 91 | + |
| 92 | + return _ |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture |
| 96 | +async def celery_worker_controller( |
| 97 | + celery_settings: CelerySettings, |
| 98 | + celery_app: Celery, |
| 99 | + register_celery_tasks: Callable[[Celery], None], |
| 100 | +) -> AsyncIterator[TestWorkController]: |
| 101 | + |
| 102 | + def _create_app() -> FastAPI: |
| 103 | + |
| 104 | + return FastAPI( |
| 105 | + title="worker_fastapi_app", |
| 106 | + description="Test application for celery_library", |
| 107 | + version="0.0.0", |
| 108 | + ) |
| 109 | + |
| 110 | + def _on_worker_init_wrapper(sender: WorkController, **_kwargs) -> None: |
| 111 | + return partial(on_worker_init, _create_app, celery_settings)(sender, **_kwargs) |
| 112 | + |
| 113 | + worker_init.connect(_on_worker_init_wrapper) |
| 114 | + worker_shutdown.connect(on_worker_shutdown) |
| 115 | + |
| 116 | + register_celery_tasks(celery_app) |
| 117 | + |
| 118 | + with start_worker( |
| 119 | + celery_app, |
| 120 | + pool="threads", |
| 121 | + concurrency=1, |
| 122 | + loglevel="info", |
| 123 | + perform_ping_check=False, |
| 124 | + queues="default,cpu_bound", |
| 125 | + ) as worker: |
| 126 | + yield worker |
| 127 | + |
| 128 | + |
| 129 | +@pytest.fixture |
| 130 | +def with_celery_worker( |
| 131 | + celery_worker_controller: TestWorkController, |
| 132 | +) -> CeleryTaskWorker: |
| 133 | + assert isinstance(celery_worker_controller.app, Celery) |
| 134 | + return get_celery_worker(celery_worker_controller.app) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.fixture |
| 138 | +def user_id(faker: Faker) -> UserID: |
| 139 | + return TypeAdapter(UserID).validate_python(faker.pyint(min_value=1, max_value=1000)) |
| 140 | + |
| 141 | + |
| 142 | +@pytest.fixture |
| 143 | +def product_name() -> ProductName: |
| 144 | + return TypeAdapter(ProductName).validate_python("pytest-product") |
0 commit comments