test_result.py 23 KB

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