test_result.py 24 KB

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