test_result.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. import traceback
  4. from contextlib import contextmanager
  5. from case import Mock, call, patch, skip
  6. from celery import uuid
  7. from celery import states
  8. from celery.backends.base import SyncBackendMixin
  9. from celery.exceptions import (
  10. CPendingDeprecationWarning, ImproperlyConfigured,
  11. IncompleteStream, TimeoutError,
  12. )
  13. from celery.five import range
  14. from celery.result import (
  15. AsyncResult,
  16. EagerResult,
  17. ResultSet,
  18. GroupResult,
  19. result_from_tuple,
  20. assert_will_not_block,
  21. )
  22. from celery.utils.serialization import pickle
  23. PYTRACEBACK = """\
  24. Traceback (most recent call last):
  25. File "foo.py", line 2, in foofunc
  26. don't matter
  27. File "bar.py", line 3, in barfunc
  28. don't matter
  29. Doesn't matter: really!\
  30. """
  31. def mock_task(name, state, result, traceback=None):
  32. return dict(
  33. id=uuid(), name=name, state=state,
  34. result=result, traceback=traceback,
  35. )
  36. def save_result(app, task):
  37. traceback = task.get('traceback') or 'Some traceback'
  38. if task['state'] == states.SUCCESS:
  39. app.backend.mark_as_done(task['id'], task['result'])
  40. elif task['state'] == states.RETRY:
  41. app.backend.mark_as_retry(
  42. task['id'], task['result'], traceback=traceback,
  43. )
  44. else:
  45. app.backend.mark_as_failure(
  46. task['id'], task['result'], traceback=traceback,
  47. )
  48. def make_mock_group(app, size=10):
  49. tasks = [mock_task('ts%d' % i, states.SUCCESS, i) for i in range(size)]
  50. [save_result(app, task) for task in tasks]
  51. return [app.AsyncResult(task['id']) for task in tasks]
  52. class _MockBackend:
  53. def add_pending_result(self, *args, **kwargs):
  54. return True
  55. def wait_for_pending(self, *args, **kwargs):
  56. return True
  57. class test_AsyncResult:
  58. def setup(self):
  59. self.app.conf.result_cache_max = 100
  60. self.app.conf.result_serializer = 'pickle'
  61. self.task1 = mock_task('task1', states.SUCCESS, 'the')
  62. self.task2 = mock_task('task2', states.SUCCESS, 'quick')
  63. self.task3 = mock_task('task3', states.FAILURE, KeyError('brown'))
  64. self.task4 = mock_task('task3', states.RETRY, KeyError('red'))
  65. self.task5 = mock_task(
  66. 'task3', states.FAILURE, KeyError('blue'), PYTRACEBACK,
  67. )
  68. for task in (self.task1, self.task2,
  69. self.task3, self.task4, self.task5):
  70. save_result(self.app, task)
  71. @self.app.task(shared=False)
  72. def mytask():
  73. pass
  74. self.mytask = mytask
  75. @patch('celery.result.task_join_will_block')
  76. def test_assert_will_not_block(self, task_join_will_block):
  77. task_join_will_block.return_value = True
  78. with pytest.raises(RuntimeError):
  79. assert_will_not_block()
  80. task_join_will_block.return_value = False
  81. assert_will_not_block()
  82. @patch('celery.result.task_join_will_block')
  83. def test_get_sync_subtask_option(self, task_join_will_block):
  84. task_join_will_block.return_value = True
  85. tid = uuid()
  86. backend = _MockBackend()
  87. res_subtask_async = AsyncResult(tid, backend=backend)
  88. with pytest.raises(RuntimeError):
  89. res_subtask_async.get()
  90. res_subtask_async.get(disable_sync_subtasks=False)
  91. def test_without_id(self):
  92. with pytest.raises(ValueError):
  93. AsyncResult(None, app=self.app)
  94. def test_compat_properties(self):
  95. x = self.app.AsyncResult('1')
  96. assert x.task_id == x.id
  97. x.task_id = '2'
  98. assert x.id == '2'
  99. @pytest.mark.usefixtures('depends_on_current_app')
  100. def test_reduce_direct(self):
  101. x = AsyncResult('1', app=self.app)
  102. fun, args = x.__reduce__()
  103. assert fun(*args) == x
  104. def test_children(self):
  105. x = self.app.AsyncResult('1')
  106. children = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  107. x._cache = {'children': children, 'status': states.SUCCESS}
  108. x.backend = Mock()
  109. assert x.children
  110. assert len(x.children) == 3
  111. def test_propagates_for_parent(self):
  112. x = self.app.AsyncResult(uuid())
  113. x.backend = Mock(name='backend')
  114. x.backend.get_task_meta.return_value = {}
  115. x.backend.wait_for_pending.return_value = 84
  116. x.parent = EagerResult(uuid(), KeyError('foo'), states.FAILURE)
  117. with pytest.raises(KeyError):
  118. x.get(propagate=True)
  119. x.backend.wait_for_pending.assert_not_called()
  120. x.parent = EagerResult(uuid(), 42, states.SUCCESS)
  121. assert x.get(propagate=True) == 84
  122. x.backend.wait_for_pending.assert_called()
  123. def test_get_children(self):
  124. tid = uuid()
  125. x = self.app.AsyncResult(tid)
  126. child = [self.app.AsyncResult(uuid()).as_tuple()
  127. for i in range(10)]
  128. x._cache = {'children': child}
  129. assert x.children
  130. assert len(x.children) == 10
  131. x._cache = {'status': states.SUCCESS}
  132. x.backend._cache[tid] = {'result': None}
  133. assert x.children is None
  134. def test_build_graph_get_leaf_collect(self):
  135. x = self.app.AsyncResult('1')
  136. x.backend._cache['1'] = {'status': states.SUCCESS, 'result': None}
  137. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  138. x.iterdeps = Mock()
  139. x.iterdeps.return_value = (
  140. (None, x),
  141. (x, c[0]),
  142. (c[0], c[1]),
  143. (c[1], c[2])
  144. )
  145. x.backend.READY_STATES = states.READY_STATES
  146. assert x.graph
  147. assert x.get_leaf() is 2
  148. it = x.collect()
  149. assert list(it) == [
  150. (x, None),
  151. (c[0], 0),
  152. (c[1], 1),
  153. (c[2], 2),
  154. ]
  155. def test_iterdeps(self):
  156. x = self.app.AsyncResult('1')
  157. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  158. x._cache = {'status': states.SUCCESS, 'result': None, 'children': c}
  159. for child in c:
  160. child.backend = Mock()
  161. child.backend.get_children.return_value = []
  162. it = x.iterdeps()
  163. assert list(it) == [
  164. (None, x),
  165. (x, c[0]),
  166. (x, c[1]),
  167. (x, c[2]),
  168. ]
  169. x._cache = None
  170. x.ready = Mock()
  171. x.ready.return_value = False
  172. with pytest.raises(IncompleteStream):
  173. list(x.iterdeps())
  174. list(x.iterdeps(intermediate=True))
  175. def test_eq_not_implemented(self):
  176. assert self.app.AsyncResult('1') != object()
  177. @pytest.mark.usefixtures('depends_on_current_app')
  178. def test_reduce(self):
  179. a1 = self.app.AsyncResult('uuid')
  180. restored = pickle.loads(pickle.dumps(a1))
  181. assert restored.id == 'uuid'
  182. a2 = self.app.AsyncResult('uuid')
  183. assert pickle.loads(pickle.dumps(a2)).id == 'uuid'
  184. def test_maybe_set_cache_empty(self):
  185. self.app.AsyncResult('uuid')._maybe_set_cache(None)
  186. def test_set_cache__children(self):
  187. r1 = self.app.AsyncResult('id1')
  188. r2 = self.app.AsyncResult('id2')
  189. r1._set_cache({'children': [r2.as_tuple()]})
  190. assert r2 in r1.children
  191. def test_successful(self):
  192. ok_res = self.app.AsyncResult(self.task1['id'])
  193. nok_res = self.app.AsyncResult(self.task3['id'])
  194. nok_res2 = self.app.AsyncResult(self.task4['id'])
  195. assert ok_res.successful()
  196. assert not nok_res.successful()
  197. assert not nok_res2.successful()
  198. pending_res = self.app.AsyncResult(uuid())
  199. assert not pending_res.successful()
  200. def test_raising(self):
  201. notb = self.app.AsyncResult(self.task3['id'])
  202. withtb = self.app.AsyncResult(self.task5['id'])
  203. with pytest.raises(KeyError):
  204. notb.get()
  205. try:
  206. withtb.get()
  207. except KeyError:
  208. tb = traceback.format_exc()
  209. assert ' File "foo.py", line 2, in foofunc' not in tb
  210. assert ' File "bar.py", line 3, in barfunc' not in tb
  211. assert 'KeyError:' in tb
  212. assert "'blue'" in tb
  213. else:
  214. raise AssertionError('Did not raise KeyError.')
  215. @skip.unless_module('tblib')
  216. def test_raising_remote_tracebacks(self):
  217. withtb = self.app.AsyncResult(self.task5['id'])
  218. self.app.conf.task_remote_tracebacks = True
  219. try:
  220. withtb.get()
  221. except KeyError:
  222. tb = traceback.format_exc()
  223. assert ' File "foo.py", line 2, in foofunc' in tb
  224. assert ' File "bar.py", line 3, in barfunc' in tb
  225. assert 'KeyError:' in tb
  226. assert "'blue'" in tb
  227. else:
  228. raise AssertionError('Did not raise KeyError.')
  229. def test_str(self):
  230. ok_res = self.app.AsyncResult(self.task1['id'])
  231. ok2_res = self.app.AsyncResult(self.task2['id'])
  232. nok_res = self.app.AsyncResult(self.task3['id'])
  233. assert str(ok_res) == self.task1['id']
  234. assert str(ok2_res) == self.task2['id']
  235. assert str(nok_res) == self.task3['id']
  236. pending_id = uuid()
  237. pending_res = self.app.AsyncResult(pending_id)
  238. assert str(pending_res) == pending_id
  239. def test_repr(self):
  240. ok_res = self.app.AsyncResult(self.task1['id'])
  241. ok2_res = self.app.AsyncResult(self.task2['id'])
  242. nok_res = self.app.AsyncResult(self.task3['id'])
  243. assert repr(ok_res) == '<AsyncResult: %s>' % (self.task1['id'],)
  244. assert repr(ok2_res) == '<AsyncResult: %s>' % (self.task2['id'],)
  245. assert repr(nok_res) == '<AsyncResult: %s>' % (self.task3['id'],)
  246. pending_id = uuid()
  247. pending_res = self.app.AsyncResult(pending_id)
  248. assert repr(pending_res) == '<AsyncResult: %s>' % (pending_id,)
  249. def test_hash(self):
  250. assert (hash(self.app.AsyncResult('x0w991')) ==
  251. hash(self.app.AsyncResult('x0w991')))
  252. assert (hash(self.app.AsyncResult('x0w991')) !=
  253. hash(self.app.AsyncResult('x1w991')))
  254. def test_get_traceback(self):
  255. ok_res = self.app.AsyncResult(self.task1['id'])
  256. nok_res = self.app.AsyncResult(self.task3['id'])
  257. nok_res2 = self.app.AsyncResult(self.task4['id'])
  258. assert not ok_res.traceback
  259. assert nok_res.traceback
  260. assert nok_res2.traceback
  261. pending_res = self.app.AsyncResult(uuid())
  262. assert not pending_res.traceback
  263. def test_get__backend_gives_None(self):
  264. res = self.app.AsyncResult(self.task1['id'])
  265. res.backend.wait_for = Mock(name='wait_for')
  266. res.backend.wait_for.return_value = None
  267. assert res.get() is None
  268. def test_get(self):
  269. ok_res = self.app.AsyncResult(self.task1['id'])
  270. ok2_res = self.app.AsyncResult(self.task2['id'])
  271. nok_res = self.app.AsyncResult(self.task3['id'])
  272. nok2_res = self.app.AsyncResult(self.task4['id'])
  273. callback = Mock(name='callback')
  274. assert ok_res.get(callback=callback) == 'the'
  275. callback.assert_called_with(ok_res.id, 'the')
  276. assert ok2_res.get() == 'quick'
  277. with pytest.raises(KeyError):
  278. nok_res.get()
  279. assert nok_res.get(propagate=False)
  280. assert isinstance(nok2_res.result, KeyError)
  281. assert ok_res.info == 'the'
  282. def test_eq_ne(self):
  283. r1 = self.app.AsyncResult(self.task1['id'])
  284. r2 = self.app.AsyncResult(self.task1['id'])
  285. r3 = self.app.AsyncResult(self.task2['id'])
  286. assert r1 == r2
  287. assert r1 != r3
  288. assert r1 == r2.id
  289. assert r1 != r3.id
  290. @pytest.mark.usefixtures('depends_on_current_app')
  291. def test_reduce_restore(self):
  292. r1 = self.app.AsyncResult(self.task1['id'])
  293. fun, args = r1.__reduce__()
  294. assert fun(*args) == r1
  295. def test_get_timeout(self):
  296. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  297. with pytest.raises(TimeoutError):
  298. res.get(timeout=0.001)
  299. pending_res = self.app.AsyncResult(uuid())
  300. with patch('celery.result.time') as _time:
  301. with pytest.raises(TimeoutError):
  302. pending_res.get(timeout=0.001, interval=0.001)
  303. _time.sleep.assert_called_with(0.001)
  304. def test_get_timeout_longer(self):
  305. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  306. with patch('celery.result.time') as _time:
  307. with pytest.raises(TimeoutError):
  308. res.get(timeout=1, interval=1)
  309. _time.sleep.assert_called_with(1)
  310. def test_ready(self):
  311. oks = (self.app.AsyncResult(self.task1['id']),
  312. self.app.AsyncResult(self.task2['id']),
  313. self.app.AsyncResult(self.task3['id']))
  314. assert all(result.ready() for result in oks)
  315. assert not self.app.AsyncResult(self.task4['id']).ready()
  316. assert not self.app.AsyncResult(uuid()).ready()
  317. class test_ResultSet:
  318. def test_resultset_repr(self):
  319. assert repr(self.app.ResultSet(
  320. [self.app.AsyncResult(t) for t in ['1', '2', '3']]))
  321. def test_eq_other(self):
  322. assert self.app.ResultSet([
  323. self.app.AsyncResult(t) for t in [1, 3, 3]]) != 1
  324. rs1 = self.app.ResultSet([self.app.AsyncResult(1)])
  325. rs2 = self.app.ResultSet([self.app.AsyncResult(1)])
  326. assert rs1 == rs2
  327. def test_get(self):
  328. x = self.app.ResultSet([self.app.AsyncResult(t) for t in [1, 2, 3]])
  329. b = x.results[0].backend = Mock()
  330. b.supports_native_join = False
  331. x.join_native = Mock()
  332. x.join = Mock()
  333. x.get()
  334. x.join.assert_called()
  335. b.supports_native_join = True
  336. x.get()
  337. x.join_native.assert_called()
  338. def test_eq_ne(self):
  339. g1 = self.app.ResultSet([
  340. self.app.AsyncResult('id1'),
  341. self.app.AsyncResult('id2'),
  342. ])
  343. g2 = self.app.ResultSet([
  344. self.app.AsyncResult('id1'),
  345. self.app.AsyncResult('id2'),
  346. ])
  347. g3 = self.app.ResultSet([
  348. self.app.AsyncResult('id3'),
  349. self.app.AsyncResult('id1'),
  350. ])
  351. assert g1 == g2
  352. assert g1 != g3
  353. assert g1 != object()
  354. def test_takes_app_from_first_task(self):
  355. x = ResultSet([self.app.AsyncResult('id1')])
  356. assert x.app is x.results[0].app
  357. x.app = self.app
  358. assert x.app is self.app
  359. def test_get_empty(self):
  360. x = self.app.ResultSet([])
  361. assert x.supports_native_join is None
  362. x.join = Mock(name='join')
  363. x.get()
  364. x.join.assert_called()
  365. def test_add(self):
  366. x = self.app.ResultSet([self.app.AsyncResult(1)])
  367. x.add(self.app.AsyncResult(2))
  368. assert len(x) == 2
  369. x.add(self.app.AsyncResult(2))
  370. assert len(x) == 2
  371. @contextmanager
  372. def dummy_copy(self):
  373. with patch('celery.result.copy') as copy:
  374. def passt(arg):
  375. return arg
  376. copy.side_effect = passt
  377. yield
  378. def test_iterate_respects_subpolling_interval(self):
  379. r1 = self.app.AsyncResult(uuid())
  380. r2 = self.app.AsyncResult(uuid())
  381. backend = r1.backend = r2.backend = Mock()
  382. backend.subpolling_interval = 10
  383. ready = r1.ready = r2.ready = Mock()
  384. def se(*args, **kwargs):
  385. ready.side_effect = KeyError()
  386. return False
  387. ready.return_value = False
  388. ready.side_effect = se
  389. x = self.app.ResultSet([r1, r2])
  390. with self.dummy_copy():
  391. with patch('celery.result.time') as _time:
  392. with pytest.warns(CPendingDeprecationWarning):
  393. with pytest.raises(KeyError):
  394. list(x.iterate())
  395. _time.sleep.assert_called_with(10)
  396. backend.subpolling_interval = 0
  397. with patch('celery.result.time') as _time:
  398. with pytest.warns(CPendingDeprecationWarning):
  399. with pytest.raises(KeyError):
  400. ready.return_value = False
  401. ready.side_effect = se
  402. list(x.iterate())
  403. _time.sleep.assert_not_called()
  404. def test_times_out(self):
  405. r1 = self.app.AsyncResult(uuid)
  406. r1.ready = Mock()
  407. r1.ready.return_value = False
  408. x = self.app.ResultSet([r1])
  409. with self.dummy_copy():
  410. with patch('celery.result.time'):
  411. with pytest.warns(CPendingDeprecationWarning):
  412. with pytest.raises(TimeoutError):
  413. list(x.iterate(timeout=1))
  414. def test_add_discard(self):
  415. x = self.app.ResultSet([])
  416. x.add(self.app.AsyncResult('1'))
  417. assert self.app.AsyncResult('1') in x.results
  418. x.discard(self.app.AsyncResult('1'))
  419. x.discard(self.app.AsyncResult('1'))
  420. x.discard('1')
  421. assert self.app.AsyncResult('1') not in x.results
  422. x.update([self.app.AsyncResult('2')])
  423. def test_clear(self):
  424. x = self.app.ResultSet([])
  425. r = x.results
  426. x.clear()
  427. assert x.results is r
  428. class MockAsyncResultFailure(AsyncResult):
  429. @property
  430. def result(self):
  431. return KeyError('baz')
  432. @property
  433. def state(self):
  434. return states.FAILURE
  435. def get(self, propagate=True, **kwargs):
  436. if propagate:
  437. raise self.result
  438. return self.result
  439. class MockAsyncResultSuccess(AsyncResult):
  440. forgotten = False
  441. def forget(self):
  442. self.forgotten = True
  443. @property
  444. def result(self):
  445. return 42
  446. @property
  447. def state(self):
  448. return states.SUCCESS
  449. def get(self, **kwargs):
  450. return self.result
  451. class SimpleBackend(SyncBackendMixin):
  452. ids = []
  453. def __init__(self, ids=[]):
  454. self.ids = ids
  455. def _ensure_not_eager(self):
  456. pass
  457. def get_many(self, *args, **kwargs):
  458. return ((id, {'result': i, 'status': states.SUCCESS})
  459. for i, id in enumerate(self.ids))
  460. class test_GroupResult:
  461. def setup(self):
  462. self.size = 10
  463. self.ts = self.app.GroupResult(
  464. uuid(), make_mock_group(self.app, self.size),
  465. )
  466. @pytest.mark.usefixtures('depends_on_current_app')
  467. def test_is_pickleable(self):
  468. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  469. assert pickle.loads(pickle.dumps(ts)) == ts
  470. ts2 = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  471. assert pickle.loads(pickle.dumps(ts2)) == ts2
  472. @pytest.mark.usefixtures('depends_on_current_app')
  473. def test_reduce(self):
  474. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  475. fun, args = ts.__reduce__()
  476. ts2 = fun(*args)
  477. assert ts2.id == ts.id
  478. assert ts == ts2
  479. def test_eq_ne(self):
  480. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  481. ts2 = self.app.GroupResult(ts.id, ts.results)
  482. ts3 = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  483. ts4 = self.app.GroupResult(ts.id, [self.app.AsyncResult(uuid())])
  484. assert ts == ts2
  485. assert ts != ts3
  486. assert ts != ts4
  487. assert ts != object()
  488. def test_len(self):
  489. assert len(self.ts) == self.size
  490. def test_eq_other(self):
  491. assert self.ts != 1
  492. @pytest.mark.usefixtures('depends_on_current_app')
  493. def test_pickleable(self):
  494. assert pickle.loads(pickle.dumps(self.ts))
  495. def test_iterate_raises(self):
  496. ar = MockAsyncResultFailure(uuid(), app=self.app)
  497. ts = self.app.GroupResult(uuid(), [ar])
  498. with pytest.warns(CPendingDeprecationWarning):
  499. it = ts.iterate()
  500. with pytest.raises(KeyError):
  501. next(it)
  502. def test_forget(self):
  503. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  504. MockAsyncResultSuccess(uuid(), app=self.app)]
  505. ts = self.app.GroupResult(uuid(), subs)
  506. ts.forget()
  507. for sub in subs:
  508. assert sub.forgotten
  509. def test_getitem(self):
  510. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  511. MockAsyncResultSuccess(uuid(), app=self.app)]
  512. ts = self.app.GroupResult(uuid(), subs)
  513. assert ts[0] is subs[0]
  514. def test_save_restore(self):
  515. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  516. MockAsyncResultSuccess(uuid(), app=self.app)]
  517. ts = self.app.GroupResult(uuid(), subs)
  518. ts.save()
  519. with pytest.raises(AttributeError):
  520. ts.save(backend=object())
  521. assert self.app.GroupResult.restore(ts.id).results == ts.results
  522. ts.delete()
  523. assert self.app.GroupResult.restore(ts.id) is None
  524. with pytest.raises(AttributeError):
  525. self.app.GroupResult.restore(ts.id, backend=object())
  526. def test_restore_app(self):
  527. subs = [MockAsyncResultSuccess(uuid(), app=self.app)]
  528. ts = self.app.GroupResult(uuid(), subs)
  529. ts.save()
  530. restored = GroupResult.restore(ts.id, app=self.app)
  531. assert restored.id == ts.id
  532. def test_join_native(self):
  533. backend = SimpleBackend()
  534. results = [self.app.AsyncResult(uuid(), backend=backend)
  535. for i in range(10)]
  536. ts = self.app.GroupResult(uuid(), results)
  537. ts.app.backend = backend
  538. backend.ids = [result.id for result in results]
  539. res = ts.join_native()
  540. assert res == list(range(10))
  541. callback = Mock(name='callback')
  542. assert not ts.join_native(callback=callback)
  543. callback.assert_has_calls([
  544. call(r.id, i) for i, r in enumerate(ts.results)
  545. ])
  546. def test_join_native_raises(self):
  547. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  548. ts.iter_native = Mock()
  549. ts.iter_native.return_value = iter([
  550. (uuid(), {'status': states.FAILURE, 'result': KeyError()})
  551. ])
  552. with pytest.raises(KeyError):
  553. ts.join_native(propagate=True)
  554. def test_failed_join_report(self):
  555. res = Mock()
  556. ts = self.app.GroupResult(uuid(), [res])
  557. res.state = states.FAILURE
  558. res.backend.is_cached.return_value = True
  559. assert next(ts._failed_join_report()) is res
  560. res.backend.is_cached.return_value = False
  561. with pytest.raises(StopIteration):
  562. next(ts._failed_join_report())
  563. def test_repr(self):
  564. assert repr(
  565. self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())]))
  566. def test_children_is_results(self):
  567. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  568. assert ts.children is ts.results
  569. def test_iter_native(self):
  570. backend = SimpleBackend()
  571. results = [self.app.AsyncResult(uuid(), backend=backend)
  572. for i in range(10)]
  573. ts = self.app.GroupResult(uuid(), results)
  574. ts.app.backend = backend
  575. backend.ids = [result.id for result in results]
  576. assert len(list(ts.iter_native())) == 10
  577. def test_iterate_yields(self):
  578. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  579. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  580. ts = self.app.GroupResult(uuid(), [ar, ar2])
  581. with pytest.warns(CPendingDeprecationWarning):
  582. it = ts.iterate()
  583. assert next(it) == 42
  584. assert next(it) == 42
  585. def test_iterate_eager(self):
  586. ar1 = EagerResult(uuid(), 42, states.SUCCESS)
  587. ar2 = EagerResult(uuid(), 42, states.SUCCESS)
  588. ts = self.app.GroupResult(uuid(), [ar1, ar2])
  589. with pytest.warns(CPendingDeprecationWarning):
  590. it = ts.iterate()
  591. assert next(it) == 42
  592. assert next(it) == 42
  593. def test_join_timeout(self):
  594. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  595. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  596. ar3 = self.app.AsyncResult(uuid())
  597. ts = self.app.GroupResult(uuid(), [ar, ar2, ar3])
  598. with pytest.raises(TimeoutError):
  599. ts.join(timeout=0.0000001)
  600. ar4 = self.app.AsyncResult(uuid())
  601. ar4.get = Mock()
  602. ts2 = self.app.GroupResult(uuid(), [ar4])
  603. assert ts2.join(timeout=0.1)
  604. callback = Mock(name='callback')
  605. assert not ts2.join(timeout=0.1, callback=callback)
  606. callback.assert_called_with(ar4.id, ar4.get())
  607. def test_iter_native_when_empty_group(self):
  608. ts = self.app.GroupResult(uuid(), [])
  609. assert list(ts.iter_native()) == []
  610. def test_iterate_simple(self):
  611. with pytest.warns(CPendingDeprecationWarning):
  612. it = self.ts.iterate()
  613. results = sorted(list(it))
  614. assert results == list(range(self.size))
  615. def test___iter__(self):
  616. assert list(iter(self.ts)) == self.ts.results
  617. def test_join(self):
  618. joined = self.ts.join()
  619. assert joined == list(range(self.size))
  620. def test_successful(self):
  621. assert self.ts.successful()
  622. def test_failed(self):
  623. assert not self.ts.failed()
  624. def test_maybe_throw(self):
  625. self.ts.results = [Mock(name='r1')]
  626. self.ts.maybe_throw()
  627. self.ts.results[0].maybe_throw.assert_called_with(
  628. callback=None, propagate=True,
  629. )
  630. def test_join__on_message(self):
  631. with pytest.raises(ImproperlyConfigured):
  632. self.ts.join(on_message=Mock())
  633. def test_waiting(self):
  634. assert not self.ts.waiting()
  635. def test_ready(self):
  636. assert self.ts.ready()
  637. def test_completed_count(self):
  638. assert self.ts.completed_count() == len(self.ts)
  639. class test_pending_AsyncResult:
  640. def test_result(self, app):
  641. res = app.AsyncResult(uuid())
  642. assert res.result is None
  643. class test_failed_AsyncResult:
  644. def setup(self):
  645. self.size = 11
  646. self.app.conf.result_serializer = 'pickle'
  647. results = make_mock_group(self.app, 10)
  648. failed = mock_task('ts11', states.FAILURE, KeyError('Baz'))
  649. save_result(self.app, failed)
  650. failed_res = self.app.AsyncResult(failed['id'])
  651. self.ts = self.app.GroupResult(uuid(), results + [failed_res])
  652. def test_completed_count(self):
  653. assert self.ts.completed_count() == len(self.ts) - 1
  654. def test_iterate_simple(self):
  655. with pytest.warns(CPendingDeprecationWarning):
  656. it = self.ts.iterate()
  657. def consume():
  658. return list(it)
  659. with pytest.raises(KeyError):
  660. consume()
  661. def test_join(self):
  662. with pytest.raises(KeyError):
  663. self.ts.join()
  664. def test_successful(self):
  665. assert not self.ts.successful()
  666. def test_failed(self):
  667. assert self.ts.failed()
  668. class test_pending_Group:
  669. def setup(self):
  670. self.ts = self.app.GroupResult(
  671. uuid(), [self.app.AsyncResult(uuid()),
  672. self.app.AsyncResult(uuid())])
  673. def test_completed_count(self):
  674. assert self.ts.completed_count() == 0
  675. def test_ready(self):
  676. assert not self.ts.ready()
  677. def test_waiting(self):
  678. assert self.ts.waiting()
  679. def test_join(self):
  680. with pytest.raises(TimeoutError):
  681. self.ts.join(timeout=0.001)
  682. def test_join_longer(self):
  683. with pytest.raises(TimeoutError):
  684. self.ts.join(timeout=1)
  685. class test_EagerResult:
  686. def setup(self):
  687. @self.app.task(shared=False)
  688. def raising(x, y):
  689. raise KeyError(x, y)
  690. self.raising = raising
  691. def test_wait_raises(self):
  692. res = self.raising.apply(args=[3, 3])
  693. with pytest.raises(KeyError):
  694. res.wait()
  695. assert res.wait(propagate=False)
  696. def test_wait(self):
  697. res = EagerResult('x', 'x', states.RETRY)
  698. res.wait()
  699. assert res.state == states.RETRY
  700. assert res.status == states.RETRY
  701. def test_forget(self):
  702. res = EagerResult('x', 'x', states.RETRY)
  703. res.forget()
  704. def test_revoke(self):
  705. res = self.raising.apply(args=[3, 3])
  706. assert not res.revoke()
  707. class test_tuples:
  708. def test_AsyncResult(self):
  709. x = self.app.AsyncResult(uuid())
  710. assert x, result_from_tuple(x.as_tuple() == self.app)
  711. assert x, result_from_tuple(x == self.app)
  712. def test_with_parent(self):
  713. x = self.app.AsyncResult(uuid())
  714. x.parent = self.app.AsyncResult(uuid())
  715. y = result_from_tuple(x.as_tuple(), self.app)
  716. assert y == x
  717. assert y.parent == x.parent
  718. assert isinstance(y.parent, AsyncResult)
  719. def test_compat(self):
  720. uid = uuid()
  721. x = result_from_tuple([uid, []], app=self.app)
  722. assert x.id == uid
  723. def test_GroupResult(self):
  724. x = self.app.GroupResult(
  725. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  726. )
  727. assert x, result_from_tuple(x.as_tuple() == self.app)
  728. assert x, result_from_tuple(x == self.app)