test_test_utils.py 1.7 KB

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