test_canvas.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import pytest
  2. from celery import chain, chord, group
  3. from celery.exceptions import TimeoutError
  4. from celery.result import AsyncResult, GroupResult
  5. from .conftest import flaky
  6. from .tasks import add, collect_ids, ids
  7. TIMEOUT = 120
  8. class test_chain:
  9. @flaky
  10. def test_simple_chain(self, manager):
  11. c = add.s(4, 4) | add.s(8) | add.s(16)
  12. assert c().get(timeout=TIMEOUT) == 32
  13. @flaky
  14. def test_complex_chain(self, manager):
  15. c = (
  16. add.s(2, 2) | (
  17. add.s(4) | add.s(8) | add.s(16)
  18. ) |
  19. group(add.s(i) for i in range(4))
  20. )
  21. res = c()
  22. assert res.get(timeout=TIMEOUT) == [32, 33, 34, 35]
  23. @flaky
  24. def test_parent_ids(self, manager, num=10):
  25. assert manager.inspect().ping()
  26. c = chain(ids.si(i=i) for i in range(num))
  27. c.freeze()
  28. res = c()
  29. try:
  30. res.get(timeout=TIMEOUT)
  31. except TimeoutError:
  32. print(manager.inspect.active())
  33. print(manager.inspect.reserved())
  34. print(manager.inspect.stats())
  35. raise
  36. self.assert_ids(res, num - 1)
  37. def assert_ids(self, res, size):
  38. i, root = size, res
  39. while root.parent:
  40. root = root.parent
  41. node = res
  42. while node:
  43. root_id, parent_id, value = node.get(timeout=30)
  44. assert value == i
  45. if node.parent:
  46. assert parent_id == node.parent.id
  47. assert root_id == root.id
  48. node = node.parent
  49. i -= 1
  50. class test_group:
  51. @flaky
  52. def test_parent_ids(self, manager):
  53. assert manager.inspect().ping()
  54. g = (
  55. ids.si(i=1) |
  56. ids.si(i=2) |
  57. group(ids.si(i=i) for i in range(2, 50))
  58. )
  59. res = g()
  60. expected_root_id = res.parent.parent.id
  61. expected_parent_id = res.parent.id
  62. values = res.get(timeout=TIMEOUT)
  63. for i, r in enumerate(values):
  64. root_id, parent_id, value = r
  65. assert root_id == expected_root_id
  66. assert parent_id == expected_parent_id
  67. assert value == i + 2
  68. def assert_ids(r, expected_value, expected_root_id, expected_parent_id):
  69. root_id, parent_id, value = r.get(timeout=TIMEOUT)
  70. assert expected_value == value
  71. assert root_id == expected_root_id
  72. assert parent_id == expected_parent_id
  73. class test_chord:
  74. @flaky
  75. def test_parent_ids(self, manager):
  76. if not manager.app.conf.result_backend.startswith('redis'):
  77. raise pytest.skip('Requires redis result backend.')
  78. root = ids.si(i=1)
  79. expected_root_id = root.freeze().id
  80. g = chain(
  81. root, ids.si(i=2),
  82. chord(
  83. group(ids.si(i=i) for i in range(3, 50)),
  84. chain(collect_ids.s(i=50) | ids.si(i=51)),
  85. ),
  86. )
  87. self.assert_parentids_chord(g(), expected_root_id)
  88. @flaky
  89. def test_parent_ids__OR(self, manager):
  90. if not manager.app.conf.result_backend.startswith('redis'):
  91. raise pytest.skip('Requires redis result backend.')
  92. root = ids.si(i=1)
  93. expected_root_id = root.freeze().id
  94. g = (
  95. root |
  96. ids.si(i=2) |
  97. group(ids.si(i=i) for i in range(3, 50)) |
  98. collect_ids.s(i=50) |
  99. ids.si(i=51)
  100. )
  101. self.assert_parentids_chord(g(), expected_root_id)
  102. def assert_parentids_chord(self, res, expected_root_id):
  103. assert isinstance(res, AsyncResult)
  104. assert isinstance(res.parent, AsyncResult)
  105. assert isinstance(res.parent.parent, GroupResult)
  106. assert isinstance(res.parent.parent.parent, AsyncResult)
  107. assert isinstance(res.parent.parent.parent.parent, AsyncResult)
  108. # first we check the last task
  109. assert_ids(res, 51, expected_root_id, res.parent.id)
  110. # then the chord callback
  111. prev, (root_id, parent_id, value) = res.parent.get(timeout=30)
  112. assert value == 50
  113. assert root_id == expected_root_id
  114. # started by one of the chord header tasks.
  115. assert parent_id in res.parent.parent.results
  116. # check what the chord callback recorded
  117. for i, p in enumerate(prev):
  118. root_id, parent_id, value = p
  119. assert root_id == expected_root_id
  120. assert parent_id == res.parent.parent.parent.id
  121. # ids(i=2)
  122. root_id, parent_id, value = res.parent.parent.parent.get(timeout=30)
  123. assert value == 2
  124. assert parent_id == res.parent.parent.parent.parent.id
  125. assert root_id == expected_root_id
  126. # ids(i=1)
  127. root_id, parent_id, value = res.parent.parent.parent.parent.get(
  128. timeout=30)
  129. assert value == 1
  130. assert root_id == expected_root_id
  131. assert parent_id is None