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