codekingpro/portable-devtools
114k
1# Author: Ovidiu Predescu
2# Date: July 2011
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import sys
17import unittest
18
19from tornado.testing import AsyncTestCase, gen_test
20
21try:
22 from twisted.internet.defer import inlineCallbacks # type: ignore
23
24 have_twisted = True
25except ImportError:
26 have_twisted = False
27except Exception:
28 # Twisted is currently incompatible with the first 3.14 alpha release; disable this
29 # test until the beta when it will hopefully be fixed (note that this requires us to
30 # update our requirements.txt to pick up a new version of twisted).
31 if sys.version_info[:2] == (3, 14) and sys.version_info.releaselevel == "alpha":
32 have_twisted = False
33 else:
34 raise
35else:
36 # Not used directly but needed for `yield deferred` to work.
37 import tornado.platform.twisted # noqa: F401
38
39skipIfNoTwisted = unittest.skipUnless(have_twisted, "twisted module not present")
40
41
42@skipIfNoTwisted
43class ConvertDeferredTest(AsyncTestCase):
44 @gen_test
45 def test_success(self):
46 @inlineCallbacks
47 def fn():
48 if False:
49 # inlineCallbacks doesn't work with regular functions;
50 # must have a yield even if it's unreachable.
51 yield
52 return 42
53
54 res = yield fn()
55 self.assertEqual(res, 42)
56
57 @gen_test
58 def test_failure(self):
59 @inlineCallbacks
60 def fn():
61 if False:
62 yield
63 1 / 0
64
65 with self.assertRaises(ZeroDivisionError):
66 yield fn()
67
68
69if __name__ == "__main__":
70 unittest.main()
71 