Browse Source

Add timeout to anyio deadlock tests

This ensures that if an anyio deadlock issue is re-introduced the test suite will fail in a helpful way rather than just hanging.
pull/15388/head
oliver 1 month ago
parent
commit
f3a18003e6
  1. 7
      tests/test_concurrency.py
  2. 4
      tests/test_depends_deadlock.py

7
tests/test_concurrency.py

@ -1,3 +1,4 @@
import asyncio
import contextlib
import time
from collections.abc import Iterator
@ -40,7 +41,7 @@ async def test_contextmanager_in_threadpool() -> None:
@pytest.mark.usefixtures("reset_teardown_limiter")
async def test_competing_acquire_release() -> None:
"""Check that the main threadpool does not block the teardown threadpool."""
pool_size = anyio.to_thread.current_default_thread_limiter().total_tokens
pool_size = int(anyio.to_thread.current_default_thread_limiter().total_tokens)
acquirable = False
acquired = []
@ -63,6 +64,8 @@ async def test_competing_acquire_release() -> None:
# The threadpool should now be full of threads waiting to acquire
# The release function should be able to run without being blocked by acquires
await concurrency.run_in_teardown_threadpool(release)
await asyncio.wait_for(
concurrency.run_in_teardown_threadpool(release), timeout=10
)
assert len(acquired) == pool_size

4
tests/test_depends_deadlock.py

@ -11,7 +11,7 @@ from pydantic import BaseModel
mutex = threading.Lock()
# Simulate releaasing a pooled resource in the teardown of a Depends,
# Simulate releasing a pooled resource in the teardown of a Depends,
# which in reality is usually a database connection or similar.
def release_resource() -> Iterator[None]:
try:
@ -53,4 +53,4 @@ def test_depends_deadlock() -> None:
tasks = [make_request(aclient) for _ in range(100)]
await asyncio.gather(*tasks)
asyncio.run(run_requests())
asyncio.run(asyncio.wait_for(run_requests(), timeout=10))

Loading…
Cancel
Save