test_sets.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import absolute_import
  2. from __future__ import with_statement
  3. import anyjson
  4. from celery import current_app
  5. from celery.task import Task
  6. from celery.task.sets import subtask, TaskSet
  7. from celery.canvas import Signature
  8. from celery.tests.utils import Case
  9. class MockTask(Task):
  10. name = "tasks.add"
  11. def run(self, x, y, **kwargs):
  12. return x + y
  13. @classmethod
  14. def apply_async(cls, args, kwargs, **options):
  15. return (args, kwargs, options)
  16. @classmethod
  17. def apply(cls, args, kwargs, **options):
  18. return (args, kwargs, options)
  19. class test_subtask(Case):
  20. def test_behaves_like_type(self):
  21. s = subtask("tasks.add", (2, 2), {"cache": True},
  22. {"routing_key": "CPU-bound"})
  23. self.assertDictEqual(subtask(s), s)
  24. def test_task_argument_can_be_task_cls(self):
  25. s = subtask(MockTask, (2, 2))
  26. self.assertEqual(s.task, MockTask.name)
  27. def test_apply_async(self):
  28. s = MockTask.subtask((2, 2), {"cache": True},
  29. {"routing_key": "CPU-bound"})
  30. args, kwargs, options = s.apply_async()
  31. self.assertTupleEqual(args, (2, 2))
  32. self.assertDictEqual(kwargs, {"cache": True})
  33. self.assertDictEqual(options, {"routing_key": "CPU-bound"})
  34. def test_delay_argmerge(self):
  35. s = MockTask.subtask((2, ), {"cache": True},
  36. {"routing_key": "CPU-bound"})
  37. args, kwargs, options = s.delay(10, cache=False, other="foo")
  38. self.assertTupleEqual(args, (10, 2))
  39. self.assertDictEqual(kwargs, {"cache": False, "other": "foo"})
  40. self.assertDictEqual(options, {"routing_key": "CPU-bound"})
  41. def test_apply_async_argmerge(self):
  42. s = MockTask.subtask((2, ), {"cache": True},
  43. {"routing_key": "CPU-bound"})
  44. args, kwargs, options = s.apply_async((10, ),
  45. {"cache": False, "other": "foo"},
  46. routing_key="IO-bound",
  47. exchange="fast")
  48. self.assertTupleEqual(args, (10, 2))
  49. self.assertDictEqual(kwargs, {"cache": False, "other": "foo"})
  50. self.assertDictEqual(options, {"routing_key": "IO-bound",
  51. "exchange": "fast"})
  52. def test_apply_argmerge(self):
  53. s = MockTask.subtask((2, ), {"cache": True},
  54. {"routing_key": "CPU-bound"})
  55. args, kwargs, options = s.apply((10, ),
  56. {"cache": False, "other": "foo"},
  57. routing_key="IO-bound",
  58. exchange="fast")
  59. self.assertTupleEqual(args, (10, 2))
  60. self.assertDictEqual(kwargs, {"cache": False, "other": "foo"})
  61. self.assertDictEqual(options, {"routing_key": "IO-bound",
  62. "exchange": "fast"})
  63. def test_is_JSON_serializable(self):
  64. s = MockTask.subtask((2, ), {"cache": True},
  65. {"routing_key": "CPU-bound"})
  66. s.args = list(s.args) # tuples are not preserved
  67. # but this doesn't matter.
  68. self.assertEqual(s, subtask(anyjson.loads(anyjson.dumps(s))))
  69. def test_repr(self):
  70. s = MockTask.subtask((2, ), {"cache": True})
  71. self.assertIn("2", repr(s))
  72. self.assertIn("cache=True", repr(s))
  73. def test_reduce(self):
  74. s = MockTask.subtask((2, ), {"cache": True})
  75. cls, args = s.__reduce__()
  76. self.assertDictEqual(dict(cls(*args)), dict(s))
  77. class test_TaskSet(Case):
  78. def test_task_arg_can_be_iterable__compat(self):
  79. ts = TaskSet([MockTask.subtask((i, i))
  80. for i in (2, 4, 8)])
  81. self.assertEqual(len(ts), 3)
  82. def test_respects_ALWAYS_EAGER(self):
  83. app = current_app
  84. class MockTaskSet(TaskSet):
  85. applied = 0
  86. def apply(self, *args, **kwargs):
  87. self.applied += 1
  88. ts = MockTaskSet([MockTask.subtask((i, i))
  89. for i in (2, 4, 8)])
  90. app.conf.CELERY_ALWAYS_EAGER = True
  91. try:
  92. ts.apply_async()
  93. finally:
  94. app.conf.CELERY_ALWAYS_EAGER = False
  95. self.assertEqual(ts.applied, 1)
  96. def test_apply_async(self):
  97. applied = [0]
  98. class mocksubtask(Signature):
  99. def apply_async(self, *args, **kwargs):
  100. applied[0] += 1
  101. ts = TaskSet([mocksubtask(MockTask, (i, i))
  102. for i in (2, 4, 8)])
  103. ts.apply_async()
  104. self.assertEqual(applied[0], 3)
  105. class Publisher(object):
  106. def send(self, *args, **kwargs):
  107. pass
  108. ts.apply_async(publisher=Publisher())
  109. # setting current_task
  110. @current_app.task
  111. def xyz():
  112. pass
  113. from celery.state import _task_stack
  114. _task_stack.push(xyz)
  115. try:
  116. ts.apply_async(publisher=Publisher())
  117. finally:
  118. _task_stack.pop()
  119. xyz.pop_request()
  120. def test_apply(self):
  121. applied = [0]
  122. class mocksubtask(Signature):
  123. def apply(self, *args, **kwargs):
  124. applied[0] += 1
  125. ts = TaskSet([mocksubtask(MockTask, (i, i))
  126. for i in (2, 4, 8)])
  127. ts.apply()
  128. self.assertEqual(applied[0], 3)
  129. def test_set_app(self):
  130. ts = TaskSet([])
  131. ts.app = 42
  132. self.assertEqual(ts.app, 42)
  133. def test_set_tasks(self):
  134. ts = TaskSet([])
  135. ts.tasks = [1, 2, 3]
  136. self.assertEqual(ts.data, [1, 2, 3])
  137. def test_set_Publisher(self):
  138. ts = TaskSet([])
  139. ts.Publisher = 42
  140. self.assertEqual(ts.Publisher, 42)