From 23359e76217ace5879bef1e9e4dd84ecd58f16f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Fri, 23 Feb 2024 00:55:58 +0200 Subject: [PATCH] Fixed test errors and warnings --- tests/test_concurrent.py | 6 ++---- tests/test_context.py | 22 ++++++++++++---------- tests/test_event.py | 14 ++++++-------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/test_concurrent.py b/tests/test_concurrent.py index 85dc36ce..3b62275d 100644 --- a/tests/test_concurrent.py +++ b/tests/test_concurrent.py @@ -1,6 +1,5 @@ from __future__ import annotations -from asyncio import AbstractEventLoop from concurrent.futures import Executor, ThreadPoolExecutor from threading import Thread, current_thread @@ -39,7 +38,7 @@ def check_thread(ctx: Context) -> None: @pytest.mark.asyncio -async def test_executor_default(event_loop: AbstractEventLoop, context: Context) -> None: +async def test_executor_default(context: Context) -> None: @executor def check_thread(ctx: Context) -> None: assert current_thread() is not event_loop_thread @@ -51,7 +50,6 @@ def check_thread(ctx: Context) -> None: @pytest.mark.asyncio async def test_executor_worker_thread( - event_loop: AbstractEventLoop, context: Context, special_executor: ThreadPoolExecutor, ) -> None: @@ -74,7 +72,7 @@ def runs_in_default_worker(ctx: Context) -> str: @pytest.mark.asyncio -async def test_executor_missing_context(event_loop: AbstractEventLoop, context: Context) -> None: +async def test_executor_missing_context(context: Context) -> None: @executor("special") def runs_in_default_worker() -> None: pass diff --git a/tests/test_context.py b/tests/test_context.py index c8856fd7..b2551042 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -2,6 +2,7 @@ import asyncio import sys +from asyncio import get_running_loop from collections.abc import Callable from concurrent.futures import Executor, ThreadPoolExecutor from inspect import isawaitable @@ -176,9 +177,9 @@ def test_contextmanager_exception(self, context, event_loop): assert exc.value is exception @pytest.mark.asyncio - async def test_async_contextmanager_exception(self, event_loop, context): + async def test_async_contextmanager_exception(self, context): """Test that "async with context:" calls close() with the exception raised in the block.""" - close_future = event_loop.create_future() + close_future = get_running_loop().create_future() close_future.set_result(None) exception = Exception("foo") with patch.object(context, "close", return_value=close_future) as close: @@ -191,9 +192,9 @@ async def test_async_contextmanager_exception(self, event_loop, context): @pytest.mark.parametrize("types", [int, (int,), ()], ids=["type", "tuple", "empty"]) @pytest.mark.asyncio - async def test_add_resource(self, context, event_loop, types): + async def test_add_resource(self, context, types): """Test that a resource is properly added in the context and listeners are notified.""" - event_loop.call_soon(context.add_resource, 6, "foo", None, types) + get_running_loop().call_soon(context.add_resource, 6, "foo", None, types) event = await context.resource_added.wait_event() assert event.resource_types == (int,) @@ -231,7 +232,7 @@ def test_add_resource_context_attr_conflict(self, context: Context) -> None: """ context.a = 2 - with pytest.raises(ResourceConflict) as exc, pytest.deprecated_call(): + with pytest.raises(ResourceConflict) as exc: context.add_resource(2, context_attr="a") exc.match("this context already has an attribute 'a'") @@ -327,7 +328,7 @@ async def test_add_resource_factory_context_attr_conflict(self, context: Context with pytest.deprecated_call(): context.add_resource_factory(lambda ctx: None, str, context_attr="foo") - with pytest.raises(ResourceConflict) as exc, pytest.deprecated_call(): + with pytest.raises(ResourceConflict) as exc: await context.add_resource_factory(lambda ctx: None, str, context_attr="foo") exc.match( @@ -434,13 +435,14 @@ def test_require_resource_not_found(self, context: Context) -> None: assert exc.value.name == "foo" @pytest.mark.asyncio - async def test_request_resource_parent_add(self, context, event_loop): + async def test_request_resource_parent_add(self, context): """ Test that adding a resource to the parent context will satisfy a resource request in a child context. """ async with context, Context() as child_context: + event_loop = get_running_loop() task = event_loop.create_task(child_context.request_resource(int)) event_loop.call_soon(context.add_resource, 6) resource = await task @@ -785,17 +787,17 @@ def test_explicit_parent_deprecation() -> None: @pytest.mark.asyncio -async def test_context_stack_corruption(event_loop): +async def test_context_stack_corruption(): async def generator() -> AsyncGenerator: async with Context(): yield gen = generator() - await event_loop.create_task(gen.asend(None)) + await get_running_loop().create_task(gen.asend(None)) async with Context() as ctx: with pytest.warns(UserWarning, match="Potential context stack corruption detected"): try: - await event_loop.create_task(gen.asend(None)) + await get_running_loop().create_task(gen.asend(None)) except StopAsyncIteration: pass diff --git a/tests/test_event.py b/tests/test_event.py index 4d9f3e90..ebe8aded 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -1,7 +1,7 @@ from __future__ import annotations import gc -from asyncio import AbstractEventLoop, Queue, all_tasks, current_task +from asyncio import Queue, all_tasks, current_task, get_running_loop from datetime import datetime, timedelta, timezone from typing import NoReturn @@ -104,9 +104,7 @@ async def test_dispatch_raw(self, source: DummySource) -> None: assert events == [event] @pytest.mark.asyncio - async def test_dispatch_log_exceptions( - self, event_loop: AbstractEventLoop, source: DummySource, caplog - ) -> None: + async def test_dispatch_log_exceptions(self, source: DummySource, caplog) -> None: """Test that listener exceptions are logged and that dispatch() resolves to ``False``.""" def listener(event) -> NoReturn: @@ -156,8 +154,8 @@ async def test_dispatch_raw_class_mismatch(self, source: DummySource) -> None: assert str(exc.value) == "event must be of type test_event.DummyEvent" @pytest.mark.asyncio - async def test_wait_event(self, source: DummySource, event_loop) -> None: - event_loop.call_soon(source.event_a.dispatch) + async def test_wait_event(self, source: DummySource) -> None: + get_running_loop().call_soon(source.event_a.dispatch) received_event = await source.event_a.wait_event() assert received_event.topic == "event_a" @@ -206,7 +204,7 @@ class SignalOwner: ids=["nofilter", "filter"], ) @pytest.mark.asyncio -async def test_wait_event(event_loop, filter, expected_value) -> None: +async def test_wait_event(filter, expected_value) -> None: """ Test that wait_event returns the first event matched by the filter, or the first event if there is no filter. @@ -214,7 +212,7 @@ async def test_wait_event(event_loop, filter, expected_value) -> None: """ source1 = DummySource() for i in range(1, 4): - event_loop.call_soon(source1.event_a.dispatch, i) + get_running_loop().call_soon(source1.event_a.dispatch, i) event = await wait_event([source1.event_a], filter) assert event.args == (expected_value,)