forked from invl/retry
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
534617e
commit a34ef8b
Showing
5 changed files
with
100 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ignore: | ||
- "tests" | ||
- "setup.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
[metadata] | ||
name = reretry | ||
version = 0.11.1 | ||
version = 0.11.2 | ||
author = leshchenko1979 | ||
author_email = [email protected] | ||
summary = An easy to use, but functional decorator for retrying on exceptions. | ||
license = Apache License 2.0 | ||
description_file = README.md | ||
long_description_content_type = text/x-rst | ||
home_page = https://github.com/leshchenko1979/reretry | ||
requires_python = >=3.5 | ||
requires_python = >=3.6 | ||
classifier = | ||
Development Status :: 4 - Beta | ||
Intended Audience :: Developers | ||
License :: OSI Approved :: Apache Software License | ||
Natural Language :: English | ||
Operating System :: OS Independent | ||
Programming Language :: Python | ||
Programming Language :: Python :: 3.5 | ||
Programming Language :: Python :: 3.6 | ||
Programming Language :: Python :: 3.7 | ||
Programming Language :: Python :: 3.8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import asyncio | ||
|
||
import pytest | ||
from reretry.api import _is_async, retry, retry_call | ||
|
||
|
||
|
||
def test_is_async(): | ||
async def async_func(): | ||
pass | ||
|
||
def non_async_func(): | ||
pass | ||
|
||
def generator(): | ||
yield | ||
|
||
assert _is_async(async_func) | ||
assert not _is_async(non_async_func) | ||
assert not _is_async(generator) | ||
assert not _is_async(generator()) | ||
assert not _is_async(MagicMock(spec=non_async_func, return_value=-1)) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async(): | ||
attempts = 1 | ||
raised = False | ||
|
||
@retry(tries=2) | ||
async def f(): | ||
await asyncio.sleep(0.01) | ||
nonlocal attempts, raised | ||
if attempts: | ||
raised = True | ||
attempts -= 1 | ||
raise RuntimeError | ||
return True | ||
|
||
assert await f() | ||
assert raised | ||
assert attempts == 0 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_fail_and_callback(): | ||
cb_called = False | ||
|
||
async def cb(exception: Exception): | ||
nonlocal cb_called | ||
cb_called = True | ||
assert exception.args[0] == 1 | ||
|
||
attempts = 3 | ||
|
||
@retry(tries=2, fail_callback=cb) | ||
async def f(): | ||
await asyncio.sleep(0.01) | ||
nonlocal attempts | ||
if attempts: | ||
attempts -= 1 | ||
raise RuntimeError(1) | ||
return True | ||
|
||
with pytest.raises(RuntimeError): | ||
await f() | ||
|
||
assert cb_called | ||
|
||
|
||
def test_check_params(): | ||
with pytest.raises(AssertionError): | ||
retry_call(lambda: None, show_traceback=True, logger=None) | ||
|
||
async def async_func(): | ||
pass | ||
|
||
def non_async_func(): | ||
pass | ||
|
||
with pytest.raises(AssertionError): | ||
retry_call(async_func, fail_callback=non_async_func) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters