Skip to content

Commit

Permalink
Access the event loop directly via asyncio due to pytest's fixture de…
Browse files Browse the repository at this point in the history
…precation

Signed-off-by: Sergey Vasilyev <[email protected]>
  • Loading branch information
nolar committed Jan 18, 2024
1 parent c0cb28b commit e376c53
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 25 deletions.
9 changes: 7 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,13 @@ def assert_logs_fn(patterns, prohibited=[], strict=False):
#
# Helpers for asyncio checks.
#
@pytest.fixture()
async def loop():
yield asyncio.get_running_loop()


@pytest.fixture(autouse=True)
def _no_asyncio_pending_tasks(event_loop):
def _no_asyncio_pending_tasks(loop: asyncio.AbstractEventLoop):
"""
Ensure there are no unattended asyncio tasks after the test.
Expand All @@ -725,7 +730,7 @@ def _no_asyncio_pending_tasks(event_loop):

# Let the pytest-asyncio's async2sync wrapper to finish all callbacks. Otherwise, it raises:
# <Task pending name='Task-2' coro=<<async_generator_athrow without __name__>()>>
event_loop.run_until_complete(asyncio.sleep(0))
loop.run_until_complete(asyncio.sleep(0))

# Detect all leftover tasks.
after = _get_all_tasks()
Expand Down
6 changes: 3 additions & 3 deletions tests/logging/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def _caplog_all_levels(caplog):


@pytest.fixture(autouse=True)
def event_queue_loop(event_loop):
token = event_queue_loop_var.set(event_loop)
def event_queue_loop(loop): # must be sync-def
token = event_queue_loop_var.set(loop)
try:
yield event_loop
yield loop
finally:
event_queue_loop_var.reset(token)

Expand Down
6 changes: 3 additions & 3 deletions tests/posting/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


@pytest.fixture()
def event_queue_loop(event_loop):
token = event_queue_loop_var.set(event_loop)
def event_queue_loop(loop): # must be sync-def
token = event_queue_loop_var.set(loop)
try:
yield event_loop
yield loop
finally:
event_queue_loop_var.reset(token)

Expand Down
4 changes: 2 additions & 2 deletions tests/posting/test_threadsafety.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@


@pytest.fixture()
def awakener(event_loop):
def awakener():
handles = []

def noop():
pass

def awaken_fn(delay, fn=noop):
handle = event_loop.call_later(delay, fn)
handle = asyncio.get_running_loop().call_later(delay, fn)
handles.append(handle)

try:
Expand Down
5 changes: 3 additions & 2 deletions tests/primitives/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def test_no_triggering():
await asyncio.wait([task])


async def test_triggering(event_loop, timer):
async def test_triggering(timer):
source = asyncio.Condition()
target = asyncio.Condition()
task = asyncio.create_task(condition_chain(source, target))
Expand All @@ -28,7 +28,8 @@ async def delayed_trigger():
async with source:
source.notify_all()

event_loop.call_later(0.1, asyncio.create_task, delayed_trigger())
loop = asyncio.get_running_loop()
loop.call_later(0.1, asyncio.create_task, delayed_trigger())

with timer:
async with target:
Expand Down
21 changes: 12 additions & 9 deletions tests/primitives/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ async def test_empty_by_default():
await asyncio.wait_for(container.wait(), timeout=0.1)


async def test_does_not_wake_up_when_reset(event_loop, timer):
async def test_does_not_wake_up_when_reset(timer):
container = Container()

async def reset_it():
await container.reset()

event_loop.call_later(0.05, asyncio.create_task, reset_it())
loop = asyncio.get_running_loop()
loop.call_later(0.05, asyncio.create_task, reset_it())

with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(container.wait(), timeout=0.1)


async def test_wakes_up_when_preset(event_loop, timer):
async def test_wakes_up_when_preset(timer):
container = Container()
await container.set(123)

Expand All @@ -34,13 +35,14 @@ async def test_wakes_up_when_preset(event_loop, timer):
assert result == 123


async def test_wakes_up_when_set(event_loop, timer):
async def test_wakes_up_when_set(timer):
container = Container()

async def set_it():
await container.set(123)

event_loop.call_later(0.1, asyncio.create_task, set_it())
loop = asyncio.get_running_loop()
loop.call_later(0.1, asyncio.create_task, set_it())

with timer:
result = await container.wait()
Expand All @@ -49,14 +51,15 @@ async def set_it():
assert result == 123


async def test_iterates_when_set(event_loop, timer):
async def test_iterates_when_set(timer):
container = Container()

async def set_it(v):
await container.set(v)

event_loop.call_later(0.1, asyncio.create_task, set_it(123))
event_loop.call_later(0.2, asyncio.create_task, set_it(234))
loop = asyncio.get_running_loop()
loop.call_later(0.1, asyncio.create_task, set_it(123))
loop.call_later(0.2, asyncio.create_task, set_it(234))

values = []
with timer:
Expand All @@ -69,7 +72,7 @@ async def set_it(v):
assert values == [123, 234]


async def test_iterates_when_preset(event_loop, timer):
async def test_iterates_when_preset(timer):
container = Container()
await container.set(123)

Expand Down
6 changes: 3 additions & 3 deletions tests/reactor/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def watcher_limited(mocker, settings):


@pytest.fixture()
def watcher_in_background(settings, resource, event_loop, worker_spy, stream):
async def watcher_in_background(settings, resource, worker_spy, stream):

# Prevent remembering the streaming objects in the mocks.
async def do_nothing(*args, **kwargs):
Expand All @@ -57,7 +57,7 @@ async def do_nothing(*args, **kwargs):
settings=settings,
processor=do_nothing,
)
task = event_loop.create_task(coro)
task = asyncio.create_task(coro)

try:
# Go for a test.
Expand All @@ -66,6 +66,6 @@ async def do_nothing(*args, **kwargs):
# Terminate the watcher to cleanup the loop.
task.cancel()
try:
event_loop.run_until_complete(task)
await task
except asyncio.CancelledError:
pass # cancellations are expected at this point
2 changes: 1 addition & 1 deletion tests/reactor/test_queueing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def test_watchevent_demultiplexing(worker_mock, timer, resource, processor
])
@pytest.mark.usefixtures('watcher_limited')
async def test_watchevent_batching(settings, resource, processor, timer,
stream, events, uids, vals, event_loop):
stream, events, uids, vals):
""" Verify that only the last event per uid is actually handled. """

# Override the default timeouts to make the tests faster.
Expand Down

0 comments on commit e376c53

Please sign in to comment.