1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
Description: Use a fake system clock for unit tests.
.
This removes a non-deterministic dependency on the real system clock.
Bug-Debian: http://bugs.debian.org/854539
Author: Ben Finney <bignose@debian.org>
Last-Update: 2017-02-18
diff --git i/irc/tests/test_client.py w/irc/tests/test_client.py
index 1e2a8ddb..fd6fb9bf 100644
--- i/irc/tests/test_client.py
+++ w/irc/tests/test_client.py
@@ -44,6 +44,44 @@ class TestHandlers(object):
assert not handler1 < handler2
assert not handler2 < handler1
+
+class FakeClock(object):
+ """ A fake clock that is under control of test cases. """
+
+ _default_initial_seconds = 1450000000.0
+ _default_tick_duration = 0.0005
+
+ def __init__(
+ self,
+ seconds=_default_initial_seconds,
+ tick_duration=_default_tick_duration,
+ ):
+ self._tick_duration = tick_duration
+ self.reset(seconds)
+
+ def reset(self, seconds):
+ """ Reset the clock time to `seconds`. """
+ self._seconds = seconds
+
+ def advance(self, seconds):
+ """ Advance the clock by `seconds`. """
+ self._seconds += max(0, seconds)
+
+ def tick(self):
+ """ Advance the clock by its tick duration. """
+ self.advance(self._tick_duration)
+
+ def time(self):
+ """ Get the current time, as seconds since epoch. """
+ self.tick()
+ return self._seconds
+
+
+fake_clock = FakeClock()
+
+
+@mock.patch.object(time, 'time', new=fake_clock.time)
+@mock.patch.object(time, 'sleep', new=fake_clock.advance)
class TestThrottler(object):
def test_function_throttled(self):
"""
|