Skip to content

Add support for mocking sleep when retrying async functions#440

Closed
snorfalorpagus wants to merge 3 commits intojd:mainfrom
snorfalorpagus:mock-async-sleep
Closed

Add support for mocking sleep when retrying async functions#440
snorfalorpagus wants to merge 3 commits intojd:mainfrom
snorfalorpagus:mock-async-sleep

Conversation

@snorfalorpagus
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Contributor

mergify Bot commented Feb 8, 2024

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

jd
jd previously approved these changes Feb 9, 2024
@mergify mergify Bot dismissed jd’s stale review February 9, 2024 13:32

Pull request has been modified.

@snorfalorpagus
Copy link
Copy Markdown
Author

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

@aaronkavlie-wf
Copy link
Copy Markdown

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.

@mtvx
Copy link
Copy Markdown

mtvx commented May 20, 2025

Any change for getting this in?

@snorfalorpagus
Copy link
Copy Markdown
Author

I don't have the energy to resolve conflicts on this as it's been sitting idle for so long without merge. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot easily mock out async sleep function

4 participants