test_bootsteps.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from __future__ import absolute_import
  2. from mock import Mock
  3. from celery import bootsteps
  4. from celery.tests.utils import AppCase, Case
  5. class test_Step(Case):
  6. class Def(bootsteps.Step):
  7. name = 'test_Step.Def'
  8. def test_namespace_name(self, ns='test_namespace_name'):
  9. class X(bootsteps.Step):
  10. namespace = ns
  11. name = 'X'
  12. self.assertEqual(X.name, 'X')
  13. class Y(bootsteps.Step):
  14. name = '%s.Y' % ns
  15. self.assertEqual(Y.name, '%s.Y' % ns)
  16. def test_init(self):
  17. self.assertTrue(self.Def(self))
  18. def test_create(self):
  19. self.Def(self).create(self)
  20. def test_include_if(self):
  21. x = self.Def(self)
  22. x.enabled = True
  23. self.assertTrue(x.include_if(self))
  24. x.enabled = False
  25. self.assertFalse(x.include_if(self))
  26. def test_instantiate(self):
  27. self.assertIsInstance(self.Def(self).instantiate(self.Def, self),
  28. self.Def)
  29. def test_include_when_enabled(self):
  30. x = self.Def(self)
  31. x.create = Mock()
  32. x.create.return_value = 'George'
  33. self.assertTrue(x.include(self))
  34. self.assertEqual(x.obj, 'George')
  35. x.create.assert_called_with(self)
  36. def test_include_when_disabled(self):
  37. x = self.Def(self)
  38. x.enabled = False
  39. x.create = Mock()
  40. self.assertFalse(x.include(self))
  41. self.assertFalse(x.create.call_count)
  42. class test_StartStopStep(Case):
  43. class Def(bootsteps.StartStopStep):
  44. name = 'test_StartStopStep.Def'
  45. def setUp(self):
  46. self.steps = []
  47. def test_start__stop(self):
  48. x = self.Def(self)
  49. x.create = Mock()
  50. # include creates the underlying object and sets
  51. # its x.obj attribute to it, as well as appending
  52. # it to the parent.steps list.
  53. x.include(self)
  54. self.assertTrue(self.steps)
  55. self.assertIs(self.steps[0], x)
  56. x.start(self)
  57. x.obj.start.assert_called_with()
  58. x.stop(self)
  59. x.obj.stop.assert_called_with()
  60. def test_include_when_disabled(self):
  61. x = self.Def(self)
  62. x.enabled = False
  63. x.include(self)
  64. self.assertFalse(self.steps)
  65. def test_terminate(self):
  66. x = self.Def(self)
  67. x.terminable = False
  68. x.create = Mock()
  69. x.include(self)
  70. x.terminate(self)
  71. x.obj.stop.assert_called_with()
  72. class test_Namespace(AppCase):
  73. class NS(bootsteps.Namespace):
  74. name = 'test_Namespace'
  75. def test_steps_added_to_unclaimed(self):
  76. class tnA(bootsteps.Step):
  77. name = 'test_Namespace.A'
  78. class tnB(bootsteps.Step):
  79. name = 'test_Namespace.B'
  80. class xxA(bootsteps.Step):
  81. name = 'xx.A'
  82. class NS(self.NS):
  83. default_steps = [tnA, tnB]
  84. ns = NS(app=self.app)
  85. self.assertIn(tnA, ns._all_steps())
  86. self.assertIn(tnB, ns._all_steps())
  87. self.assertNotIn(xxA, ns._all_steps())
  88. def test_init(self):
  89. ns = self.NS(app=self.app)
  90. self.assertIs(ns.app, self.app)
  91. self.assertEqual(ns.name, 'test_Namespace')
  92. def test_apply(self):
  93. class MyNS(bootsteps.Namespace):
  94. name = 'test_apply'
  95. def modules(self):
  96. return ['A', 'B']
  97. class B(bootsteps.Step):
  98. name = 'test_apply.B'
  99. class C(bootsteps.Step):
  100. name = 'test_apply.C'
  101. requires = [B]
  102. class A(bootsteps.Step):
  103. name = 'test_apply.A'
  104. requires = [C]
  105. class D(bootsteps.Step):
  106. name = 'test_apply.D'
  107. last = True
  108. x = MyNS([A, D], app=self.app)
  109. x.apply(self)
  110. self.assertIsInstance(x.order[0], B)
  111. self.assertIsInstance(x.order[1], C)
  112. self.assertIsInstance(x.order[2], A)
  113. self.assertIsInstance(x.order[3], D)
  114. self.assertIn(A, x.types)
  115. self.assertIs(x[A.name], x.order[2])
  116. def test_find_last_but_no_steps(self):
  117. class MyNS(bootsteps.Namespace):
  118. name = 'qwejwioqjewoqiej'
  119. x = MyNS(app=self.app)
  120. x.apply(self)
  121. self.assertIsNone(x._find_last())