Debian Codegraph @

t/integration/test_rabbitmq_quorum_queue_cycle_detection.py

raw
1 """
2 Integration tests for RabbitMQ cycle detection bug with quorum queues.
3
4 This reproduces the conditions for
5 https://github.com/celery/celery/issues/9867 when running against RabbitMQ
6 <4.0.1.
7 """
8
9 import os
10
11 import pytest
12 from kombu import Exchange, Queue
13
14 from celery import Celery, exceptions
15 from celery.contrib.testing.worker import start_worker
16
17
18 class TaskFailedException(Exception):
19 """Test exception for tasks that should fail and retry."""
20 pass
21
22
23 def get_rabbitmq_url():
24 """Get RabbitMQ broker URL from environment."""
25 user = os.environ.get("RABBITMQ_DEFAULT_USER", "guest")
26 password = os.environ.get("RABBITMQ_DEFAULT_PASSWORD", "guest")
27 return os.environ.get(
28 "TEST_BROKER", f"pyamqp://{user}:{password}@localhost:5672//")
29
30
31 def get_backend_url():
32 """Get backend URL from environment."""
33 return os.environ.get("TEST_BACKEND", "rpc")
34
35
36 def create_test_app_with_quorum_queues():
37 """
38 Create Celery app configured for quorum queue testing.
39
40 Returns:
41 Tuple of (app, queue_name)
42 """
43 broker_url = get_rabbitmq_url()
44 backend_url = get_backend_url()
45
46 app = Celery(
47 "test_cycle_detection",
48 broker=broker_url,
49 backend=backend_url,
50 )
51
52 # Configure queue with quorum queue type
53 queue_name = 'test_cycle_queue'
54 default_exchange = Exchange('celery.topic', type='topic')
55
56 app.conf.task_queues = [
57 Queue(
58 queue_name,
59 exchange=default_exchange,
60 routing_key=queue_name,
61 queue_arguments={'x-queue-type': 'quorum'}
62 ),
63 ]
64
65 app.conf.task_default_queue = queue_name
66
67 # Recommended settings for quorum queues
68 app.conf.broker_transport_options = {"confirm_publish": True}
69
70 # Enable result extended to track retries
71 app.conf.result_extended = True
72
73 return app
74
75
76 @pytest.mark.amqp
77 @pytest.mark.timeout(35)
78 def test_countdown_task_with_retry():
79 """Test where a countdown task gets retried with the same delay."""
80 app = create_test_app_with_quorum_queues()
81
82 @app.task(bind=True, default_retry_delay=3, max_retries=1)
83 def countdown_task_with_retry(self):
84 """Task that fails once then succeeds."""
85 # First attempt: fail to trigger retry
86 if self.request.retries < 1:
87 raise self.retry(exc=TaskFailedException("Simulated failure"))
88
89 return self.request.retries
90
91 with start_worker(
92 app,
93 loglevel="INFO",
94 shutdown_timeout=15,
95 ):
96 # Execute task with countdown=3 (same as default_retry_delay)
97 # We need to hit the same delayed buckets to trigger the cycle detection
98 # in RabbitMQ.
99 result = countdown_task_with_retry.apply_async(countdown=3)
100
101 try:
102 # Wait for the task to complete, we must wait at least long enough
103 # for the countdown to be reached and the retry to occur (6s).
104 value = result.get(timeout=15)
105 except exceptions.TimeoutError as e:
106 pytest.fail(
107 f"Task was silently dropped by RabbitMQ due to cycle detection. "
108 f"Exception: {e!r}."
109 )
110
111 assert value == 1, \
112 f"Expected task to succeed after 1 retry, got: {value}"
113
114
115 @pytest.mark.amqp
116 @pytest.mark.timeout(35)
117 def test_non_eta_task_with_multiple_retries():
118 """Test where a task gets retried twice with the same delay."""
119 app = create_test_app_with_quorum_queues()
120
121 @app.task(bind=True, max_retries=2)
122 def non_eta_task_with_retries(self):
123 """Task that fails twice then succeeds."""
124 # First and second attempts: fail to trigger retry
125 if self.request.retries < 2:
126 raise self.retry(exc=TaskFailedException("Simulated failure"), countdown=3)
127
128 return self.request.retries
129
130 with start_worker(
131 app,
132 loglevel="INFO",
133 shutdown_timeout=15,
134 ):
135 result = non_eta_task_with_retries.apply_async()
136
137 try:
138 # Wait for the task to complete, we must wait at least long enough
139 # for the two retries to occur (6s).
140 value = result.get(timeout=15)
141 except exceptions.TimeoutError as e:
142 pytest.fail(
143 f"Task was silently dropped by RabbitMQ due to cycle detection. "
144 f"Exception: {e!r}"
145 )
146
147 assert value == 2, \
148 f"Expected task to succeed after 2 retries, got: {value}"