Browse Source

switch from asyncio.wait_for to pytest timeout in deadlock tests

`wait_for` was causing the test suite to hang completely when running with `-n auto`.
pull/15388/head
oliver 1 month ago
parent
commit
79dd0ebf53
  1. 6
      tests/test_concurrency.py
  2. 4
      tests/test_depends_deadlock.py

6
tests/test_concurrency.py

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

4
tests/test_depends_deadlock.py

@ -3,6 +3,7 @@ import threading
import time import time
from collections.abc import Iterator from collections.abc import Iterator
import pytest
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
from httpx import ASGITransport, AsyncClient from httpx import ASGITransport, AsyncClient
from pydantic import BaseModel from pydantic import BaseModel
@ -42,6 +43,7 @@ def get_deadlock(dep: None = Depends(release_resource)) -> Item:
# Fire off 100 requests in parallel(ish) in order to create contention # Fire off 100 requests in parallel(ish) in order to create contention
# over the shared resource (simulating a fastapi server that interacts with # over the shared resource (simulating a fastapi server that interacts with
# a database connection pool). # a database connection pool).
@pytest.mark.timeout(10, func_only=True)
def test_depends_deadlock() -> None: def test_depends_deadlock() -> None:
async def make_request(client: AsyncClient): async def make_request(client: AsyncClient):
await client.get("/deadlock") await client.get("/deadlock")
@ -53,4 +55,4 @@ def test_depends_deadlock() -> None:
tasks = [make_request(aclient) for _ in range(100)] tasks = [make_request(aclient) for _ in range(100)]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
asyncio.run(asyncio.wait_for(run_requests(), timeout=10)) asyncio.run(run_requests())

Loading…
Cancel
Save