Debian Codegraph @

t/integration/test_dedup_chain_dispatch.py

raw
1 """Integration tests for chain/callback dispatch on the dedup fast-path.
2
3 When ``worker_deduplicate_successful_tasks=True`` and
4 ``task_acks_late=True``, a redelivered task that hits the dedup
5 fast-path in ``trace.py`` must still dispatch its chain and link
6 callbacks.
7
8 See https://github.com/celery/celery/issues/9835
9 """
10
11 import pytest
12
13 from celery import chain
14 from celery.contrib.testing.worker import start_worker
15 from celery.result import AsyncResult
16
17 from .conftest import flaky
18 from .tasks import add, identity, reject_then_succeed, store_success_then_reject
19
20 TIMEOUT = 60
21
22
23 @pytest.fixture()
24 def dedup_worker(celery_session_app):
25 """Solo worker with dedup enabled.
26
27 Temporarily enables ``worker_deduplicate_successful_tasks`` on the
28 session app, starts a solo worker, and restores the original
29 setting on teardown.
30 """
31 if not celery_session_app.backend.persistent:
32 raise pytest.skip('Requires a persistent result backend.')
33
34 orig_dedup = celery_session_app.conf.worker_deduplicate_successful_tasks
35 orig_acks_late = celery_session_app.conf.task_acks_late
36 celery_session_app.conf.worker_deduplicate_successful_tasks = True
37 celery_session_app.conf.task_acks_late = True
38
39 try:
40 with start_worker(
41 celery_session_app,
42 pool='solo',
43 concurrency=1,
44 perform_ping_check=False,
45 shutdown_timeout=TIMEOUT,
46 ) as worker:
47 yield worker
48 finally:
49 celery_session_app.conf.worker_deduplicate_successful_tasks = orig_dedup
50 celery_session_app.conf.task_acks_late = orig_acks_late
51
52
53 class test_dedup_chain_dispatch:
54 """Test chain/callback dispatch on the dedup fast-path."""
55
56 @flaky
57 @pytest.mark.usefixtures('dedup_worker')
58 def test_chain_completes_with_dedup_enabled(self):
59 """Smoke test: a normal chain works when dedup is on."""
60 c = chain(add.s(2, 3), add.s(5))
61 assert c().get(timeout=TIMEOUT) == 10
62
63 @flaky
64 @pytest.mark.usefixtures('dedup_worker')
65 def test_reject_requeue_completes_chain(self):
66 """Reject passthrough: chain completes after rejection + redelivery."""
67 c = chain(reject_then_succeed.s(), identity.s())
68 assert c().get(timeout=TIMEOUT) == 'second-pass'
69
70 @flaky
71 @pytest.mark.usefixtures('dedup_worker')
72 def test_dedup_dispatches_chain_on_redelivery(self):
73 """Core test: dedup fast-path dispatches the chain."""
74 c = chain(store_success_then_reject.s(), identity.s())
75 assert c().get(timeout=TIMEOUT) == 'first-pass'
76
77 @flaky
78 @pytest.mark.usefixtures('dedup_worker')
79 def test_dedup_dispatches_callback_on_redelivery(self, celery_session_app):
80 """Dedup fast-path dispatches link callbacks."""
81 import uuid as _uuid
82 cb_id = _uuid.uuid4().hex
83 sig = store_success_then_reject.s()
84 sig.link(identity.s().set(task_id=cb_id))
85 sig.apply_async()
86 cb_result = AsyncResult(cb_id, app=celery_session_app)
87 assert cb_result.get(timeout=TIMEOUT) == 'first-pass'