19
class FakeDelayedCall(object):
20
def __init__(self, when, clock, what, a, kw):
29
return self.what(*self.a, **self.kw)
33
return "<FakeDelayedCall of %r>" % (self.what,)
37
self.clock.calls.remove((self.when, self))
50
def callLater(self, when, what, *a, **kw):
51
self.calls.append((self.seconds() + when, FakeDelayedCall(self.seconds() + when, self, what, a, kw)))
52
return self.calls[-1][1]
54
def adjust(self, amount):
55
self.rightNow += amount
57
def runUntilCurrent(self):
58
while self.calls and self.calls[0][0] <= self.seconds():
59
when, call = self.calls.pop(0)
62
def pump(self, timings):
63
timings = list(timings)
67
self.adjust(timings.pop())
68
self.runUntilCurrent()
72
21
class TestException(Exception):
26
class ClockTestCase(unittest.TestCase):
28
Test the non-wallclock based clock implementation.
30
def testSeconds(self):
32
Test that the L{seconds} method of the fake clock returns fake time.
35
self.assertEquals(c.seconds(), 0)
38
def testCallLater(self):
40
Test that calls can be scheduled for later with the fake clock and
41
hands back an L{IDelayedCall}.
44
call = c.callLater(1, lambda a, b: None, 1, b=2)
45
self.failUnless(interfaces.IDelayedCall.providedBy(call))
46
self.assertEquals(call.getTime(), 1)
47
self.failUnless(call.active())
50
def testCallLaterCancelled(self):
52
Test that calls can be cancelled.
55
call = c.callLater(1, lambda a, b: None, 1, b=2)
57
self.failIf(call.active())
60
def testAdvance(self):
62
Test that advancing the clock will fire some calls.
66
call = c.callLater(2, lambda: events.append(None))
68
self.assertEquals(events, [])
70
self.assertEquals(events, [None])
71
self.failIf(call.active())
74
def testAdvanceCancel(self):
76
Test attemping to cancel the call in a callback.
78
AlreadyCalled should be raised, not for example a ValueError from
79
removing the call from Clock.calls. This requires call.called to be
80
set before the callback is called.
84
self.assertRaises(error.AlreadyCalled, call.cancel)
85
call = c.callLater(1, cb)
89
def testCallLaterDelayed(self):
91
Test that calls can be delayed.
95
call = c.callLater(1, lambda a, b: events.append((a, b)), 1, b=2)
97
self.assertEquals(call.getTime(), 2)
99
self.assertEquals(events, [])
101
self.assertEquals(events, [(1, 2)])
104
def testCallLaterResetLater(self):
106
Test that calls can have their time reset to a later time.
110
call = c.callLater(2, lambda a, b: events.append((a, b)), 1, b=2)
113
self.assertEquals(call.getTime(), 4)
115
self.assertEquals(events, [])
117
self.assertEquals(events, [(1, 2)])
120
def testCallLaterResetSooner(self):
122
Test that calls can have their time reset to an earlier time.
126
call = c.callLater(4, lambda a, b: events.append((a, b)), 1, b=2)
128
self.assertEquals(call.getTime(), 3)
130
self.assertEquals(events, [(1, 2)])
77
134
class LoopTestCase(unittest.TestCase):
78
135
def testBasicFunction(self):
79
136
# Arrange to have time advanced enough so that our function is