Skip to content

Commit d014a52

Browse files
committed
Fix tests that were using asyncio.sleep
1 parent 0c60655 commit d014a52

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

async_generator/_tests/conftest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import pytest
22
from functools import wraps, partial
33
import inspect
4+
import types
5+
6+
7+
@types.coroutine
8+
def mock_sleep():
9+
yield "mock_sleep"
410

511

612
# Wrap any 'async def' tests so that they get automatically iterated.
@@ -18,7 +24,12 @@ def pytest_pyfunc_call(pyfuncitem):
1824
def wrapper(**kwargs):
1925
coro = fn(**kwargs)
2026
try:
21-
coro.send(None)
27+
while True:
28+
value = coro.send(None)
29+
if value != "mock_sleep": # pragma: no cover
30+
raise RuntimeError(
31+
"coroutine runner confused: {!r}".format(value)
32+
)
2233
except StopIteration:
2334
pass
2435

async_generator/_tests/test_async_generator.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import types
44
import sys
5-
import asyncio
65
import collections.abc
76
from functools import wraps
87
import gc
98

9+
from .conftest import mock_sleep
1010
from .. import (
1111
async_generator,
1212
yield_,
@@ -42,7 +42,7 @@ async def async_range(count):
4242
async def double(ait):
4343
async for value in ait:
4444
await yield_(value * 2)
45-
await asyncio.sleep(0.001)
45+
await mock_sleep()
4646

4747

4848
class HasAsyncGenMethod:
@@ -194,18 +194,24 @@ async def recurse():
194194
async def test_reentrance_forbidden_while_suspended_in_coroutine_runner():
195195
@async_generator
196196
async def f():
197-
await asyncio.sleep(1)
198-
await yield_()
197+
await mock_sleep()
198+
await yield_("final yield")
199199

200200
ag = f()
201201
asend_coro = ag.asend(None)
202202
fut = asend_coro.send(None)
203+
assert fut == "mock_sleep"
203204
# Now the async generator's frame is not executing, but a call to asend()
204205
# *is* executing. Make sure that in this case, ag_running is True, and we
205206
# can't start up another call to asend().
206207
assert ag.ag_running
207208
with pytest.raises(ValueError):
208209
await ag.asend(None)
210+
# Clean up
211+
with pytest.raises(StopIteration):
212+
asend_coro.send(None)
213+
with pytest.raises(StopAsyncIteration):
214+
ag.asend(None).send(None)
209215

210216

211217
################################################################
@@ -302,7 +308,7 @@ async def sync_yield_during_aclose():
302308
try:
303309
await yield_(1)
304310
finally:
305-
await asyncio.sleep(0)
311+
await mock_sleep()
306312

307313

308314
@async_generator

0 commit comments

Comments
 (0)