Skip to content

Commit

Permalink
Fixed test errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Feb 22, 2024
1 parent f1238c8 commit 23359e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
6 changes: 2 additions & 4 deletions 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

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions tests/test_context.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,)
Expand Down Expand Up @@ -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'")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
14 changes: 6 additions & 8 deletions 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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -206,15 +204,15 @@ 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.
"""
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,)
Expand Down

0 comments on commit 23359e7

Please sign in to comment.