test_tornado.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # coding: utf-8
  2. # Copyright 2017 Elisey Zanko
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain 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,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import unittest
  16. from tenacity import RetryError, retry, stop_after_attempt
  17. from tenacity import tornadoweb
  18. from tenacity.tests.test_tenacity import NoIOErrorAfterCount
  19. from tornado import gen
  20. from tornado import testing
  21. @retry
  22. @gen.coroutine
  23. def _retryable_coroutine(thing):
  24. yield gen.sleep(0.00001)
  25. thing.go()
  26. @retry(stop=stop_after_attempt(2))
  27. @gen.coroutine
  28. def _retryable_coroutine_with_2_attempts(thing):
  29. yield gen.sleep(0.00001)
  30. thing.go()
  31. class TestTornado(testing.AsyncTestCase):
  32. @testing.gen_test
  33. def test_retry(self):
  34. assert gen.is_coroutine_function(_retryable_coroutine)
  35. thing = NoIOErrorAfterCount(5)
  36. yield _retryable_coroutine(thing)
  37. assert thing.counter == thing.count
  38. @testing.gen_test
  39. def test_stop_after_attempt(self):
  40. assert gen.is_coroutine_function(_retryable_coroutine)
  41. thing = NoIOErrorAfterCount(2)
  42. try:
  43. yield _retryable_coroutine_with_2_attempts(thing)
  44. except RetryError:
  45. assert thing.counter == 2
  46. def test_repr(self):
  47. repr(tornadoweb.TornadoRetrying())
  48. def test_old_tornado(self):
  49. old_attr = gen.is_coroutine_function
  50. try:
  51. del gen.is_coroutine_function
  52. # is_coroutine_function was introduced in tornado 4.5;
  53. # verify that we don't *completely* fall over on old versions
  54. @retry
  55. def retryable(thing):
  56. pass
  57. finally:
  58. gen.is_coroutine_function = old_attr
  59. if __name__ == '__main__':
  60. unittest.main()