Debian Codegraph @

t/smoke/tests/test_consumer.py

raw
1 import pytest
2 from pytest_celery import RESULT_TIMEOUT, CeleryTestSetup, RedisTestBroker
3
4 from celery import Celery
5 from celery.canvas import chain, group
6 from t.smoke.tasks import long_running_task, noop
7
8 WORKER_PREFETCH_MULTIPLIER = 2
9 WORKER_CONCURRENCY = 5
10 MAX_PREFETCH = WORKER_PREFETCH_MULTIPLIER * WORKER_CONCURRENCY
11
12
13 @pytest.fixture
14 def default_worker_app(default_worker_app: Celery) -> Celery:
15 app = default_worker_app
16 app.conf.worker_prefetch_multiplier = WORKER_PREFETCH_MULTIPLIER
17 app.conf.worker_concurrency = WORKER_CONCURRENCY
18 app.conf.visibility_timeout = 3600
19 if app.conf.broker_url.startswith("redis"):
20 app.conf.broker_transport_options = {
21 "visibility_timeout": app.conf.visibility_timeout,
22 "polling_interval": 1,
23 }
24 if app.conf.result_backend.startswith("redis"):
25 app.conf.result_backend_transport_options = {
26 "visibility_timeout": app.conf.visibility_timeout,
27 "polling_interval": 1,
28 }
29 return app
30
31
32 class test_worker_enable_prefetch_count_reduction_true:
33 @pytest.fixture
34 def default_worker_app(self, default_worker_app: Celery) -> Celery:
35 app = default_worker_app
36 app.conf.worker_enable_prefetch_count_reduction = True
37 return app
38
39 @pytest.mark.parametrize("expected_running_tasks_count", range(1, WORKER_CONCURRENCY + 1))
40 def test_reducing_prefetch_count(self, celery_setup: CeleryTestSetup, expected_running_tasks_count: int):
41 if isinstance(celery_setup.broker, RedisTestBroker):
42 # When running in debug it works, when running from CLI it sometimes works
43 pytest.xfail("Test is flaky with Redis broker")
44 sig = group(long_running_task.s(420) for _ in range(expected_running_tasks_count))
45 sig.apply_async(queue=celery_setup.worker.worker_queue)
46 celery_setup.broker.restart()
47
48 expected_reduced_prefetch = max(
49 WORKER_PREFETCH_MULTIPLIER, MAX_PREFETCH - expected_running_tasks_count * WORKER_PREFETCH_MULTIPLIER
50 )
51
52 expected_prefetch_reduce_message = (
53 f"Temporarily reducing the prefetch count to {expected_reduced_prefetch} "
54 f"to avoid over-fetching since {expected_running_tasks_count} tasks are currently being processed."
55 )
56 celery_setup.worker.assert_log_exists(expected_prefetch_reduce_message)
57
58 expected_prefetch_restore_message = (
59 f"The prefetch count will be gradually restored to {MAX_PREFETCH} as the tasks complete processing."
60 )
61 celery_setup.worker.assert_log_exists(expected_prefetch_restore_message)
62
63 def test_prefetch_count_restored(self, celery_setup: CeleryTestSetup):
64 if isinstance(celery_setup.broker, RedisTestBroker):
65 # When running in debug it works, when running from CLI it sometimes works
66 pytest.xfail("Test is flaky with Redis broker")
67 expected_running_tasks_count = MAX_PREFETCH * WORKER_PREFETCH_MULTIPLIER
68 sig = group(long_running_task.s(10) for _ in range(expected_running_tasks_count))
69 sig.apply_async(queue=celery_setup.worker.worker_queue)
70 celery_setup.broker.restart()
71 expected_prefetch_restore_message = (
72 f"Resuming normal operations following a restart.\n"
73 f"Prefetch count has been restored to the maximum of {MAX_PREFETCH}"
74 )
75 celery_setup.worker.assert_log_exists(expected_prefetch_restore_message)
76
77 class test_cancel_tasks_on_connection_loss:
78 @pytest.fixture
79 def default_worker_app(self, default_worker_app: Celery) -> Celery:
80 app = default_worker_app
81 app.conf.worker_prefetch_multiplier = 2
82 app.conf.worker_cancel_long_running_tasks_on_connection_loss = True
83 app.conf.task_acks_late = True
84 return app
85
86 def test_max_prefetch_passed_on_broker_restart(self, celery_setup: CeleryTestSetup):
87 if isinstance(celery_setup.broker, RedisTestBroker):
88 # When running in debug it works, when running from CLI it sometimes works
89 pytest.xfail("Test is flaky with Redis broker")
90 sig = group(long_running_task.s(420) for _ in range(WORKER_CONCURRENCY))
91 sig.apply_async(queue=celery_setup.worker.worker_queue)
92 celery_setup.broker.restart()
93 noop.s().apply_async(queue=celery_setup.worker.worker_queue)
94 celery_setup.worker.assert_log_exists("Task t.smoke.tasks.noop")
95
96
97 class test_worker_enable_prefetch_count_reduction_false:
98 @pytest.fixture
99 def default_worker_app(self, default_worker_app: Celery) -> Celery:
100 app = default_worker_app
101 app.conf.worker_prefetch_multiplier = 1
102 app.conf.worker_enable_prefetch_count_reduction = False
103 app.conf.worker_cancel_long_running_tasks_on_connection_loss = True
104 app.conf.task_acks_late = True
105 return app
106
107 def test_max_prefetch_not_passed_on_broker_restart(self, celery_setup: CeleryTestSetup):
108 if isinstance(celery_setup.broker, RedisTestBroker):
109 # When running in debug it works, when running from CLI it sometimes works
110 pytest.xfail("Test is flaky with Redis broker")
111 sig = group(long_running_task.s(10) for _ in range(WORKER_CONCURRENCY))
112 r = sig.apply_async(queue=celery_setup.worker.worker_queue)
113 celery_setup.broker.restart()
114 noop.s().apply_async(queue=celery_setup.worker.worker_queue)
115 assert "Task t.smoke.tasks.noop" not in celery_setup.worker.logs()
116 r.get(timeout=RESULT_TIMEOUT)
117 assert "Task t.smoke.tasks.noop" in celery_setup.worker.logs()
118
119
120 class test_consumer:
121 def test_worker_consume_tasks_after_redis_broker_restart(
122 self,
123 celery_setup: CeleryTestSetup,
124 ):
125 queue = celery_setup.worker.worker_queue
126 assert noop.s().apply_async(queue=queue).get(timeout=RESULT_TIMEOUT) is None
127 celery_setup.broker.kill()
128 celery_setup.worker.wait_for_log("Trying again in 8.00 seconds... (4/100)")
129 celery_setup.broker.restart()
130
131 count = 5
132 assert (
133 group(noop.s() for _ in range(count))
134 .apply_async(queue=queue)
135 .get(timeout=RESULT_TIMEOUT)
136 == [None] * count
137 )
138
139 assert (
140 chain(
141 group(noop.si() for _ in range(count)),
142 group(noop.si() for _ in range(count)),
143 )
144 .apply_async(queue=queue)
145 .get(timeout=RESULT_TIMEOUT)
146 == [None] * count
147 )