Debian Codegraph @

t/smoke/tests/test_tasks.py

raw
1 from __future__ import annotations
2
3 import pytest
4 from pytest_celery import RESULT_TIMEOUT, CeleryTestSetup, CeleryTestWorker, CeleryWorkerCluster
5 from tenacity import retry, stop_after_attempt, wait_fixed
6
7 from celery import Celery, signature
8 from celery.exceptions import SoftTimeLimitExceeded, TimeLimitExceeded, WorkerLostError
9 from t.integration.tasks import add, identity
10 from t.smoke.conftest import SuiteOperations, TaskTermination
11 from t.smoke.tasks import (replace_with_task, soft_time_limit_lower_than_time_limit,
12 soft_time_limit_must_exceed_time_limit)
13
14
15 class test_task_termination(SuiteOperations):
16 @pytest.fixture
17 def default_worker_app(self, default_worker_app: Celery) -> Celery:
18 app = default_worker_app
19 app.conf.worker_prefetch_multiplier = 1
20 app.conf.worker_concurrency = 1
21 return app
22
23 @pytest.mark.parametrize(
24 "method,expected_error",
25 [
26 (TaskTermination.Method.SIGKILL, WorkerLostError),
27 (TaskTermination.Method.SYSTEM_EXIT, WorkerLostError),
28 (TaskTermination.Method.DELAY_TIMEOUT, TimeLimitExceeded),
29 # Exhausting the memory messes up the CI environment
30 # (TaskTermination.Method.EXHAUST_MEMORY, WorkerLostError),
31 ],
32 )
33 def test_child_process_respawn(
34 self,
35 celery_setup: CeleryTestSetup,
36 method: TaskTermination.Method,
37 expected_error: Exception,
38 ):
39 pinfo_before = celery_setup.worker.get_running_processes_info(
40 ["pid", "name"],
41 filters={"name": "celery"},
42 )
43
44 with pytest.raises(expected_error):
45 self.apply_self_termination_task(celery_setup.worker, method).get()
46
47 # Allowing the worker to respawn the child process before we continue
48 @retry(
49 stop=stop_after_attempt(42),
50 wait=wait_fixed(0.1),
51 reraise=True,
52 )
53 def wait_for_two_celery_processes():
54 pinfo_current = celery_setup.worker.get_running_processes_info(
55 ["pid", "name"],
56 filters={"name": "celery"},
57 )
58 if len(pinfo_current) != 2:
59 assert False, f"Child process did not respawn with method: {method.name}"
60
61 wait_for_two_celery_processes()
62
63 pinfo_after = celery_setup.worker.get_running_processes_info(
64 ["pid", "name"],
65 filters={"name": "celery"},
66 )
67
68 pids_before = {item["pid"] for item in pinfo_before}
69 pids_after = {item["pid"] for item in pinfo_after}
70 assert len(pids_before | pids_after) == 3
71
72 @pytest.mark.parametrize(
73 "method,expected_log,expected_exception_msg",
74 [
75 (
76 TaskTermination.Method.SIGKILL,
77 "Worker exited prematurely: signal 9 (SIGKILL)",
78 None,
79 ),
80 (
81 TaskTermination.Method.SYSTEM_EXIT,
82 "Worker exited prematurely: exitcode 1",
83 None,
84 ),
85 (
86 TaskTermination.Method.DELAY_TIMEOUT,
87 "Hard time limit (2s) exceeded for t.smoke.tasks.self_termination_delay_timeout",
88 "TimeLimitExceeded(2,)",
89 ),
90 # Exhausting the memory messes up the CI environment
91 # (
92 # TaskTermination.Method.EXHAUST_MEMORY,
93 # "Worker exited prematurely: signal 9 (SIGKILL)",
94 # None,
95 # ),
96 ],
97 )
98 def test_terminated_task_logs_correct_error(
99 self,
100 celery_setup: CeleryTestSetup,
101 method: TaskTermination.Method,
102 expected_log: str,
103 expected_exception_msg: str | None,
104 ):
105 try:
106 self.apply_self_termination_task(celery_setup.worker, method).get()
107 except Exception as err:
108 assert expected_exception_msg or expected_log in str(err)
109
110 celery_setup.worker.assert_log_exists(expected_log)
111
112
113 class test_replace:
114 @pytest.fixture
115 def celery_worker_cluster(
116 self,
117 celery_worker: CeleryTestWorker,
118 celery_other_dev_worker: CeleryTestWorker,
119 ) -> CeleryWorkerCluster:
120 cluster = CeleryWorkerCluster(celery_worker, celery_other_dev_worker)
121 yield cluster
122 cluster.teardown()
123
124 def test_sanity(self, celery_setup: CeleryTestSetup):
125 queues = [w.worker_queue for w in celery_setup.worker_cluster]
126 assert len(queues) == 2
127 assert queues[0] != queues[1]
128 replace_with = signature(identity, args=(40,), queue=queues[1])
129 sig1 = replace_with_task.s(replace_with)
130 sig2 = add.s(2).set(queue=queues[1])
131 c = sig1 | sig2
132 r = c.apply_async(queue=queues[0])
133 assert r.get(timeout=RESULT_TIMEOUT) == 42
134
135
136 class test_time_limit:
137 def test_soft_time_limit_lower_than_time_limit(self, celery_setup: CeleryTestSetup):
138 sig = soft_time_limit_lower_than_time_limit.s()
139 result = sig.apply_async(queue=celery_setup.worker.worker_queue)
140 with pytest.raises(SoftTimeLimitExceeded):
141 result.get(timeout=RESULT_TIMEOUT) is None
142
143 def test_soft_time_limit_must_exceed_time_limit(self, celery_setup: CeleryTestSetup):
144 sig = soft_time_limit_must_exceed_time_limit.s()
145 with pytest.raises(ValueError, match="soft_time_limit must be less than or equal to time_limit"):
146 sig.apply_async(queue=celery_setup.worker.worker_queue)