test_timer2.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import time
  2. import unittest2 as 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. import time
  20. from datetime import datetime
  21. mktime = timer2.mktime
  22. scratch = [None]
  23. def _overflow(x):
  24. raise OverflowError(x)
  25. def on_error(exc_info):
  26. scratch[0] = exc_info
  27. s = timer2.Schedule(on_error=on_error)
  28. timer2.mktime = _overflow
  29. try:
  30. s.enter(timer2.Entry(lambda: None, (), {}),
  31. eta=datetime.now())
  32. finally:
  33. timer2.mktime = mktime
  34. _, exc, _ = scratch[0]
  35. self.assertIsInstance(exc, OverflowError)
  36. class test_Timer(unittest.TestCase):
  37. @skip_if_quick
  38. def test_enter_after(self):
  39. t = timer2.Timer()
  40. done = [False]
  41. def set_done():
  42. done[0] = True
  43. try:
  44. t.apply_after(300, set_done)
  45. while not done[0]:
  46. time.sleep(0.1)
  47. finally:
  48. t.stop()