test_timer2.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import sys
  2. import time
  3. from case import Mock, patch, call
  4. import celery.utils.timer2 as timer2
  5. class test_Timer:
  6. def test_enter_after(self):
  7. t = timer2.Timer()
  8. try:
  9. done = [False]
  10. def set_done():
  11. done[0] = True
  12. t.call_after(0.3, set_done)
  13. mss = 0
  14. while not done[0]:
  15. if mss >= 2.0:
  16. raise Exception('test timed out')
  17. time.sleep(0.1)
  18. mss += 0.1
  19. finally:
  20. t.stop()
  21. def test_exit_after(self):
  22. t = timer2.Timer()
  23. t.call_after = Mock()
  24. t.exit_after(0.3, priority=10)
  25. t.call_after.assert_called_with(0.3, sys.exit, 10)
  26. def test_ensure_started_not_started(self):
  27. t = timer2.Timer()
  28. t.running = True
  29. t.start = Mock()
  30. t.ensure_started()
  31. t.start.assert_not_called()
  32. t.running = False
  33. t.on_start = Mock()
  34. t.ensure_started()
  35. t.on_start.assert_called_with(t)
  36. t.start.assert_called_with()
  37. @patch('celery.utils.timer2.sleep')
  38. def test_on_tick(self, sleep):
  39. on_tick = Mock(name='on_tick')
  40. t = timer2.Timer(on_tick=on_tick)
  41. ne = t._next_entry = Mock(name='_next_entry')
  42. ne.return_value = 3.33
  43. ne.on_nth_call_do(t._is_shutdown.set, 3)
  44. t.run()
  45. sleep.assert_called_with(3.33)
  46. on_tick.assert_has_calls([call(3.33), call(3.33), call(3.33)])
  47. @patch('os._exit')
  48. def test_thread_crash(self, _exit):
  49. t = timer2.Timer()
  50. t._next_entry = Mock()
  51. t._next_entry.side_effect = OSError(131)
  52. t.run()
  53. _exit.assert_called_with(1)
  54. def test_gc_race_lost(self):
  55. t = timer2.Timer()
  56. t._is_stopped.set = Mock()
  57. t._is_stopped.set.side_effect = TypeError()
  58. t._is_shutdown.set()
  59. t.run()
  60. t._is_stopped.set.assert_called_with()
  61. def test_test_enter(self):
  62. t = timer2.Timer()
  63. t._do_enter = Mock()
  64. e = Mock()
  65. t.enter(e, 13, 0)
  66. t._do_enter.assert_called_with('enter_at', e, 13, priority=0)
  67. def test_test_enter_after(self):
  68. t = timer2.Timer()
  69. t._do_enter = Mock()
  70. t.enter_after()
  71. t._do_enter.assert_called_with('enter_after')
  72. def test_cancel(self):
  73. t = timer2.Timer()
  74. tref = Mock()
  75. t.cancel(tref)
  76. tref.cancel.assert_called_with()