test_result.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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 {
  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. def test_eq_with_parent(self):
  493. # GroupResult instances with different .parent are not equal
  494. grp_res = self.app.GroupResult(
  495. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  496. parent=self.app.AsyncResult(uuid())
  497. )
  498. grp_res_2 = self.app.GroupResult(grp_res.id, grp_res.results)
  499. assert grp_res != grp_res_2
  500. grp_res_2.parent = self.app.AsyncResult(uuid())
  501. assert grp_res != grp_res_2
  502. grp_res_2.parent = grp_res.parent
  503. assert grp_res == grp_res_2
  504. @pytest.mark.usefixtures('depends_on_current_app')
  505. def test_pickleable(self):
  506. assert pickle.loads(pickle.dumps(self.ts))
  507. def test_iterate_raises(self):
  508. ar = MockAsyncResultFailure(uuid(), app=self.app)
  509. ts = self.app.GroupResult(uuid(), [ar])
  510. with pytest.warns(CPendingDeprecationWarning):
  511. it = ts.iterate()
  512. with pytest.raises(KeyError):
  513. next(it)
  514. def test_forget(self):
  515. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  516. MockAsyncResultSuccess(uuid(), app=self.app)]
  517. ts = self.app.GroupResult(uuid(), subs)
  518. ts.forget()
  519. for sub in subs:
  520. assert sub.forgotten
  521. def test_getitem(self):
  522. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  523. MockAsyncResultSuccess(uuid(), app=self.app)]
  524. ts = self.app.GroupResult(uuid(), subs)
  525. assert ts[0] is subs[0]
  526. def test_save_restore(self):
  527. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  528. MockAsyncResultSuccess(uuid(), app=self.app)]
  529. ts = self.app.GroupResult(uuid(), subs)
  530. ts.save()
  531. with pytest.raises(AttributeError):
  532. ts.save(backend=object())
  533. assert self.app.GroupResult.restore(ts.id).results == ts.results
  534. ts.delete()
  535. assert self.app.GroupResult.restore(ts.id) is None
  536. with pytest.raises(AttributeError):
  537. self.app.GroupResult.restore(ts.id, backend=object())
  538. def test_restore_app(self):
  539. subs = [MockAsyncResultSuccess(uuid(), app=self.app)]
  540. ts = self.app.GroupResult(uuid(), subs)
  541. ts.save()
  542. restored = GroupResult.restore(ts.id, app=self.app)
  543. assert restored.id == ts.id
  544. def test_join_native(self):
  545. backend = SimpleBackend()
  546. results = [self.app.AsyncResult(uuid(), backend=backend)
  547. for i in range(10)]
  548. ts = self.app.GroupResult(uuid(), results)
  549. ts.app.backend = backend
  550. backend.ids = [result.id for result in results]
  551. res = ts.join_native()
  552. assert res == list(range(10))
  553. callback = Mock(name='callback')
  554. assert not ts.join_native(callback=callback)
  555. callback.assert_has_calls([
  556. call(r.id, i) for i, r in enumerate(ts.results)
  557. ])
  558. def test_join_native_raises(self):
  559. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  560. ts.iter_native = Mock()
  561. ts.iter_native.return_value = iter([
  562. (uuid(), {'status': states.FAILURE, 'result': KeyError()})
  563. ])
  564. with pytest.raises(KeyError):
  565. ts.join_native(propagate=True)
  566. def test_failed_join_report(self):
  567. res = Mock()
  568. ts = self.app.GroupResult(uuid(), [res])
  569. res.state = states.FAILURE
  570. res.backend.is_cached.return_value = True
  571. assert next(ts._failed_join_report()) is res
  572. res.backend.is_cached.return_value = False
  573. with pytest.raises(StopIteration):
  574. next(ts._failed_join_report())
  575. def test_repr(self):
  576. assert repr(
  577. self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())]))
  578. def test_children_is_results(self):
  579. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  580. assert ts.children is ts.results
  581. def test_iter_native(self):
  582. backend = SimpleBackend()
  583. results = [self.app.AsyncResult(uuid(), backend=backend)
  584. for i in range(10)]
  585. ts = self.app.GroupResult(uuid(), results)
  586. ts.app.backend = backend
  587. backend.ids = [result.id for result in results]
  588. assert len(list(ts.iter_native())) == 10
  589. def test_iterate_yields(self):
  590. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  591. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  592. ts = self.app.GroupResult(uuid(), [ar, ar2])
  593. with pytest.warns(CPendingDeprecationWarning):
  594. it = ts.iterate()
  595. assert next(it) == 42
  596. assert next(it) == 42
  597. def test_iterate_eager(self):
  598. ar1 = EagerResult(uuid(), 42, states.SUCCESS)
  599. ar2 = EagerResult(uuid(), 42, states.SUCCESS)
  600. ts = self.app.GroupResult(uuid(), [ar1, ar2])
  601. with pytest.warns(CPendingDeprecationWarning):
  602. it = ts.iterate()
  603. assert next(it) == 42
  604. assert next(it) == 42
  605. def test_join_timeout(self):
  606. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  607. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  608. ar3 = self.app.AsyncResult(uuid())
  609. ts = self.app.GroupResult(uuid(), [ar, ar2, ar3])
  610. with pytest.raises(TimeoutError):
  611. ts.join(timeout=0.0000001)
  612. ar4 = self.app.AsyncResult(uuid())
  613. ar4.get = Mock()
  614. ts2 = self.app.GroupResult(uuid(), [ar4])
  615. assert ts2.join(timeout=0.1)
  616. callback = Mock(name='callback')
  617. assert not ts2.join(timeout=0.1, callback=callback)
  618. callback.assert_called_with(ar4.id, ar4.get())
  619. def test_iter_native_when_empty_group(self):
  620. ts = self.app.GroupResult(uuid(), [])
  621. assert list(ts.iter_native()) == []
  622. def test_iterate_simple(self):
  623. with pytest.warns(CPendingDeprecationWarning):
  624. it = self.ts.iterate()
  625. results = sorted(list(it))
  626. assert results == list(range(self.size))
  627. def test___iter__(self):
  628. assert list(iter(self.ts)) == self.ts.results
  629. def test_join(self):
  630. joined = self.ts.join()
  631. assert joined == list(range(self.size))
  632. def test_successful(self):
  633. assert self.ts.successful()
  634. def test_failed(self):
  635. assert not self.ts.failed()
  636. def test_maybe_throw(self):
  637. self.ts.results = [Mock(name='r1')]
  638. self.ts.maybe_throw()
  639. self.ts.results[0].maybe_throw.assert_called_with(
  640. callback=None, propagate=True,
  641. )
  642. def test_join__on_message(self):
  643. with pytest.raises(ImproperlyConfigured):
  644. self.ts.join(on_message=Mock())
  645. def test_waiting(self):
  646. assert not self.ts.waiting()
  647. def test_ready(self):
  648. assert self.ts.ready()
  649. def test_completed_count(self):
  650. assert self.ts.completed_count() == len(self.ts)
  651. class test_pending_AsyncResult:
  652. def test_result(self, app):
  653. res = app.AsyncResult(uuid())
  654. assert res.result is None
  655. class test_failed_AsyncResult:
  656. def setup(self):
  657. self.size = 11
  658. self.app.conf.result_serializer = 'pickle'
  659. results = make_mock_group(self.app, 10)
  660. failed = mock_task('ts11', states.FAILURE, KeyError('Baz'))
  661. save_result(self.app, failed)
  662. failed_res = self.app.AsyncResult(failed['id'])
  663. self.ts = self.app.GroupResult(uuid(), results + [failed_res])
  664. def test_completed_count(self):
  665. assert self.ts.completed_count() == len(self.ts) - 1
  666. def test_iterate_simple(self):
  667. with pytest.warns(CPendingDeprecationWarning):
  668. it = self.ts.iterate()
  669. def consume():
  670. return list(it)
  671. with pytest.raises(KeyError):
  672. consume()
  673. def test_join(self):
  674. with pytest.raises(KeyError):
  675. self.ts.join()
  676. def test_successful(self):
  677. assert not self.ts.successful()
  678. def test_failed(self):
  679. assert self.ts.failed()
  680. class test_pending_Group:
  681. def setup(self):
  682. self.ts = self.app.GroupResult(
  683. uuid(), [self.app.AsyncResult(uuid()),
  684. self.app.AsyncResult(uuid())])
  685. def test_completed_count(self):
  686. assert self.ts.completed_count() == 0
  687. def test_ready(self):
  688. assert not self.ts.ready()
  689. def test_waiting(self):
  690. assert self.ts.waiting()
  691. def test_join(self):
  692. with pytest.raises(TimeoutError):
  693. self.ts.join(timeout=0.001)
  694. def test_join_longer(self):
  695. with pytest.raises(TimeoutError):
  696. self.ts.join(timeout=1)
  697. class test_EagerResult:
  698. def setup(self):
  699. @self.app.task(shared=False)
  700. def raising(x, y):
  701. raise KeyError(x, y)
  702. self.raising = raising
  703. def test_wait_raises(self):
  704. res = self.raising.apply(args=[3, 3])
  705. with pytest.raises(KeyError):
  706. res.wait()
  707. assert res.wait(propagate=False)
  708. def test_wait(self):
  709. res = EagerResult('x', 'x', states.RETRY)
  710. res.wait()
  711. assert res.state == states.RETRY
  712. assert res.status == states.RETRY
  713. def test_forget(self):
  714. res = EagerResult('x', 'x', states.RETRY)
  715. res.forget()
  716. def test_revoke(self):
  717. res = self.raising.apply(args=[3, 3])
  718. assert not res.revoke()
  719. class test_tuples:
  720. def test_AsyncResult(self):
  721. x = self.app.AsyncResult(uuid())
  722. assert x, result_from_tuple(x.as_tuple() == self.app)
  723. assert x, result_from_tuple(x == self.app)
  724. def test_with_parent(self):
  725. x = self.app.AsyncResult(uuid())
  726. x.parent = self.app.AsyncResult(uuid())
  727. y = result_from_tuple(x.as_tuple(), self.app)
  728. assert y == x
  729. assert y.parent == x.parent
  730. assert isinstance(y.parent, AsyncResult)
  731. def test_compat(self):
  732. uid = uuid()
  733. x = result_from_tuple([uid, []], app=self.app)
  734. assert x.id == uid
  735. def test_GroupResult(self):
  736. x = self.app.GroupResult(
  737. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  738. )
  739. assert x, result_from_tuple(x.as_tuple() == self.app)
  740. assert x, result_from_tuple(x == self.app)
  741. def test_GroupResult_with_parent(self):
  742. parent = self.app.AsyncResult(uuid())
  743. result = self.app.GroupResult(
  744. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  745. parent
  746. )
  747. second_result = result_from_tuple(result.as_tuple(), self.app)
  748. assert second_result == result
  749. assert second_result.parent == parent
  750. def test_GroupResult_as_tuple(self):
  751. parent = self.app.AsyncResult(uuid())
  752. result = self.app.GroupResult(
  753. 'group-result-1',
  754. [self.app.AsyncResult('async-result-{}'.format(i))
  755. for i in range(2)],
  756. parent
  757. )
  758. (result_id, parent_id), group_results = result.as_tuple()
  759. assert result_id == result.id
  760. assert parent_id == parent.id
  761. assert isinstance(group_results, list)
  762. expected_grp_res = [(('async-result-{}'.format(i), None), None)
  763. for i in range(2)]
  764. assert group_results == expected_grp_res