PageRenderTime 36ms CodeModel.GetById 17ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/unit_tests/test_twisted.py

https://bitbucket.org/jpellerin/nose/
Python | 93 lines | 57 code | 14 blank | 22 comment | 1 complexity | 82f670955e64366798f9b32f13c5fd31 MD5 | raw file
 1from nose.exc import SkipTest
 2from nose.tools import *
 3from nose.twistedtools import *
 4try:    
 5    from twisted.internet.defer import Deferred
 6    from twisted.internet.error import DNSLookupError
 7except ImportError:
 8    raise SkipTest('twisted not available; skipping')
 9
10_multiprocess_ = False
11
12
13def teardown():
14    # print "stopping reactor"
15    stop_reactor()
16
17class CustomError(Exception):
18    pass
19
20# FIXME move all dns-using tests to functional
21
22# Should succeed unless google is down
23#@deferred
24def test_resolve():
25    return reactor.resolve("www.google.com")
26test_resolve = deferred()(test_resolve)
27
28# Raises TypeError because the function does not return a Deferred
29#@raises(TypeError)
30#@deferred()
31def test_raises_bad_return():
32    print reactor
33    reactor.resolve("www.python.org")
34test_raises_bad_return = raises(TypeError)(deferred()(test_raises_bad_return))
35
36# Check we propagate twisted Failures as Exceptions
37# (XXX this test might take some time: find something better?)
38#@raises(DNSLookupError)
39#@deferred()
40def test_raises_twisted_error():
41    return reactor.resolve("x.y.z")
42test_raises_twisted_error = raises(DNSLookupError)(
43    deferred()(test_raises_twisted_error))
44
45# Check we detect Exceptions inside the callback chain
46#@raises(CustomError)
47#@deferred(timeout=1.0)
48def test_raises_callback_error():
49    d = Deferred()
50    def raise_error(_):
51        raise CustomError()
52    def finish():
53        d.callback(None)
54    d.addCallback(raise_error)
55    reactor.callLater(0.01, finish)
56    return d
57test_raises_callback_error = raises(CustomError)(
58    deferred(timeout=1.0)(test_raises_callback_error))
59
60# Check we detect Exceptions inside the test body
61#@raises(CustomError)
62#@deferred(timeout=1.0)
63def test_raises_plain_error():
64    raise CustomError
65test_raises_plain_error = raises(CustomError)(
66    deferred(timeout=1.0)(test_raises_plain_error))
67
68# The deferred is triggered before the timeout: ok
69#@deferred(timeout=1.0)
70def test_timeout_ok():
71    d = Deferred()
72    def finish():
73        d.callback(None)
74    reactor.callLater(0.01, finish)
75    return d
76test_timeout_ok = deferred(timeout=1.0)(test_timeout_ok)
77
78# The deferred is triggered after the timeout: failure
79#@raises(TimeExpired)
80#@deferred(timeout=0.1)
81def test_timeout_expired():
82    d = Deferred()
83    def finish():
84        d.callback(None)
85    reactor.callLater(1.0, finish)
86    return d
87test_timeout_expired = raises(TimeExpired)(
88    deferred(timeout=0.1)(test_timeout_expired))
89
90
91if __name__ == '__main__':
92    from nose import runmodule
93    runmodule()