Browse Source

fix AsyncClient usage

pull/12066/head
svlandeg 8 months ago
parent
commit
a75414d733
  1. 8
      tests/test_depends_deadlock.py

8
tests/test_depends_deadlock.py

@ -3,8 +3,8 @@ import threading
import time import time
from typing import Generator from typing import Generator
import httpx
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
from httpx import ASGITransport, AsyncClient
from pydantic import BaseModel from pydantic import BaseModel
app = FastAPI() app = FastAPI()
@ -66,11 +66,13 @@ def get_deadlock(db: MyDB = Depends(get_db)):
# able to free the resource without deadlocking, allowing each request to # able to free the resource without deadlocking, allowing each request to
# be handled timely # be handled timely
def test_depends_deadlock_patch(): def test_depends_deadlock_patch():
async def make_request(client: httpx.AsyncClient): async def make_request(client: AsyncClient):
await client.get("/deadlock") await client.get("/deadlock")
async def run_requests(): async def run_requests():
async with httpx.AsyncClient(app=app, base_url="http://testserver") as aclient: async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://testserver"
) as aclient:
tasks = [make_request(aclient) for _ in range(100)] tasks = [make_request(aclient) for _ in range(100)]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)

Loading…
Cancel
Save