|
|
13b1513 |
From c5eb00d4151e204cab32a3026583326745054419 Mon Sep 17 00:00:00 2001
|
|
|
13b1513 |
From: Stefan Tjarks <stefan.tjarks@eventbase.com>
|
|
|
13b1513 |
Date: Tue, 17 Jul 2018 13:53:05 -0700
|
|
|
13b1513 |
Subject: [PATCH] Drop support for Python 3.4 to support aiohttp>3.3
|
|
|
13b1513 |
|
|
|
13b1513 |
aiohttp dropped support for Python 3.4 and with it `asyncio.coroutine`
|
|
|
13b1513 |
decorated generators.
|
|
|
13b1513 |
To support latest aiohttp Python 3.4 support is dropped and all notion
|
|
|
13b1513 |
of `asyncio.coroutine` is replaced with `async def`.
|
|
|
13b1513 |
|
|
|
13b1513 |
Signed-off-by: Randy Barlow <randy@electronsweatshop.com>
|
|
|
13b1513 |
---
|
|
|
13b1513 |
.coveragerc-py34 => .coveragerc-py35 | 0
|
|
|
13b1513 |
.travis.yml | 1 -
|
|
|
13b1513 |
Makefile | 10 +-
|
|
|
13b1513 |
README.rst | 17 +-
|
|
|
13b1513 |
backoff/_async.py | 33 ++--
|
|
|
13b1513 |
.../test_backoff_async.py | 167 +++++++-----------
|
|
|
13b1513 |
6 files changed, 84 insertions(+), 144 deletions(-)
|
|
|
13b1513 |
rename .coveragerc-py34 => .coveragerc-py35 (100%)
|
|
|
13b1513 |
rename tests/{python34 => python35}/test_backoff_async.py (82%)
|
|
|
13b1513 |
|
|
|
13b1513 |
diff --git a/.coveragerc-py34 b/.coveragerc-py35
|
|
|
13b1513 |
similarity index 100%
|
|
|
13b1513 |
rename from .coveragerc-py34
|
|
|
13b1513 |
rename to .coveragerc-py35
|
|
|
13b1513 |
diff --git a/.travis.yml b/.travis.yml
|
|
|
13b1513 |
index 1c0c7fd..b5cb8de 100644
|
|
|
13b1513 |
--- a/.travis.yml
|
|
|
13b1513 |
+++ b/.travis.yml
|
|
|
13b1513 |
@@ -2,7 +2,6 @@ language: python
|
|
|
13b1513 |
python:
|
|
|
13b1513 |
- "2.6"
|
|
|
13b1513 |
- "2.7"
|
|
|
13b1513 |
- - "3.4"
|
|
|
13b1513 |
- "3.5"
|
|
|
13b1513 |
- "3.6"
|
|
|
13b1513 |
matrix:
|
|
|
13b1513 |
diff --git a/Makefile b/Makefile
|
|
|
13b1513 |
index 66526bd..eb5f188 100644
|
|
|
13b1513 |
--- a/Makefile
|
|
|
13b1513 |
+++ b/Makefile
|
|
|
13b1513 |
@@ -1,7 +1,7 @@
|
|
|
13b1513 |
PY_VERSION := $(wordlist 2,4,$(subst ., ,$(shell python --version 2>&1)))
|
|
|
13b1513 |
PY_MAJOR := $(word 1,${PY_VERSION})
|
|
|
13b1513 |
PY_MINOR := $(word 2,${PY_VERSION})
|
|
|
13b1513 |
-PY_GTE_34 = $(shell echo $(PY_MAJOR).$(PY_MINOR)\>=3.4 | bc)
|
|
|
13b1513 |
+PY_GTE_35 = $(shell echo $(PY_MAJOR).$(PY_MINOR)\>=3.5 | bc)
|
|
|
13b1513 |
PY_GTE_27 = $(shell echo $(PY_MAJOR).$(PY_MINOR)\>=2.7 | bc)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
@@ -18,10 +18,10 @@ pep8:
|
|
|
13b1513 |
@pep8 backoff tests
|
|
|
13b1513 |
|
|
|
13b1513 |
flake8:
|
|
|
13b1513 |
-ifeq ($(PY_GTE_34),1)
|
|
|
13b1513 |
+ifeq ($(PY_GTE_35),1)
|
|
|
13b1513 |
@flake8 backoff tests
|
|
|
13b1513 |
else ifeq ($(PY_GTE_27),1)
|
|
|
13b1513 |
- @flake8 --exclude tests/python34,backoff/_async.py backoff tests
|
|
|
13b1513 |
+ @flake8 --exclude tests/python35,backoff/_async.py backoff tests
|
|
|
13b1513 |
else
|
|
|
13b1513 |
@echo 'Not running flake8 for Python < 2.7'
|
|
|
13b1513 |
endif
|
|
|
13b1513 |
@@ -32,8 +32,8 @@ clean:
|
|
|
13b1513 |
@rm -rf build dist .coverage MANIFEST
|
|
|
13b1513 |
|
|
|
13b1513 |
test: clean
|
|
|
13b1513 |
-ifeq ($(PY_GTE_34),1)
|
|
|
13b1513 |
- @PYTHONPATH=. py.test --cov-config .coveragerc-py34 --cov backoff tests
|
|
|
13b1513 |
+ifeq ($(PY_GTE_35),1)
|
|
|
13b1513 |
+ @PYTHONPATH=. py.test --cov-config .coveragerc-py35 --cov backoff tests
|
|
|
13b1513 |
else
|
|
|
13b1513 |
@PYTHONPATH=. py.test --cov-config .coveragerc-py2 --cov backoff tests/test_*.py
|
|
|
13b1513 |
endif
|
|
|
13b1513 |
diff --git a/README.rst b/README.rst
|
|
|
13b1513 |
index ee535e7..fee5723 100644
|
|
|
13b1513 |
--- a/README.rst
|
|
|
13b1513 |
+++ b/README.rst
|
|
|
13b1513 |
@@ -284,22 +284,7 @@ On Python 3.5 and above with ``async def`` and ``await`` syntax:
|
|
|
13b1513 |
async with session.get(url) as response:
|
|
|
13b1513 |
return await response.text()
|
|
|
13b1513 |
|
|
|
13b1513 |
-In case you use Python 3.4 you can use `@asyncio.coroutine` and `yield from`:
|
|
|
13b1513 |
-
|
|
|
13b1513 |
-.. code-block:: python
|
|
|
13b1513 |
-
|
|
|
13b1513 |
- @backoff.on_exception(backoff.expo, aiohttp.ClientError, max_time=60)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def get_url_py34(url):
|
|
|
13b1513 |
- with aiohttp.ClientSession() as session:
|
|
|
13b1513 |
- response = yield from session.get(url)
|
|
|
13b1513 |
- try:
|
|
|
13b1513 |
- return (yield from response.text())
|
|
|
13b1513 |
- except Exception:
|
|
|
13b1513 |
- response.close()
|
|
|
13b1513 |
- raise
|
|
|
13b1513 |
- finally:
|
|
|
13b1513 |
- yield from response.release()
|
|
|
13b1513 |
+Python 3.4 is not supported.
|
|
|
13b1513 |
|
|
|
13b1513 |
Logging configuration
|
|
|
13b1513 |
---------------------
|
|
|
13b1513 |
diff --git a/backoff/_async.py b/backoff/_async.py
|
|
|
13b1513 |
index e1b70d0..2cd33a8 100644
|
|
|
13b1513 |
--- a/backoff/_async.py
|
|
|
13b1513 |
+++ b/backoff/_async.py
|
|
|
13b1513 |
@@ -20,8 +20,7 @@ def _ensure_coroutines(coros_or_funcs):
|
|
|
13b1513 |
return [_ensure_coroutine(f) for f in coros_or_funcs]
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@asyncio.coroutine
|
|
|
13b1513 |
-def _call_handlers(hdlrs, target, args, kwargs, tries, elapsed, **extra):
|
|
|
13b1513 |
+async def _call_handlers(hdlrs, target, args, kwargs, tries, elapsed, **extra):
|
|
|
13b1513 |
details = {
|
|
|
13b1513 |
'target': target,
|
|
|
13b1513 |
'args': args,
|
|
|
13b1513 |
@@ -31,7 +30,7 @@ def _call_handlers(hdlrs, target, args, kwargs, tries, elapsed, **extra):
|
|
|
13b1513 |
}
|
|
|
13b1513 |
details.update(extra)
|
|
|
13b1513 |
for hdlr in hdlrs:
|
|
|
13b1513 |
- yield from hdlr(details)
|
|
|
13b1513 |
+ await hdlr(details)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
def retry_predicate(target, wait_gen, predicate,
|
|
|
13b1513 |
@@ -49,8 +48,7 @@ def retry_predicate(target, wait_gen, predicate,
|
|
|
13b1513 |
assert asyncio.iscoroutinefunction(target)
|
|
|
13b1513 |
|
|
|
13b1513 |
@functools.wraps(target)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def retry(*args, **kwargs):
|
|
|
13b1513 |
+ async def retry(*args, **kwargs):
|
|
|
13b1513 |
|
|
|
13b1513 |
# change names because python 2.x doesn't have nonlocal
|
|
|
13b1513 |
max_tries_ = _maybe_call(max_tries)
|
|
|
13b1513 |
@@ -64,20 +62,20 @@ def retry_predicate(target, wait_gen, predicate,
|
|
|
13b1513 |
elapsed = _total_seconds(datetime.datetime.now() - start)
|
|
|
13b1513 |
details = (target, args, kwargs, tries, elapsed)
|
|
|
13b1513 |
|
|
|
13b1513 |
- ret = yield from target(*args, **kwargs)
|
|
|
13b1513 |
+ ret = await target(*args, **kwargs)
|
|
|
13b1513 |
if predicate(ret):
|
|
|
13b1513 |
max_tries_exceeded = (tries == max_tries_)
|
|
|
13b1513 |
max_time_exceeded = (max_time_ is not None and
|
|
|
13b1513 |
elapsed >= max_time_)
|
|
|
13b1513 |
|
|
|
13b1513 |
if max_tries_exceeded or max_time_exceeded:
|
|
|
13b1513 |
- yield from _call_handlers(
|
|
|
13b1513 |
+ await _call_handlers(
|
|
|
13b1513 |
giveup_hdlrs, *details, value=ret)
|
|
|
13b1513 |
break
|
|
|
13b1513 |
|
|
|
13b1513 |
seconds = _next_wait(wait, jitter, elapsed, max_time_)
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from _call_handlers(
|
|
|
13b1513 |
+ await _call_handlers(
|
|
|
13b1513 |
backoff_hdlrs, *details, value=ret, wait=seconds)
|
|
|
13b1513 |
|
|
|
13b1513 |
# Note: there is no convenient way to pass explicit event
|
|
|
13b1513 |
@@ -89,10 +87,10 @@ def retry_predicate(target, wait_gen, predicate,
|
|
|
13b1513 |
# See for details:
|
|
|
13b1513 |
# <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
|
|
|
13b1513 |
# <https://bugs.python.org/issue28613>
|
|
|
13b1513 |
- yield from asyncio.sleep(seconds)
|
|
|
13b1513 |
+ await asyncio.sleep(seconds)
|
|
|
13b1513 |
continue
|
|
|
13b1513 |
else:
|
|
|
13b1513 |
- yield from _call_handlers(success_hdlrs, *details, value=ret)
|
|
|
13b1513 |
+ await _call_handlers(success_hdlrs, *details, value=ret)
|
|
|
13b1513 |
break
|
|
|
13b1513 |
|
|
|
13b1513 |
return ret
|
|
|
13b1513 |
@@ -114,8 +112,7 @@ def retry_exception(target, wait_gen, exception,
|
|
|
13b1513 |
assert not asyncio.iscoroutinefunction(jitter)
|
|
|
13b1513 |
|
|
|
13b1513 |
@functools.wraps(target)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def retry(*args, **kwargs):
|
|
|
13b1513 |
+ async def retry(*args, **kwargs):
|
|
|
13b1513 |
# change names because python 2.x doesn't have nonlocal
|
|
|
13b1513 |
max_tries_ = _maybe_call(max_tries)
|
|
|
13b1513 |
max_time_ = _maybe_call(max_time)
|
|
|
13b1513 |
@@ -129,20 +126,20 @@ def retry_exception(target, wait_gen, exception,
|
|
|
13b1513 |
details = (target, args, kwargs, tries, elapsed)
|
|
|
13b1513 |
|
|
|
13b1513 |
try:
|
|
|
13b1513 |
- ret = yield from target(*args, **kwargs)
|
|
|
13b1513 |
+ ret = await target(*args, **kwargs)
|
|
|
13b1513 |
except exception as e:
|
|
|
13b1513 |
- giveup_result = yield from giveup(e)
|
|
|
13b1513 |
+ giveup_result = await giveup(e)
|
|
|
13b1513 |
max_tries_exceeded = (tries == max_tries_)
|
|
|
13b1513 |
max_time_exceeded = (max_time_ is not None and
|
|
|
13b1513 |
elapsed >= max_time_)
|
|
|
13b1513 |
|
|
|
13b1513 |
if giveup_result or max_tries_exceeded or max_time_exceeded:
|
|
|
13b1513 |
- yield from _call_handlers(giveup_hdlrs, *details)
|
|
|
13b1513 |
+ await _call_handlers(giveup_hdlrs, *details)
|
|
|
13b1513 |
raise
|
|
|
13b1513 |
|
|
|
13b1513 |
seconds = _next_wait(wait, jitter, elapsed, max_time_)
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from _call_handlers(
|
|
|
13b1513 |
+ await _call_handlers(
|
|
|
13b1513 |
backoff_hdlrs, *details, wait=seconds)
|
|
|
13b1513 |
|
|
|
13b1513 |
# Note: there is no convenient way to pass explicit event
|
|
|
13b1513 |
@@ -154,9 +151,9 @@ def retry_exception(target, wait_gen, exception,
|
|
|
13b1513 |
# See for details:
|
|
|
13b1513 |
# <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
|
|
|
13b1513 |
# <https://bugs.python.org/issue28613>
|
|
|
13b1513 |
- yield from asyncio.sleep(seconds)
|
|
|
13b1513 |
+ await asyncio.sleep(seconds)
|
|
|
13b1513 |
else:
|
|
|
13b1513 |
- yield from _call_handlers(success_hdlrs, *details)
|
|
|
13b1513 |
+ await _call_handlers(success_hdlrs, *details)
|
|
|
13b1513 |
|
|
|
13b1513 |
return ret
|
|
|
13b1513 |
return retry
|
|
|
13b1513 |
diff --git a/tests/python34/test_backoff_async.py b/tests/python35/test_backoff_async.py
|
|
|
13b1513 |
similarity index 82%
|
|
|
13b1513 |
rename from tests/python34/test_backoff_async.py
|
|
|
13b1513 |
rename to tests/python35/test_backoff_async.py
|
|
|
13b1513 |
index 0917c6b..c1a30f8 100644
|
|
|
13b1513 |
--- a/tests/python34/test_backoff_async.py
|
|
|
13b1513 |
+++ b/tests/python35/test_backoff_async.py
|
|
|
13b1513 |
@@ -8,47 +8,41 @@ import random
|
|
|
13b1513 |
from tests.common import _log_hdlrs, _save_target
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate(monkeypatch):
|
|
|
13b1513 |
+async def test_on_predicate(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.expo)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def return_true(log, n):
|
|
|
13b1513 |
+ async def return_true(log, n):
|
|
|
13b1513 |
val = (len(log) == n - 1)
|
|
|
13b1513 |
log.append(val)
|
|
|
13b1513 |
return val
|
|
|
13b1513 |
|
|
|
13b1513 |
log = []
|
|
|
13b1513 |
- ret = yield from return_true(log, 3)
|
|
|
13b1513 |
+ ret = await return_true(log, 3)
|
|
|
13b1513 |
assert ret is True
|
|
|
13b1513 |
assert 3 == len(log)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_max_tries(monkeypatch):
|
|
|
13b1513 |
+async def test_on_predicate_max_tries(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.expo, jitter=None, max_tries=3)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def return_true(log, n):
|
|
|
13b1513 |
+ async def return_true(log, n):
|
|
|
13b1513 |
val = (len(log) == n)
|
|
|
13b1513 |
log.append(val)
|
|
|
13b1513 |
return val
|
|
|
13b1513 |
|
|
|
13b1513 |
log = []
|
|
|
13b1513 |
- ret = yield from return_true(log, 10)
|
|
|
13b1513 |
+ ret = await return_true(log, 10)
|
|
|
13b1513 |
assert ret is False
|
|
|
13b1513 |
assert 3 == len(log)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_exception(backoff.expo, KeyError)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def keyerror_then_true(log, n):
|
|
|
13b1513 |
+ async def keyerror_then_true(log, n):
|
|
|
13b1513 |
if len(log) == n:
|
|
|
13b1513 |
return True
|
|
|
13b1513 |
e = KeyError()
|
|
|
13b1513 |
@@ -56,17 +50,15 @@ def test_on_exception(monkeypatch):
|
|
|
13b1513 |
raise e
|
|
|
13b1513 |
|
|
|
13b1513 |
log = []
|
|
|
13b1513 |
- assert (yield from keyerror_then_true(log, 3)) is True
|
|
|
13b1513 |
+ assert (await keyerror_then_true(log, 3)) is True
|
|
|
13b1513 |
assert 3 == len(log)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_tuple(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_tuple(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_exception(backoff.expo, (KeyError, ValueError))
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def keyerror_valueerror_then_true(log):
|
|
|
13b1513 |
+ async def keyerror_valueerror_then_true(log):
|
|
|
13b1513 |
if len(log) == 2:
|
|
|
13b1513 |
return True
|
|
|
13b1513 |
if len(log) == 0:
|
|
|
13b1513 |
@@ -77,19 +69,17 @@ def test_on_exception_tuple(monkeypatch):
|
|
|
13b1513 |
raise e
|
|
|
13b1513 |
|
|
|
13b1513 |
log = []
|
|
|
13b1513 |
- assert (yield from keyerror_valueerror_then_true(log)) is True
|
|
|
13b1513 |
+ assert (await keyerror_valueerror_then_true(log)) is True
|
|
|
13b1513 |
assert 2 == len(log)
|
|
|
13b1513 |
assert isinstance(log[0], KeyError)
|
|
|
13b1513 |
assert isinstance(log[1], ValueError)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_max_tries(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_max_tries(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_exception(backoff.expo, KeyError, jitter=None, max_tries=3)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def keyerror_then_true(log, n, foo=None):
|
|
|
13b1513 |
+ async def keyerror_then_true(log, n, foo=None):
|
|
|
13b1513 |
if len(log) == n:
|
|
|
13b1513 |
return True
|
|
|
13b1513 |
e = KeyError()
|
|
|
13b1513 |
@@ -98,13 +88,12 @@ def test_on_exception_max_tries(monkeypatch):
|
|
|
13b1513 |
|
|
|
13b1513 |
log = []
|
|
|
13b1513 |
with pytest.raises(KeyError):
|
|
|
13b1513 |
- yield from keyerror_then_true(log, 10, foo="bar")
|
|
|
13b1513 |
+ await keyerror_then_true(log, 10, foo="bar")
|
|
|
13b1513 |
|
|
|
13b1513 |
assert 3 == len(log)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_success_random_jitter(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_success_random_jitter(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
@@ -117,13 +106,12 @@ def test_on_exception_success_random_jitter(monkeypatch):
|
|
|
13b1513 |
jitter=backoff.random_jitter,
|
|
|
13b1513 |
factor=0.5)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def succeeder(*args, **kwargs):
|
|
|
13b1513 |
+ async def succeeder(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
if len(log['backoff']) < 2:
|
|
|
13b1513 |
raise ValueError("catch me")
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -135,8 +123,7 @@ def test_on_exception_success_random_jitter(monkeypatch):
|
|
|
13b1513 |
assert details['wait'] >= 0.5 * 2 ** i
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_success_full_jitter(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_success_full_jitter(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
@@ -149,13 +136,12 @@ def test_on_exception_success_full_jitter(monkeypatch):
|
|
|
13b1513 |
jitter=backoff.full_jitter,
|
|
|
13b1513 |
factor=0.5)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def succeeder(*args, **kwargs):
|
|
|
13b1513 |
+ async def succeeder(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
if len(log['backoff']) < 2:
|
|
|
13b1513 |
raise ValueError("catch me")
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -167,8 +153,7 @@ def test_on_exception_success_full_jitter(monkeypatch):
|
|
|
13b1513 |
assert details['wait'] <= 0.5 * 2 ** i
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_success():
|
|
|
13b1513 |
+async def test_on_exception_success():
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_exception(backoff.constant,
|
|
|
13b1513 |
@@ -179,13 +164,12 @@ def test_on_exception_success():
|
|
|
13b1513 |
jitter=lambda: 0,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
def succeeder(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
if len(log['backoff']) < 2:
|
|
|
13b1513 |
raise ValueError("catch me")
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -211,8 +195,7 @@ def test_on_exception_success():
|
|
|
13b1513 |
'tries': 3}
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_giveup():
|
|
|
13b1513 |
+async def test_on_exception_giveup():
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_exception(backoff.constant,
|
|
|
13b1513 |
@@ -224,12 +207,11 @@ def test_on_exception_giveup():
|
|
|
13b1513 |
jitter=lambda: 0,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def exceptor(*args, **kwargs):
|
|
|
13b1513 |
+ async def exceptor(*args, **kwargs):
|
|
|
13b1513 |
raise ValueError("catch me")
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(ValueError):
|
|
|
13b1513 |
- yield from exceptor(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await exceptor(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice and giving up once
|
|
|
13b1513 |
assert len(log['success']) == 0
|
|
|
13b1513 |
@@ -245,8 +227,7 @@ def test_on_exception_giveup():
|
|
|
13b1513 |
'tries': 3}
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_giveup_predicate(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_giveup_predicate(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
def on_baz(e):
|
|
|
13b1513 |
@@ -257,22 +238,19 @@ def test_on_exception_giveup_predicate(monkeypatch):
|
|
|
13b1513 |
@backoff.on_exception(backoff.constant,
|
|
|
13b1513 |
ValueError,
|
|
|
13b1513 |
giveup=on_baz)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def foo_bar_baz():
|
|
|
13b1513 |
+ async def foo_bar_baz():
|
|
|
13b1513 |
raise ValueError(vals.pop())
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(ValueError):
|
|
|
13b1513 |
- yield from foo_bar_baz()
|
|
|
13b1513 |
+ await foo_bar_baz()
|
|
|
13b1513 |
|
|
|
13b1513 |
assert not vals
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_giveup_coro(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_giveup_coro(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def on_baz(e):
|
|
|
13b1513 |
+ async def on_baz(e):
|
|
|
13b1513 |
return str(e) == "baz"
|
|
|
13b1513 |
|
|
|
13b1513 |
vals = ["baz", "bar", "foo"]
|
|
|
13b1513 |
@@ -280,18 +258,16 @@ def test_on_exception_giveup_coro(monkeypatch):
|
|
|
13b1513 |
@backoff.on_exception(backoff.constant,
|
|
|
13b1513 |
ValueError,
|
|
|
13b1513 |
giveup=on_baz)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def foo_bar_baz():
|
|
|
13b1513 |
+ async def foo_bar_baz():
|
|
|
13b1513 |
raise ValueError(vals.pop())
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(ValueError):
|
|
|
13b1513 |
- yield from foo_bar_baz()
|
|
|
13b1513 |
+ await foo_bar_baz()
|
|
|
13b1513 |
|
|
|
13b1513 |
assert not vals
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_success():
|
|
|
13b1513 |
+async def test_on_predicate_success():
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.constant,
|
|
|
13b1513 |
@@ -301,12 +277,11 @@ def test_on_predicate_success():
|
|
|
13b1513 |
jitter=lambda: 0,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def success(*args, **kwargs):
|
|
|
13b1513 |
+ async def success(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
return len(log['backoff']) == 2
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from success(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await success(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -334,8 +309,7 @@ def test_on_predicate_success():
|
|
|
13b1513 |
'value': True}
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_giveup():
|
|
|
13b1513 |
+async def test_on_predicate_giveup():
|
|
|
13b1513 |
log, log_success, log_backoff, log_giveup = _log_hdlrs()
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.constant,
|
|
|
13b1513 |
@@ -346,11 +320,10 @@ def test_on_predicate_giveup():
|
|
|
13b1513 |
jitter=lambda: 0,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def emptiness(*args, **kwargs):
|
|
|
13b1513 |
+ async def emptiness(*args, **kwargs):
|
|
|
13b1513 |
pass
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from emptiness(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await emptiness(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice and giving up once
|
|
|
13b1513 |
assert len(log['success']) == 0
|
|
|
13b1513 |
@@ -367,8 +340,7 @@ def test_on_predicate_giveup():
|
|
|
13b1513 |
'value': None}
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_iterable_handlers():
|
|
|
13b1513 |
+async def test_on_predicate_iterable_handlers():
|
|
|
13b1513 |
hdlrs = [_log_hdlrs() for _ in range(3)]
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.constant,
|
|
|
13b1513 |
@@ -379,11 +351,10 @@ def test_on_predicate_iterable_handlers():
|
|
|
13b1513 |
jitter=lambda: 0,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def emptiness(*args, **kwargs):
|
|
|
13b1513 |
+ async def emptiness(*args, **kwargs):
|
|
|
13b1513 |
pass
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from emptiness(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await emptiness(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
for i in range(3):
|
|
|
13b1513 |
assert len(hdlrs[i][0]['success']) == 0
|
|
|
13b1513 |
@@ -402,8 +373,7 @@ def test_on_predicate_iterable_handlers():
|
|
|
13b1513 |
|
|
|
13b1513 |
# To maintain backward compatibility,
|
|
|
13b1513 |
# on_predicate should support 0-argument jitter function.
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
monkeypatch.setattr('random.random', lambda: 0)
|
|
|
13b1513 |
|
|
|
13b1513 |
@@ -417,13 +387,12 @@ def test_on_exception_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
jitter=random.random,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def succeeder(*args, **kwargs):
|
|
|
13b1513 |
+ async def succeeder(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
if len(log['backoff']) < 2:
|
|
|
13b1513 |
raise ValueError("catch me")
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await succeeder(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -451,8 +420,7 @@ def test_on_exception_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
|
|
|
13b1513 |
# To maintain backward compatibility,
|
|
|
13b1513 |
# on_predicate should support 0-argument jitter function.
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
+async def test_on_predicate_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
monkeypatch.setattr('random.random', lambda: 0)
|
|
|
13b1513 |
|
|
|
13b1513 |
@@ -465,12 +433,11 @@ def test_on_predicate_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
jitter=random.random,
|
|
|
13b1513 |
interval=0)
|
|
|
13b1513 |
@_save_target
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def success(*args, **kwargs):
|
|
|
13b1513 |
+ async def success(*args, **kwargs):
|
|
|
13b1513 |
# succeed after we've backed off twice
|
|
|
13b1513 |
return len(log['backoff']) == 2
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from success(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
+ await success(1, 2, 3, foo=1, bar=2)
|
|
|
13b1513 |
|
|
|
13b1513 |
# we try 3 times, backing off twice before succeeding
|
|
|
13b1513 |
assert len(log['success']) == 1
|
|
|
13b1513 |
@@ -498,8 +465,7 @@ def test_on_predicate_success_0_arg_jitter(monkeypatch):
|
|
|
13b1513 |
'value': True}
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_callable_max_tries(monkeypatch):
|
|
|
13b1513 |
+async def test_on_exception_callable_max_tries(monkeypatch):
|
|
|
13b1513 |
monkeypatch.setattr('asyncio.sleep', asyncio.coroutine(lambda x: None))
|
|
|
13b1513 |
|
|
|
13b1513 |
def lookup_max_tries():
|
|
|
13b1513 |
@@ -510,19 +476,17 @@ def test_on_exception_callable_max_tries(monkeypatch):
|
|
|
13b1513 |
@backoff.on_exception(backoff.constant,
|
|
|
13b1513 |
ValueError,
|
|
|
13b1513 |
max_tries=lookup_max_tries)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def exceptor():
|
|
|
13b1513 |
+ async def exceptor():
|
|
|
13b1513 |
log.append(True)
|
|
|
13b1513 |
raise ValueError()
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(ValueError):
|
|
|
13b1513 |
- yield from exceptor()
|
|
|
13b1513 |
+ await exceptor()
|
|
|
13b1513 |
|
|
|
13b1513 |
assert len(log) == 3
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_callable_gen_kwargs():
|
|
|
13b1513 |
+async def test_on_exception_callable_gen_kwargs():
|
|
|
13b1513 |
|
|
|
13b1513 |
def lookup_foo():
|
|
|
13b1513 |
return "foo"
|
|
|
13b1513 |
@@ -539,25 +503,22 @@ def test_on_exception_callable_gen_kwargs():
|
|
|
13b1513 |
max_tries=2,
|
|
|
13b1513 |
foo=lookup_foo,
|
|
|
13b1513 |
bar="bar")
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def exceptor():
|
|
|
13b1513 |
+ async def exceptor():
|
|
|
13b1513 |
raise ValueError("aah")
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(ValueError):
|
|
|
13b1513 |
- yield from exceptor()
|
|
|
13b1513 |
+ await exceptor()
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_coro_cancelling(event_loop):
|
|
|
13b1513 |
+async def test_on_exception_coro_cancelling(event_loop):
|
|
|
13b1513 |
sleep_started_event = asyncio.Event()
|
|
|
13b1513 |
|
|
|
13b1513 |
@backoff.on_predicate(backoff.expo)
|
|
|
13b1513 |
- @asyncio.coroutine
|
|
|
13b1513 |
- def coro():
|
|
|
13b1513 |
+ async def coro():
|
|
|
13b1513 |
sleep_started_event.set()
|
|
|
13b1513 |
|
|
|
13b1513 |
try:
|
|
|
13b1513 |
- yield from asyncio.sleep(10)
|
|
|
13b1513 |
+ await asyncio.sleep(10)
|
|
|
13b1513 |
except asyncio.CancelledError:
|
|
|
13b1513 |
return True
|
|
|
13b1513 |
|
|
|
13b1513 |
@@ -565,17 +526,16 @@ def test_on_exception_coro_cancelling(event_loop):
|
|
|
13b1513 |
|
|
|
13b1513 |
task = event_loop.create_task(coro())
|
|
|
13b1513 |
|
|
|
13b1513 |
- yield from sleep_started_event.wait()
|
|
|
13b1513 |
+ await sleep_started_event.wait()
|
|
|
13b1513 |
|
|
|
13b1513 |
task.cancel()
|
|
|
13b1513 |
|
|
|
13b1513 |
- assert (yield from task)
|
|
|
13b1513 |
+ assert (await task)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_exception_on_regular_function():
|
|
|
13b1513 |
+async def test_on_exception_on_regular_function():
|
|
|
13b1513 |
# Force this function to be a running coroutine.
|
|
|
13b1513 |
- yield from asyncio.sleep(0)
|
|
|
13b1513 |
+ await asyncio.sleep(0)
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(TypeError) as excinfo:
|
|
|
13b1513 |
@backoff.on_exception(backoff.expo, ValueError)
|
|
|
13b1513 |
@@ -584,10 +544,9 @@ def test_on_exception_on_regular_function():
|
|
|
13b1513 |
assert "applied to a regular function" in str(excinfo.value)
|
|
|
13b1513 |
|
|
|
13b1513 |
|
|
|
13b1513 |
-@pytest.mark.asyncio
|
|
|
13b1513 |
-def test_on_predicate_on_regular_function():
|
|
|
13b1513 |
+async def test_on_predicate_on_regular_function():
|
|
|
13b1513 |
# Force this function to be a running coroutine.
|
|
|
13b1513 |
- yield from asyncio.sleep(0)
|
|
|
13b1513 |
+ await asyncio.sleep(0)
|
|
|
13b1513 |
|
|
|
13b1513 |
with pytest.raises(TypeError) as excinfo:
|
|
|
13b1513 |
@backoff.on_predicate(backoff.expo)
|
|
|
13b1513 |
--
|
|
|
13b1513 |
2.18.0
|
|
|
13b1513 |
|