|
1 | | -"""This module contains tests for the Celery integration. |
2 | | -
|
3 | | -To be able to run those tests, you need to have Redis running on localhost:6379. |
4 | | -We have a docker compose file that you can use to start Redis: |
5 | | -
|
6 | | - ```bash |
7 | | - docker compose up -d redis |
8 | | - ``` |
9 | | -""" |
10 | | - |
11 | 1 | import logging |
12 | | -from typing import Iterator |
| 2 | +from typing import Generator, Iterator |
13 | 3 |
|
14 | 4 | import pytest |
15 | | -import redis |
16 | | -import redis.exceptions |
17 | 5 | from celery import Celery |
18 | 6 | from celery.contrib.testing.worker import start_worker |
19 | 7 | from celery.worker.worker import WorkController |
20 | 8 | from dirty_equals import IsStr |
21 | 9 | from inline_snapshot import snapshot |
22 | 10 | from opentelemetry.instrumentation.celery import CeleryInstrumentor |
| 11 | +from testcontainers.redis import RedisContainer |
23 | 12 |
|
24 | 13 | import logfire |
25 | 14 | from logfire.testing import TestExporter |
26 | 15 |
|
27 | | -pytestmark = [pytest.mark.integration] |
28 | 16 |
|
29 | | -try: |
30 | | - client = redis.Redis() |
31 | | - client.ping() # type: ignore |
32 | | -except redis.exceptions.ConnectionError: # pragma: no cover |
33 | | - pytestmark.append(pytest.mark.skip('Redis is not running')) |
| 17 | +@pytest.fixture(scope='module', autouse=True) |
| 18 | +def redis_container() -> Generator[RedisContainer, None, None]: |
| 19 | + with RedisContainer('redis:latest') as redis: |
| 20 | + yield redis |
34 | 21 |
|
35 | 22 |
|
36 | 23 | @pytest.fixture |
37 | | -def celery_app() -> Iterator[Celery]: |
38 | | - app = Celery('tasks', broker='redis://localhost:6379/0', backend='redis://localhost:6379/0') |
| 24 | +def celery_app(redis_container: RedisContainer) -> Iterator[Celery]: |
| 25 | + redis_uri = f'redis://{redis_container.get_container_host_ip()}:{redis_container.get_exposed_port(6379)}/0' |
| 26 | + app = Celery('tasks', broker=redis_uri, backend=redis_uri) |
39 | 27 |
|
40 | 28 | @app.task(name='tasks.say_hello') # type: ignore |
41 | 29 | def say_hello(): # type: ignore |
|
0 commit comments