OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 """ | |
5 Helper class to writing deterministic time-based unit tests. | |
6 | |
7 Do not use this module. It is a lie. See L{twisted.internet.task.Clock} | |
8 instead. | |
9 """ | |
10 | |
11 class Clock(object): | |
12 """ | |
13 A utility for monkey-patches various parts of Twisted to use a | |
14 simulated timing mechanism. DO NOT use this class. Use | |
15 L{twisted.internet.task.Clock}. | |
16 """ | |
17 rightNow = 0.0 | |
18 | |
19 def __call__(self): | |
20 """ | |
21 Return the current simulated time. | |
22 """ | |
23 return self.rightNow | |
24 | |
25 def install(self): | |
26 """ | |
27 Monkeypatch L{twisted.internet.reactor.seconds} to use | |
28 L{__call__} as a time source | |
29 """ | |
30 # Violation is fun. | |
31 from twisted.internet import reactor | |
32 self.reactor_original = reactor.seconds | |
33 reactor.seconds = self | |
34 | |
35 def uninstall(self): | |
36 """ | |
37 Remove the monkeypatching of L{twisted.internet.reactor.seconds}. | |
38 """ | |
39 from twisted.internet import reactor | |
40 reactor.seconds = self.reactor_original | |
41 | |
42 def adjust(self, amount): | |
43 """ | |
44 Adjust the current simulated time upward by the given C{amount}. | |
45 | |
46 Note that this does not cause any scheduled calls to be run. | |
47 """ | |
48 self.rightNow += amount | |
49 | |
50 def pump(self, reactor, timings): | |
51 """ | |
52 Iterate the given C{reactor} with increments of time specified | |
53 by C{timings}. | |
54 | |
55 For each timing, the simulated time will be L{adjust}ed and | |
56 the reactor will be iterated twice. | |
57 """ | |
58 timings = list(timings) | |
59 timings.reverse() | |
60 self.adjust(timings.pop()) | |
61 while timings: | |
62 self.adjust(timings.pop()) | |
63 reactor.iterate() | |
64 reactor.iterate() | |
65 | |
OLD | NEW |