Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_retry_exception_returns():
strategy = retry_module.RetryStrategy(max_attempts=10, wait=5.0)
now = utils.utcnow()
expected = now + datetime.timedelta(seconds=5, microseconds=0)
exc = strategy.get_retry_exception(exception=None, attempts=1)
assert isinstance(exc, exceptions.JobRetry)
assert exc.scheduled_at == expected.replace(microsecond=0)
def test_get_schedule_in_exception(exception, expected):
strategy = retry_module.RetryStrategy(retry_exceptions=[ValueError])
assert strategy.get_schedule_in(exception=exception, attempts=0) == expected
(12, retry_module.RetryStrategy(max_attempts=12)),
(True, retry_module.RetryStrategy()),
(
retry_module.RetryStrategy(max_attempts=42),
retry_module.RetryStrategy(max_attempts=42),
),
],
)
def test_get_retry_strategy(retry, expected_strategy):
assert expected_strategy == retry_module.get_retry_strategy(retry)
def test_get_retry_exception_returns_none():
strategy = retry_module.RetryStrategy(max_attempts=10, wait=5.0)
assert strategy.get_retry_exception(exception=None, attempts=100) is None
def test_get_schedule_in_time(
attempts, schedule_in, wait, linear_wait, exponential_wait
):
strategy = retry_module.RetryStrategy(
max_attempts=10,
wait=wait,
linear_wait=linear_wait,
exponential_wait=exponential_wait,
)
assert strategy.get_schedule_in(exception=None, attempts=attempts) == schedule_in
retry_module.RetryStrategy(max_attempts=42),
),
],
)
def test_get_retry_strategy(retry, expected_strategy):
assert expected_strategy == retry_module.get_retry_strategy(retry)
def get_schedule_in(self, *, exception: Exception, attempts: int) -> Optional[int]:
if self.max_attempts and attempts >= self.max_attempts:
return None
# isinstance's 2nd param must be a tuple, not an arbitrary iterable
if self.retry_exceptions and not isinstance(
exception, tuple(self.retry_exceptions)
):
return None
wait: int = self.wait
wait += self.linear_wait * attempts
wait += self.exponential_wait ** (attempts + 1)
return wait
RetryValue = Union[bool, int, RetryStrategy]
def get_retry_strategy(retry: RetryValue) -> Optional[RetryStrategy]:
if not retry:
return None
if retry is True:
return RetryStrategy()
if isinstance(retry, int):
return RetryStrategy(max_attempts=retry)
return retry
def get_retry_strategy(retry: RetryValue) -> Optional[RetryStrategy]:
if not retry:
return None
if retry is True:
return RetryStrategy()
if isinstance(retry, int):
return RetryStrategy(max_attempts=retry)
return retry
def get_retry_strategy(retry: RetryValue) -> Optional[RetryStrategy]:
if not retry:
return None
if retry is True:
return RetryStrategy()
if isinstance(retry, int):
return RetryStrategy(max_attempts=retry)
return retry