use pytest-asyncio

This commit is contained in:
Casper da Costa-Luis 2020-11-21 00:49:07 +00:00
parent ee5f8d21e8
commit a35529157d
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
2 changed files with 28 additions and 21 deletions

View File

@ -1,24 +1,18 @@
from functools import partial, wraps
from functools import partial
from time import time
import asyncio
from pytest import mark
from tqdm.asyncio import tqdm_asyncio, tarange
from .tests_tqdm import pretest_posttest # NOQA, pylint: disable=unused-import
from .tests_tqdm import StringIO, closing
from .tests_perf import retry_on_except
tqdm = partial(tqdm_asyncio, miniters=0, mininterval=0)
trange = partial(tarange, miniters=0, mininterval=0)
as_completed = partial(tqdm_asyncio.as_completed, miniters=0, mininterval=0)
def with_setup_sync(func):
@wraps(func)
def inner(*args, **kwargs):
return asyncio.run(func(*args, **kwargs))
return inner
def count(start=0, step=1):
i = start
while True:
@ -34,7 +28,16 @@ async def acount(*args, **kwargs):
yield i
@with_setup_sync
@mark.asyncio
async def test_break():
"""Test asyncio break"""
pbar = tqdm(count())
async for _ in pbar:
break
pbar.close()
@mark.asyncio
async def test_generators(capsys):
"""Test asyncio generators"""
with tqdm(count(), desc="counter") as pbar:
@ -52,7 +55,7 @@ async def test_generators(capsys):
assert '9it' in err
@with_setup_sync
@mark.asyncio
async def test_range():
"""Test asyncio range"""
with closing(StringIO()) as our_file:
@ -67,7 +70,7 @@ async def test_range():
assert '9/9' in our_file.getvalue()
@with_setup_sync
@mark.asyncio
async def test_nested():
"""Test asyncio nested"""
with closing(StringIO()) as our_file:
@ -78,7 +81,7 @@ async def test_nested():
assert 'outer: 100%' in our_file.getvalue()
@with_setup_sync
@mark.asyncio
async def test_coroutines():
"""Test asyncio coroutine.send"""
with closing(StringIO()) as our_file:
@ -92,16 +95,20 @@ async def test_coroutines():
assert '10it' in our_file.getvalue()
@retry_on_except(check_cpu_time=False)
@with_setup_sync
async def test_as_completed():
@mark.asyncio
async def test_as_completed(capsys):
"""Test asyncio as_completed"""
with closing(StringIO()) as our_file:
for retry in range(3):
t = time()
skew = time() - t
for i in as_completed([asyncio.sleep(0.01 * i)
for i in range(30, 0, -1)], file=our_file):
for i in range(30, 0, -1)]):
await i
t = time() - t - 2 * skew
assert 0.27 < t < 0.33, t
assert '30/30' in our_file.getvalue()
try:
assert 0.27 < t < 0.33, t
_, err = capsys.readouterr()
assert '30/30' in err
except AssertionError:
if retry == 2:
raise

View File

@ -12,6 +12,7 @@ isolated_build = True
deps =
pytest
pytest-cov
py{37,38,39}: pytest-asyncio
coverage
coveralls
commands =
@ -52,7 +53,6 @@ deps = {[extra]deps}
deps = {[extra]deps}
[testenv:py34]
# py34-compatible pandas
deps = {[extra]deps}
[testenv:perf]