Skip to content

Instantly share code, notes, and snippets.

@jmehnle
Created August 14, 2024 19:03
Show Gist options
  • Save jmehnle/2cf4deba48c32c01fdf6c0ee5248a1d1 to your computer and use it in GitHub Desktop.
Save jmehnle/2cf4deba48c32c01fdf6c0ee5248a1d1 to your computer and use it in GitHub Desktop.
Allowing `tenacity` retry sleep to be disabled during pytest unit tests (https://github.com/jd/tenacity/issues/106)
from contextlib import contextmanager
import asyncpg
import tenacity.asyncio
from tenacity import AsyncRetrying, retry_if_exception_type, stop_after_delay, wait_exponential
_sleep_enabled = True
# This indirection allows for global disabling of sleep, such as during unit tests.
async def _sleep(seconds: float) -> None:
if _sleep_enabled:
await tenacity.asyncio._portable_async_sleep(seconds)
@contextmanager
def no_retry_sleep():
global _sleep_enabled
_sleep_enabled = False
try:
yield
finally:
_sleep_enabled = True
def make_retry(*exception_types: type[Exception], **kwargs: Any) -> AsyncRetrying:
kwargs = {
'wait': wait_exponential(min=10, max=60),
'stop': stop_after_delay(600),
'sleep': _sleep,
**kwargs,
}
if exception_types:
kwargs['retry'] = retry_if_exception_type(exception_types)
return AsyncRetrying(**kwargs)
pg_retry = make_retry(
asyncpg.InterfaceError,
)
aws_retry = make_retry(
...
)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment