jd / tenacity

Retrying library for Python
http://tenacity.readthedocs.io
Apache License 2.0
6.82k stars 283 forks source link

Add support for mocking sleep when retrying async functions #440

Open snorfalorpagus opened 9 months ago

snorfalorpagus commented 9 months ago

This PR makes it easier to mock sleeping when retrying asynchronous functions, using the same approach as was done for synchronous functions in #236.

Example usage:

@retry(wait=wait_fixed(9999999))
def function_with_retry():
    if random.random() < 0.9:
        raise RuntimeError("boom")
    return True

async def test_something(self):
    with unittest.mock.patch("tenacity.nap.asyncio_sleep", AsyncMock()):
        await function_with_retry()

Closes #360.

mergify[bot] commented 9 months ago

⚠️ No release notes detected. Please make sure to use reno to add a changelog entry.

snorfalorpagus commented 9 months ago

@jd Let me know if there is anything else required before this can be merged. :)

aaronkavlie-wf commented 5 months ago

It would be nice to see this get in soon. We monkeypatch the sleep property on the retry decorator in our tests, however that approach broke in 8.4.2.