twisted_test.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. import sys
  16. import unittest
  17. from tornado.testing import AsyncTestCase, gen_test
  18. try:
  19. from twisted.internet.defer import inlineCallbacks # type: ignore
  20. have_twisted = True
  21. except ImportError:
  22. have_twisted = False
  23. except Exception:
  24. # Twisted is currently incompatible with the first 3.14 alpha release; disable this
  25. # test until the beta when it will hopefully be fixed (note that this requires us to
  26. # update our requirements.txt to pick up a new version of twisted).
  27. if sys.version_info[:2] == (3, 14) and sys.version_info.releaselevel == "alpha":
  28. have_twisted = False
  29. else:
  30. raise
  31. else:
  32. # Not used directly but needed for `yield deferred` to work.
  33. import tornado.platform.twisted # noqa: F401
  34. skipIfNoTwisted = unittest.skipUnless(have_twisted, "twisted module not present")
  35. @skipIfNoTwisted
  36. class ConvertDeferredTest(AsyncTestCase):
  37. @gen_test
  38. def test_success(self):
  39. @inlineCallbacks
  40. def fn():
  41. if False:
  42. # inlineCallbacks doesn't work with regular functions;
  43. # must have a yield even if it's unreachable.
  44. yield
  45. return 42
  46. res = yield fn()
  47. self.assertEqual(res, 42)
  48. @gen_test
  49. def test_failure(self):
  50. @inlineCallbacks
  51. def fn():
  52. if False:
  53. yield
  54. 1 / 0
  55. with self.assertRaises(ZeroDivisionError):
  56. yield fn()
  57. if __name__ == "__main__":
  58. unittest.main()