From 16049c1301518b2e7515a96fb6f3d0689357cc0e Mon Sep 17 00:00:00 2001 From: Sergey Vasilyev Date: Thu, 18 Jan 2024 23:23:33 +0100 Subject: [PATCH] Access the event loop directly via asyncio due to pytest's fixture deprecation Signed-off-by: Sergey Vasilyev --- tests/conftest.py | 9 +++++++-- tests/logging/conftest.py | 6 +++--- tests/posting/conftest.py | 6 +++--- tests/posting/test_threadsafety.py | 4 ++-- tests/primitives/test_conditions.py | 5 +++-- tests/primitives/test_containers.py | 21 ++++++++++++--------- tests/reactor/conftest.py | 6 +++--- tests/reactor/test_queueing.py | 2 +- 8 files changed, 34 insertions(+), 25 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9bf75e7a..dae1151f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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. @@ -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: # ()>> - event_loop.run_until_complete(asyncio.sleep(0)) + loop.run_until_complete(asyncio.sleep(0)) # Detect all leftover tasks. after = _get_all_tasks() diff --git a/tests/logging/conftest.py b/tests/logging/conftest.py index ac552630..da1915de 100644 --- a/tests/logging/conftest.py +++ b/tests/logging/conftest.py @@ -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) diff --git a/tests/posting/conftest.py b/tests/posting/conftest.py index 219dfa95..92f8fecd 100644 --- a/tests/posting/conftest.py +++ b/tests/posting/conftest.py @@ -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) diff --git a/tests/posting/test_threadsafety.py b/tests/posting/test_threadsafety.py index 65914ed9..1810917d 100644 --- a/tests/posting/test_threadsafety.py +++ b/tests/posting/test_threadsafety.py @@ -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: diff --git a/tests/primitives/test_conditions.py b/tests/primitives/test_conditions.py index 75919bd8..cf2be557 100644 --- a/tests/primitives/test_conditions.py +++ b/tests/primitives/test_conditions.py @@ -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)) @@ -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: diff --git a/tests/primitives/test_containers.py b/tests/primitives/test_containers.py index d289d129..b158b485 100644 --- a/tests/primitives/test_containers.py +++ b/tests/primitives/test_containers.py @@ -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) @@ -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() @@ -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: @@ -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) diff --git a/tests/reactor/conftest.py b/tests/reactor/conftest.py index af8e07db..28de7b93 100644 --- a/tests/reactor/conftest.py +++ b/tests/reactor/conftest.py @@ -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): @@ -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. @@ -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 diff --git a/tests/reactor/test_queueing.py b/tests/reactor/test_queueing.py index bebed44d..c892ceaf 100644 --- a/tests/reactor/test_queueing.py +++ b/tests/reactor/test_queueing.py @@ -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.