test_timer2.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import absolute_import
  2. from __future__ import with_statement
  3. import sys
  4. import time
  5. from kombu.tests.utils import redirect_stdouts
  6. from mock import Mock, patch
  7. import celery.utils.timer2 as timer2
  8. from celery.tests.utils import Case, skip_if_quick
  9. class test_Entry(Case):
  10. def test_call(self):
  11. scratch = [None]
  12. def timed(x, y, moo='foo'):
  13. scratch[0] = (x, y, moo)
  14. tref = timer2.Entry(timed, (4, 4), {'moo': 'baz'})
  15. tref()
  16. self.assertTupleEqual(scratch[0], (4, 4, 'baz'))
  17. def test_cancel(self):
  18. tref = timer2.Entry(lambda x: x, (1, ), {})
  19. tref.cancel()
  20. self.assertTrue(tref.cancelled)
  21. class test_Schedule(Case):
  22. def test_supports_Timer_interface(self):
  23. x = timer2.Schedule()
  24. x.stop()
  25. tref = Mock()
  26. x.cancel(tref)
  27. tref.cancel.assert_called_with()
  28. def test_handle_error(self):
  29. from datetime import datetime
  30. to_timestamp = timer2.to_timestamp
  31. scratch = [None]
  32. def _overflow(x):
  33. raise OverflowError(x)
  34. def on_error(exc_info):
  35. scratch[0] = exc_info
  36. s = timer2.Schedule(on_error=on_error)
  37. timer2.to_timestamp = _overflow
  38. try:
  39. s.enter(timer2.Entry(lambda: None, (), {}),
  40. eta=datetime.now())
  41. s.enter(timer2.Entry(lambda: None, (), {}),
  42. eta=None)
  43. s.on_error = None
  44. with self.assertRaises(OverflowError):
  45. s.enter(timer2.Entry(lambda: None, (), {}),
  46. eta=datetime.now())
  47. finally:
  48. timer2.to_timestamp = to_timestamp
  49. exc = scratch[0]
  50. self.assertIsInstance(exc, OverflowError)
  51. class test_Timer(Case):
  52. @skip_if_quick
  53. def test_enter_after(self):
  54. t = timer2.Timer()
  55. try:
  56. done = [False]
  57. def set_done():
  58. done[0] = True
  59. t.apply_after(300, set_done)
  60. mss = 0
  61. while not done[0]:
  62. if mss >= 2.0:
  63. raise Exception('test timed out')
  64. time.sleep(0.1)
  65. mss += 0.1
  66. finally:
  67. t.stop()
  68. def test_exit_after(self):
  69. t = timer2.Timer()
  70. t.apply_after = Mock()
  71. t.exit_after(300, priority=10)
  72. t.apply_after.assert_called_with(300, sys.exit, 10)
  73. def test_apply_interval(self):
  74. t = timer2.Timer()
  75. try:
  76. t.schedule.enter_after = Mock()
  77. myfun = Mock()
  78. myfun.__name__ = 'myfun'
  79. t.apply_interval(30, myfun)
  80. self.assertEqual(t.schedule.enter_after.call_count, 1)
  81. args1, _ = t.schedule.enter_after.call_args_list[0]
  82. msec1, tref1, _ = args1
  83. self.assertEqual(msec1, 30)
  84. tref1()
  85. self.assertEqual(t.schedule.enter_after.call_count, 2)
  86. args2, _ = t.schedule.enter_after.call_args_list[1]
  87. msec2, tref2, _ = args2
  88. self.assertEqual(msec2, 30)
  89. tref2.cancelled = True
  90. tref2()
  91. self.assertEqual(t.schedule.enter_after.call_count, 2)
  92. finally:
  93. t.stop()
  94. @patch('celery.utils.timer2.logger')
  95. def test_apply_entry_error_handled(self, logger):
  96. t = timer2.Timer()
  97. t.schedule.on_error = None
  98. fun = Mock()
  99. fun.side_effect = ValueError()
  100. t.schedule.apply_entry(fun)
  101. self.assertTrue(logger.error.called)
  102. @redirect_stdouts
  103. def test_apply_entry_error_not_handled(self, stdout, stderr):
  104. t = timer2.Timer()
  105. t.schedule.on_error = Mock()
  106. fun = Mock()
  107. fun.side_effect = ValueError()
  108. t.schedule.apply_entry(fun)
  109. fun.assert_called_with()
  110. self.assertFalse(stderr.getvalue())
  111. @patch('os._exit')
  112. def test_thread_crash(self, _exit):
  113. t = timer2.Timer()
  114. t._next_entry = Mock()
  115. t._next_entry.side_effect = OSError(131)
  116. t.run()
  117. _exit.assert_called_with(1)
  118. def test_gc_race_lost(self):
  119. t = timer2.Timer()
  120. t._is_stopped.set = Mock()
  121. t._is_stopped.set.side_effect = TypeError()
  122. t._is_shutdown.set()
  123. t.run()
  124. t._is_stopped.set.assert_called_with()
  125. def test_to_timestamp(self):
  126. self.assertIs(timer2.to_timestamp(3.13), 3.13)
  127. def test_test_enter(self):
  128. t = timer2.Timer()
  129. t._do_enter = Mock()
  130. e = Mock()
  131. t.enter(e, 13, 0)
  132. t._do_enter.assert_called_with('enter', e, 13, priority=0)
  133. def test_test_enter_after(self):
  134. t = timer2.Timer()
  135. t._do_enter = Mock()
  136. t.enter_after()
  137. t._do_enter.assert_called_with('enter_after')
  138. def test_cancel(self):
  139. t = timer2.Timer()
  140. tref = Mock()
  141. t.cancel(tref)
  142. tref.cancel.assert_called_with()