def test_none_response_when_no_matches(self): delay_function = retryhandler.create_exponential_delay_function(1, 2) checker = retryhandler.HTTPStatusCodeChecker(500) handler = retryhandler.RetryHandler(checker, delay_function) response = (HTTP_200_RESPONSE, {}) self.assertIsNone( handler(response=response, attempts=1, caught_exception=None))
def test_multi_checker(self): checker = retryhandler.ServiceErrorCodeChecker(status_code=400, error_code='Throttled') checker2 = retryhandler.HTTPStatusCodeChecker(500) self.checker = retryhandler.MultiChecker([checker, checker2]) self.assert_should_be_retried((HTTP_500_RESPONSE, {})) self.assert_should_be_retried(response=(HTTP_400_RESPONSE, { 'Error': { 'Code': 'Throttled' } })) self.assert_should_not_be_retried(response=(HTTP_200_RESPONSE, {}))
def test_max_attempts(self): self.checker = retryhandler.MaxAttemptsDecorator( retryhandler.HTTPStatusCodeChecker(500), max_attempts=3) # Retry up to three times. self.assert_should_be_retried((HTTP_500_RESPONSE, {}), attempt_number=1) self.assert_should_be_retried((HTTP_500_RESPONSE, {}), attempt_number=2) # On the third failed response, we've reached the # max attempts so we should return False. self.assert_should_not_be_retried((HTTP_500_RESPONSE, {}), attempt_number=3)
def test_action_tied_to_policy(self): # When a retry rule matches we should return the # amount of time to sleep, otherwise we should return None. delay_function = retryhandler.create_exponential_delay_function(1, 2) checker = retryhandler.HTTPStatusCodeChecker(500) handler = retryhandler.RetryHandler(checker, delay_function) response = (HTTP_500_RESPONSE, {}) self.assertEqual( handler(response=response, attempts=1, caught_exception=None), 1) self.assertEqual( handler(response=response, attempts=2, caught_exception=None), 2) self.assertEqual( handler(response=response, attempts=3, caught_exception=None), 4) self.assertEqual( handler(response=response, attempts=4, caught_exception=None), 8)
def test_max_attempts_successful(self): self.checker = retryhandler.MaxAttemptsDecorator( retryhandler.HTTPStatusCodeChecker(500), max_attempts=3) self.assert_should_be_retried((HTTP_500_RESPONSE, {}), attempt_number=1) # The second retry is successful. self.assert_should_not_be_retried((HTTP_200_RESPONSE, {}), attempt_number=2) # But now we can reuse this object. self.assert_should_be_retried((HTTP_500_RESPONSE, {}), attempt_number=1) self.assert_should_be_retried((HTTP_500_RESPONSE, {}), attempt_number=2) self.assert_should_not_be_retried((HTTP_500_RESPONSE, {}), attempt_number=3)
def test_status_code_checker(self): self.checker = retryhandler.HTTPStatusCodeChecker(500) self.assert_should_be_retried(response=(HTTP_500_RESPONSE, {}))