test_task_sets.py 5.1 KB

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