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