def max_attempts(self, value): # type: (int) -> None self._max_attempts = value if value == 1: # No retry self._get = self._http_session.get self._post = self._http_session.post self._delete = self._http_session.delete return with_retry = exceptions.retry_with( self._handle_retry, exceptions=self._exceptions, conditions=( # need retry when there is no exception but the status code is 503 lambda response: getattr(response, "status_code", None) == 503, ), max_attempts=self._max_attempts, ) self._get = with_retry(self._http_session.get) self._post = with_retry(self._http_session.post) self._delete = with_retry(self._http_session.delete)
def test_retry_with(): max_attempts = 3 with_retry = exceptions.retry_with( handle_retry=exceptions.RetryWithExponentialBackoff(), exceptions=[SomeException], conditions={}, max_attempts=max_attempts, ) class FailerUntil(object): def __init__(self, until=1): self.attempt = 0 self._until = until def __call__(self): self.attempt += 1 if self.attempt > self._until: return raise SomeException(self.attempt) with_retry(FailerUntil(2).__call__)() with pytest.raises(SomeException): with_retry(FailerUntil(3).__call__)()