test_timer2.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import time
  2. from celery.tests.utils import unittest
  3. import celery.utils.timer2 as timer2
  4. from celery.tests.utils import skip_if_quick
  5. class test_Entry(unittest.TestCase):
  6. def test_call(self):
  7. scratch = [None]
  8. def timed(x, y, moo="foo"):
  9. scratch[0] = (x, y, moo)
  10. tref = timer2.Entry(timed, (4, 4), {"moo": "baz"})
  11. tref()
  12. self.assertTupleEqual(scratch[0], (4, 4, "baz"))
  13. def test_cancel(self):
  14. tref = timer2.Entry(lambda x: x, (1, ), {})
  15. tref.cancel()
  16. self.assertTrue(tref.cancelled)
  17. class test_Schedule(unittest.TestCase):
  18. def test_handle_error(self):
  19. from datetime import datetime
  20. mktime = timer2.mktime
  21. scratch = [None]
  22. def _overflow(x):
  23. raise OverflowError(x)
  24. def on_error(exc_info):
  25. scratch[0] = exc_info
  26. s = timer2.Schedule(on_error=on_error)
  27. timer2.mktime = _overflow
  28. try:
  29. s.enter(timer2.Entry(lambda: None, (), {}),
  30. eta=datetime.now())
  31. finally:
  32. timer2.mktime = mktime
  33. _, exc, _ = scratch[0]
  34. self.assertIsInstance(exc, OverflowError)
  35. class test_Timer(unittest.TestCase):
  36. @skip_if_quick
  37. def test_enter_after(self):
  38. t = timer2.Timer()
  39. done = [False]
  40. def set_done():
  41. done[0] = True
  42. try:
  43. t.apply_after(300, set_done)
  44. while not done[0]:
  45. time.sleep(0.1)
  46. finally:
  47. t.stop()