Ticket #2692: test2.py

File test2.py, 2.8 KB (added by yaubi, 15 years ago)

2nd test, with reactor.stop

Line 
1from twisted.internet import reactor, defer
2
3def terminate():
4    print "terminating..."
5    d = defer.Deferred()
6    # do things that long last (eg. database, network, etc.)
7    reactor.callLater(1, d.callback, None)
8    return d
9
10def stop_reactor_twice():
11    reactor.stop()
12    reactor.stop()
13
14reactor.addSystemEventTrigger("before", "shutdown", terminate)
15reactor.callWhenRunning(stop_reactor_twice)
16reactor.run()
17
18
19# Output without patch:
20# 1. 'terminate' method is called twice
21# 2. exception is not meaningful
22"""
23terminating...
24terminating...
25Unhandled error in Deferred:
26Traceback (most recent call last):
27  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/defer.py", line 317, in _runCallbacks
28    self.result = callback(self.result, *args, **kw)
29  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/defer.py", line 507, in _cbDeferred
30    self.callback(self.resultList)
31  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/defer.py", line 239, in callback
32    self._startRunCallbacks(result)
33  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/defer.py", line 304, in _startRunCallbacks
34    self._runCallbacks()
35--- <exception caught here> ---
36  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/defer.py", line 317, in _runCallbacks
37    self.result = callback(self.result, *args, **kw)
38  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/base.py", line 406, in _cbContinueSystemEvent
39    self._continueSystemEvent(eventType)
40  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/base.py", line 411, in _continueSystemEvent
41    for callList in sysEvtTriggers[1], sysEvtTriggers[2]:
42exceptions.TypeError: 'NoneType' object is unsubscriptable
43"""
44
45# Output with patch:
46# 1. 'terminate' method is called once, as expected
47# 2. exception states what's actually wrong
48"""
49Traceback (most recent call last):
50  File "test2.py", line 16, in <module>
51    reactor.run()
52  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/posixbase.py", line 221, in run
53    self.mainLoop()
54  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/posixbase.py", line 229, in mainLoop
55    self.runUntilCurrent()
56  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/base.py", line 573, in runUntilCurrent
57    call.func(*call.args, **call.kw)
58--- <exception caught here> ---
59  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/base.py", line 423, in _continueSystemEvent
60    callable(*args, **kw)
61  File "test2.py", line 12, in stop_reactor_twice
62    reactor.stop()
63  File "/home/yoann/tmp/twisted-before-shutdown/trunk/twisted/internet/base.py", line 339, in stop
64    raise error.ReactorAlreadyStopped, "don't stop reactor twice"
65twisted.internet.error.ReactorAlreadyStopped: don't stop reactor twice
66"""
67