| 1 |
from contextlib import contextmanager |
| 2 |
from unittest.mock import Mock, PropertyMock, patch, sentinel |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from celery import canvas, group, result, uuid |
| 7 |
from celery.canvas import Signature |
| 8 |
from celery.exceptions import ChordError, Retry |
| 9 |
from celery.result import AsyncResult, EagerResult, GroupResult |
| 10 |
|
| 11 |
|
| 12 |
def passthru(x): |
| 13 |
return x |
| 14 |
|
| 15 |
|
| 16 |
class AnySignatureWithTask(Signature): |
| 17 |
def __eq__(self, other): |
| 18 |
return self.task == other.task |
| 19 |
|
| 20 |
|
| 21 |
class ChordCase: |
| 22 |
|
| 23 |
def setup_method(self): |
| 24 |
|
| 25 |
@self.app.task(shared=False) |
| 26 |
def add(x, y): |
| 27 |
return x + y |
| 28 |
self.add = add |
| 29 |
|
| 30 |
|
| 31 |
class TSR(GroupResult): |
| 32 |
is_ready = True |
| 33 |
value = None |
| 34 |
|
| 35 |
def ready(self): |
| 36 |
return self.is_ready |
| 37 |
|
| 38 |
def join(self, propagate=True, **kwargs): |
| 39 |
if propagate: |
| 40 |
for value in self.value: |
| 41 |
if isinstance(value, Exception): |
| 42 |
raise value |
| 43 |
return self.value |
| 44 |
join_native = join |
| 45 |
|
| 46 |
def _failed_join_report(self): |
| 47 |
for value in self.value: |
| 48 |
if isinstance(value, Exception): |
| 49 |
yield EagerResult('some_id', value, 'FAILURE') |
| 50 |
|
| 51 |
|
| 52 |
class TSRNoReport(TSR): |
| 53 |
|
| 54 |
def _failed_join_report(self): |
| 55 |
return iter([]) |
| 56 |
|
| 57 |
|
| 58 |
@contextmanager |
| 59 |
def patch_unlock_retry(app): |
| 60 |
unlock = app.tasks['celery.chord_unlock'] |
| 61 |
retry = Mock() |
| 62 |
retry.return_value = Retry() |
| 63 |
prev, unlock.retry = unlock.retry, retry |
| 64 |
try: |
| 65 |
yield unlock, retry |
| 66 |
finally: |
| 67 |
unlock.retry = prev |
| 68 |
|
| 69 |
|
| 70 |
class test_unlock_chord_task(ChordCase): |
| 71 |
|
| 72 |
def test_unlock_ready(self): |
| 73 |
|
| 74 |
class AlwaysReady(TSR): |
| 75 |
is_ready = True |
| 76 |
value = [2, 4, 8, 6] |
| 77 |
|
| 78 |
with self._chord_context(AlwaysReady) as (cb, retry, _): |
| 79 |
cb.type.apply_async.assert_called_with( |
| 80 |
([2, 4, 8, 6],), {}, task_id=cb.id, |
| 81 |
) |
| 82 |
# didn't retry |
| 83 |
assert not retry.call_count |
| 84 |
|
| 85 |
def test_deps_ready_fails(self): |
| 86 |
GroupResult = Mock(name='GroupResult') |
| 87 |
GroupResult.return_value.ready.side_effect = KeyError('foo') |
| 88 |
unlock_chord = self.app.tasks['celery.chord_unlock'] |
| 89 |
|
| 90 |
with pytest.raises(KeyError): |
| 91 |
unlock_chord('groupid', Mock(), result=[Mock()], |
| 92 |
GroupResult=GroupResult, result_from_tuple=Mock()) |
| 93 |
|
| 94 |
def test_callback_fails(self): |
| 95 |
|
| 96 |
class AlwaysReady(TSR): |
| 97 |
is_ready = True |
| 98 |
value = [2, 4, 8, 6] |
| 99 |
|
| 100 |
def setup(callback): |
| 101 |
callback.apply_async.side_effect = IOError() |
| 102 |
|
| 103 |
with self._chord_context(AlwaysReady, setup) as (cb, retry, fail): |
| 104 |
fail.assert_called() |
| 105 |
assert fail.call_args[0][0] == cb.id |
| 106 |
assert isinstance(fail.call_args[1]['exc'], ChordError) |
| 107 |
|
| 108 |
def test_unlock_ready_failed(self): |
| 109 |
|
| 110 |
class Failed(TSR): |
| 111 |
is_ready = True |
| 112 |
value = [2, KeyError('foo'), 8, 6] |
| 113 |
|
| 114 |
with self._chord_context(Failed) as (cb, retry, fail_current): |
| 115 |
cb.type.apply_async.assert_not_called() |
| 116 |
# didn't retry |
| 117 |
assert not retry.call_count |
| 118 |
fail_current.assert_called() |
| 119 |
assert fail_current.call_args[0][0] == cb.id |
| 120 |
assert isinstance(fail_current.call_args[1]['exc'], ChordError) |
| 121 |
assert 'some_id' in str(fail_current.call_args[1]['exc']) |
| 122 |
|
| 123 |
def test_unlock_ready_failed_no_culprit(self): |
| 124 |
class Failed(TSRNoReport): |
| 125 |
is_ready = True |
| 126 |
value = [2, KeyError('foo'), 8, 6] |
| 127 |
|
| 128 |
with self._chord_context(Failed) as (cb, retry, fail_current): |
| 129 |
fail_current.assert_called() |
| 130 |
assert fail_current.call_args[0][0] == cb.id |
| 131 |
assert isinstance(fail_current.call_args[1]['exc'], ChordError) |
| 132 |
|
| 133 |
@contextmanager |
| 134 |
def _chord_context(self, ResultCls, setup=None, **kwargs): |
| 135 |
@self.app.task(shared=False) |
| 136 |
def callback(*args, **kwargs): |
| 137 |
pass |
| 138 |
self.app.finalize() |
| 139 |
|
| 140 |
pts, result.GroupResult = result.GroupResult, ResultCls |
| 141 |
callback.apply_async = Mock() |
| 142 |
callback_s = callback.s() |
| 143 |
callback_s.id = 'callback_id' |
| 144 |
fail_current = self.app.backend.fail_from_current_stack = Mock() |
| 145 |
try: |
| 146 |
with patch_unlock_retry(self.app) as (unlock, retry): |
| 147 |
signature, canvas.maybe_signature = ( |
| 148 |
canvas.maybe_signature, passthru, |
| 149 |
) |
| 150 |
if setup: |
| 151 |
setup(callback) |
| 152 |
try: |
| 153 |
assert self.app.tasks['celery.chord_unlock'] is unlock |
| 154 |
try: |
| 155 |
unlock( |
| 156 |
'group_id', callback_s, |
| 157 |
result=[ |
| 158 |
self.app.AsyncResult(r) for r in ['1', 2, 3] |
| 159 |
], |
| 160 |
GroupResult=ResultCls, **kwargs |
| 161 |
) |
| 162 |
except Retry: |
| 163 |
pass |
| 164 |
finally: |
| 165 |
canvas.maybe_signature = signature |
| 166 |
yield callback_s, retry, fail_current |
| 167 |
finally: |
| 168 |
result.GroupResult = pts |
| 169 |
|
| 170 |
def test_when_not_ready(self): |
| 171 |
class NeverReady(TSR): |
| 172 |
is_ready = False |
| 173 |
|
| 174 |
with self._chord_context(NeverReady, interval=10, max_retries=30) \ |
| 175 |
as (cb, retry, _): |
| 176 |
cb.type.apply_async.assert_not_called() |
| 177 |
# did retry |
| 178 |
retry.assert_called_with(countdown=10, max_retries=30) |
| 179 |
|
| 180 |
def test_when_not_ready_with_configured_chord_retry_interval(self): |
| 181 |
class NeverReady(TSR): |
| 182 |
is_ready = False |
| 183 |
|
| 184 |
self.app.conf.result_chord_retry_interval, prev = 42, self.app.conf.result_chord_retry_interval |
| 185 |
try: |
| 186 |
with self._chord_context(NeverReady, max_retries=30) as (cb, retry, _): |
| 187 |
cb.type.apply_async.assert_not_called() |
| 188 |
# did retry |
| 189 |
retry.assert_called_with(countdown=42, max_retries=30) |
| 190 |
finally: |
| 191 |
self.app.conf.result_chord_retry_interval = prev |
| 192 |
|
| 193 |
def test_is_in_registry(self): |
| 194 |
assert 'celery.chord_unlock' in self.app.tasks |
| 195 |
|
| 196 |
def _test_unlock_join_timeout(self, timeout): |
| 197 |
class MockJoinResult(TSR): |
| 198 |
is_ready = True |
| 199 |
value = [(None,)] |
| 200 |
join = Mock(return_value=value) |
| 201 |
join_native = join |
| 202 |
|
| 203 |
self.app.conf.result_chord_join_timeout = timeout |
| 204 |
with self._chord_context(MockJoinResult): |
| 205 |
MockJoinResult.join.assert_called_with( |
| 206 |
timeout=timeout, |
| 207 |
propagate=True, |
| 208 |
) |
| 209 |
|
| 210 |
def test_unlock_join_timeout_default(self): |
| 211 |
self._test_unlock_join_timeout( |
| 212 |
timeout=self.app.conf.result_chord_join_timeout, |
| 213 |
) |
| 214 |
|
| 215 |
def test_unlock_join_timeout_custom(self): |
| 216 |
self._test_unlock_join_timeout(timeout=5.0) |
| 217 |
|
| 218 |
def test_unlock_with_chord_params_default(self): |
| 219 |
@self.app.task(shared=False) |
| 220 |
def mul(x, y): |
| 221 |
return x * y |
| 222 |
|
| 223 |
from celery import chord |
| 224 |
g = group(mul.s(1, 1), mul.s(2, 2)) |
| 225 |
body = mul.s() |
| 226 |
ch = chord(g, body, interval=10) |
| 227 |
|
| 228 |
with patch.object(ch, 'run') as run: |
| 229 |
ch.apply_async() |
| 230 |
run.assert_called_once_with( |
| 231 |
AnySignatureWithTask(g), |
| 232 |
mul.s(), |
| 233 |
(), |
| 234 |
task_id=None, |
| 235 |
kwargs={}, |
| 236 |
interval=10, |
| 237 |
) |
| 238 |
|
| 239 |
def test_unlock_with_chord_params_and_task_id(self): |
| 240 |
@self.app.task(shared=False) |
| 241 |
def mul(x, y): |
| 242 |
return x * y |
| 243 |
|
| 244 |
from celery import chord |
| 245 |
g = group(mul.s(1, 1), mul.s(2, 2)) |
| 246 |
body = mul.s() |
| 247 |
ch = chord(g, body, interval=10) |
| 248 |
|
| 249 |
with patch.object(ch, 'run') as run: |
| 250 |
ch.apply_async(task_id=sentinel.task_id) |
| 251 |
|
| 252 |
run.assert_called_once_with( |
| 253 |
AnySignatureWithTask(g), |
| 254 |
mul.s(), |
| 255 |
(), |
| 256 |
task_id=sentinel.task_id, |
| 257 |
kwargs={}, |
| 258 |
interval=10, |
| 259 |
) |
| 260 |
|
| 261 |
|
| 262 |
class test_chord(ChordCase): |
| 263 |
|
| 264 |
def test_eager(self): |
| 265 |
from celery import chord |
| 266 |
|
| 267 |
@self.app.task(shared=False) |
| 268 |
def addX(x, y): |
| 269 |
return x + y |
| 270 |
|
| 271 |
@self.app.task(shared=False) |
| 272 |
def sumX(n): |
| 273 |
return sum(n) |
| 274 |
|
| 275 |
self.app.conf.task_always_eager = True |
| 276 |
x = chord(addX.s(i, i) for i in range(10)) |
| 277 |
body = sumX.s() |
| 278 |
result = x(body) |
| 279 |
assert result.get() == sum(i + i for i in range(10)) |
| 280 |
|
| 281 |
def test_apply(self): |
| 282 |
self.app.conf.task_always_eager = False |
| 283 |
from celery import chord |
| 284 |
|
| 285 |
m = Mock() |
| 286 |
m.app.conf.task_always_eager = False |
| 287 |
m.AsyncResult = AsyncResult |
| 288 |
prev, chord.run = chord.run, m |
| 289 |
try: |
| 290 |
x = chord(self.add.s(i, i) for i in range(10)) |
| 291 |
body = self.add.s(2) |
| 292 |
result = x(body) |
| 293 |
assert result.id |
| 294 |
# does not modify original signature |
| 295 |
with pytest.raises(KeyError): |
| 296 |
body.options['task_id'] |
| 297 |
chord.run.assert_called() |
| 298 |
finally: |
| 299 |
chord.run = prev |
| 300 |
|
| 301 |
def test_init(self): |
| 302 |
from celery import chord |
| 303 |
from celery.utils.serialization import pickle |
| 304 |
|
| 305 |
@self.app.task(shared=False) |
| 306 |
def addX(x, y): |
| 307 |
return x + y |
| 308 |
|
| 309 |
@self.app.task(shared=False) |
| 310 |
def sumX(n): |
| 311 |
return sum(n) |
| 312 |
|
| 313 |
x = chord(addX.s(i, i) for i in range(10)) |
| 314 |
# kwargs used to nest and recurse in serialization/deserialization |
| 315 |
# (#6810) |
| 316 |
assert x.kwargs['kwargs'] == {} |
| 317 |
assert pickle.loads(pickle.dumps(x)).kwargs == x.kwargs |
| 318 |
|
| 319 |
|
| 320 |
class test_add_to_chord: |
| 321 |
|
| 322 |
def setup_method(self): |
| 323 |
|
| 324 |
@self.app.task(shared=False) |
| 325 |
def add(x, y): |
| 326 |
return x + y |
| 327 |
self.add = add |
| 328 |
|
| 329 |
@self.app.task(shared=False, bind=True) |
| 330 |
def adds(self, sig, lazy=False): |
| 331 |
return self.add_to_chord(sig, lazy) |
| 332 |
self.adds = adds |
| 333 |
|
| 334 |
@patch('celery.Celery.backend', new=PropertyMock(name='backend')) |
| 335 |
def test_add_to_chord(self): |
| 336 |
sig = self.add.s(2, 2) |
| 337 |
sig.delay = Mock(name='sig.delay') |
| 338 |
self.adds.request.group = uuid() |
| 339 |
self.adds.request.id = uuid() |
| 340 |
|
| 341 |
with pytest.raises(ValueError): |
| 342 |
# task not part of chord |
| 343 |
self.adds.run(sig) |
| 344 |
self.adds.request.chord = self.add.s() |
| 345 |
|
| 346 |
res1 = self.adds.run(sig, True) |
| 347 |
assert res1 == sig |
| 348 |
assert sig.options['task_id'] |
| 349 |
assert sig.options['group_id'] == self.adds.request.group |
| 350 |
assert sig.options['chord'] == self.adds.request.chord |
| 351 |
sig.delay.assert_not_called() |
| 352 |
self.app.backend.add_to_chord.assert_called_with( |
| 353 |
self.adds.request.group, sig.freeze(), |
| 354 |
) |
| 355 |
|
| 356 |
self.app.backend.reset_mock() |
| 357 |
sig2 = self.add.s(4, 4) |
| 358 |
sig2.delay = Mock(name='sig2.delay') |
| 359 |
res2 = self.adds.run(sig2) |
| 360 |
assert res2 == sig2.delay.return_value |
| 361 |
assert sig2.options['task_id'] |
| 362 |
assert sig2.options['group_id'] == self.adds.request.group |
| 363 |
assert sig2.options['chord'] == self.adds.request.chord |
| 364 |
sig2.delay.assert_called_with() |
| 365 |
self.app.backend.add_to_chord.assert_called_with( |
| 366 |
self.adds.request.group, sig2.freeze(), |
| 367 |
) |
| 368 |
|
| 369 |
|
| 370 |
class test_Chord_task(ChordCase): |
| 371 |
|
| 372 |
@patch('celery.Celery.backend', new=PropertyMock(name='backend')) |
| 373 |
def test_run(self): |
| 374 |
self.app.backend.cleanup = Mock() |
| 375 |
self.app.backend.cleanup.__name__ = 'cleanup' |
| 376 |
Chord = self.app.tasks['celery.chord'] |
| 377 |
|
| 378 |
body = self.add.signature() |
| 379 |
Chord(group(self.add.signature((i, i)) for i in range(5)), body) |
| 380 |
Chord([self.add.signature((j, j)) for j in range(5)], body) |
| 381 |
assert self.app.backend.apply_chord.call_count == 2 |
| 382 |
|
| 383 |
@patch('celery.Celery.backend', new=PropertyMock(name='backend')) |
| 384 |
def test_run__chord_size_set(self): |
| 385 |
Chord = self.app.tasks['celery.chord'] |
| 386 |
body = self.add.signature() |
| 387 |
group_size = 4 |
| 388 |
group1 = group(self.add.signature((i, i)) for i in range(group_size)) |
| 389 |
result = Chord(group1, body) |
| 390 |
|
| 391 |
self.app.backend.set_chord_size.assert_called_once_with(result.parent.id, group_size) |