Debian Codegraph @

t/integration/conftest.py

raw
1 import json
2 import logging
3 import os
4 import re
5 import time
6
7 import pytest
8
9 from celery.contrib.pytest import celery_app, celery_session_worker
10 from celery.contrib.testing.manager import Manager
11 from celery.exceptions import TimeoutError
12 from t.integration.tasks import get_redis_connection
13
14 # we have to import the pytest plugin fixtures here,
15 # in case user did not do the `python setup.py develop` yet,
16 # that installs the pytest plugin into the setuptools registry.
17
18
19 logger = logging.getLogger(__name__)
20
21 TEST_BROKER = os.environ.get('TEST_BROKER', 'pyamqp://')
22 TEST_BACKEND = os.environ.get('TEST_BACKEND', 'redis://')
23
24 RETRYABLE_EXCEPTIONS = (OSError, ConnectionError, TimeoutError)
25
26
27 def is_retryable_exception(exc):
28 return isinstance(exc, RETRYABLE_EXCEPTIONS)
29
30
31 _flaky = pytest.mark.flaky(reruns=5, reruns_delay=1, cause=is_retryable_exception)
32 _timeout = pytest.mark.timeout(timeout=300)
33
34
35 def flaky(fn):
36 return _timeout(_flaky(fn))
37
38
39 __all__ = (
40 'celery_app',
41 'celery_session_worker',
42 'flaky',
43 'get_active_redis_channels',
44 )
45
46
47 def get_active_redis_channels():
48 return get_redis_connection().execute_command('PUBSUB CHANNELS')
49
50
51 def check_for_logs(caplog, message: str, max_wait: float = 1.0, interval: float = 0.1) -> bool:
52 start_time = time.monotonic()
53 while time.monotonic() - start_time < max_wait:
54 if any(re.search(message, record.message) for record in caplog.records):
55 return True
56 time.sleep(interval)
57 return False
58
59
60 @pytest.fixture(scope='session')
61 def celery_config(request):
62 config = {
63 'broker_url': TEST_BROKER,
64 'result_backend': TEST_BACKEND,
65 'result_extended': True,
66 'cassandra_servers': ['localhost'],
67 'cassandra_keyspace': 'tests',
68 'cassandra_table': 'tests',
69 'cassandra_read_consistency': 'ONE',
70 'cassandra_write_consistency': 'ONE',
71 }
72 try:
73 # To override the default configuration, create the integration-tests-config.json file
74 # in Celery's root directory.
75 # The file must contain a dictionary of valid configuration name/value pairs.
76 with open(str(request.config.rootdir / "integration-tests-config.json")) as file:
77 overrides = json.load(file)
78 config.update(overrides)
79 except OSError:
80 pass
81 return config
82
83
84 @pytest.fixture(scope='session')
85 def celery_enable_logging():
86 return True
87
88
89 @pytest.fixture(scope='session')
90 def celery_worker_pool():
91 return 'prefork'
92
93
94 @pytest.fixture(scope='session')
95 def celery_includes():
96 return {'t.integration.tasks'}
97
98
99 @pytest.fixture
100 def app(celery_app):
101 yield celery_app
102
103
104 @pytest.fixture
105 def manager(app, celery_session_worker):
106 manager = Manager(app)
107 yield manager
108 try:
109 manager.wait_until_idle()
110 except Exception as e:
111 logger.warning("Failed to stop Celery test manager cleanly: %s", e)
112
113
114 @pytest.fixture(autouse=True)
115 def ZZZZ_set_app_current(app):
116 app.set_current()
117 app.set_default()
118
119
120 @pytest.fixture(scope='session')
121 def celery_class_tasks():
122 from t.integration.tasks import ClassBasedAutoRetryTask
123 return [ClassBasedAutoRetryTask]