图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

  1. import unittest
  2. import pytest
  3. class TestRetrySleeper(unittest.TestCase):
  4. def _pass(self):
  5. pass
  6. def _fail(self, times=1):
  7. from kazoo.retry import ForceRetryError
  8. scope = dict(times=0)
  9. def inner():
  10. if scope["times"] >= times:
  11. pass
  12. else:
  13. scope["times"] += 1
  14. raise ForceRetryError("Failed!")
  15. return inner
  16. def _makeOne(self, *args, **kwargs):
  17. from kazoo.retry import KazooRetry
  18. return KazooRetry(*args, **kwargs)
  19. def test_reset(self):
  20. retry = self._makeOne(delay=0, max_tries=2)
  21. retry(self._fail())
  22. assert retry._attempts == 1
  23. retry.reset()
  24. assert retry._attempts == 0
  25. def test_too_many_tries(self):
  26. from kazoo.retry import RetryFailedError
  27. retry = self._makeOne(delay=0)
  28. with pytest.raises(RetryFailedError):
  29. retry(self._fail(times=999))
  30. assert retry._attempts == 1
  31. def test_maximum_delay(self):
  32. def sleep_func(_time):
  33. pass
  34. retry = self._makeOne(delay=10, max_tries=100, sleep_func=sleep_func)
  35. retry(self._fail(times=10))
  36. assert retry._cur_delay < 4000
  37. # gevent's sleep function is picky about the type
  38. assert type(retry._cur_delay) == float
  39. def test_copy(self):
  40. def _sleep(t):
  41. return None
  42. retry = self._makeOne(sleep_func=_sleep)
  43. rcopy = retry.copy()
  44. assert rcopy.sleep_func is _sleep
  45. class TestKazooRetry(unittest.TestCase):
  46. def _makeOne(self, **kw):
  47. from kazoo.retry import KazooRetry
  48. return KazooRetry(**kw)
  49. def test_connection_closed(self):
  50. from kazoo.exceptions import ConnectionClosedError
  51. retry = self._makeOne()
  52. def testit():
  53. raise ConnectionClosedError()
  54. with pytest.raises(ConnectionClosedError):
  55. retry(testit)
  56. def test_session_expired(self):
  57. from kazoo.exceptions import SessionExpiredError
  58. retry = self._makeOne(max_tries=1)
  59. def testit():
  60. raise SessionExpiredError()
  61. with pytest.raises(Exception):
  62. retry(testit)