test_test_utils.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from tests import unittest
  15. from tests.utils import MockClock
  16. class MockClockTestCase(unittest.TestCase):
  17. def setUp(self):
  18. self.clock = MockClock()
  19. def test_advance_time(self):
  20. start_time = self.clock.time()
  21. self.clock.advance_time(20)
  22. self.assertEqual(20, self.clock.time() - start_time)
  23. def test_later(self):
  24. invoked = [0, 0]
  25. def _cb0():
  26. invoked[0] = 1
  27. self.clock.call_later(10, _cb0)
  28. def _cb1():
  29. invoked[1] = 1
  30. self.clock.call_later(20, _cb1)
  31. self.assertFalse(invoked[0])
  32. self.clock.advance_time(15)
  33. self.assertTrue(invoked[0])
  34. self.assertFalse(invoked[1])
  35. self.clock.advance_time(5)
  36. self.assertTrue(invoked[1])
  37. def test_cancel_later(self):
  38. invoked = [0, 0]
  39. def _cb0():
  40. invoked[0] = 1
  41. t0 = self.clock.call_later(10, _cb0)
  42. def _cb1():
  43. invoked[1] = 1
  44. self.clock.call_later(20, _cb1)
  45. self.clock.cancel_call_later(t0)
  46. self.clock.advance_time(30)
  47. self.assertFalse(invoked[0])
  48. self.assertTrue(invoked[1])