test_timer2.py 5.1 KB

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