def test_retry_with_unexpected_exception(self): def func(*args, **kwargs): raise error_util.VimException(None) retry = api.Retry() self.assertRaises(error_util.VimException, retry(func)) self.assertTrue(retry._retry_count == 0)
def test_retry_with_max_retries(self): responses = [error_util.SessionOverLoadException(None), error_util.SessionOverLoadException(None), error_util.SessionOverLoadException(None)] def func(*args, **kwargs): response = responses.pop(0) if isinstance(response, Exception): raise response return response retry = api.Retry(2, 0, 0, (error_util.SessionOverLoadException,)) self.assertRaises(error_util.SessionOverLoadException, retry(func)) self.assertTrue(retry._retry_count == 2)
def test_retry(self): result = "RESULT" @api.Retry() def func(*args, **kwargs): return result self.assertEqual(result, func()) def func2(*args, **kwargs): return result retry = api.Retry() self.assertEqual(result, retry(func2)()) self.assertTrue(retry._retry_count == 0)
def test_retry_with_expected_exceptions(self): result = "RESULT" responses = [error_util.SessionOverLoadException(None), error_util.SessionOverLoadException(None), result] def func(*args, **kwargs): response = responses.pop(0) if isinstance(response, Exception): raise response return response sleep_time_incr = 0.01 retry_count = 2 retry = api.Retry(10, sleep_time_incr, 10, (error_util.SessionOverLoadException,)) self.assertEqual(result, retry(func)()) self.assertTrue(retry._retry_count == retry_count) self.assertEqual(retry_count * sleep_time_incr, retry._sleep_time)