test_result.py 25 KB

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