示例工程 35-asgi-fastapi

35-asgi-fastapi/03-def-vs-async-def/test_def_vs_async_def.py

python · 0.6 KB

"""test_def_vs_async_def.py"""
 
from __future__ import annotations
 
import asyncio
 
import pytest
 
pytest.importorskip("fastapi")
pytest.importorskip("httpx")
 
from def_vs_async_def import main, run_all
 
 
def test_def_routes_run_in_threadpool() -> None:
    sync_time, block_time, sleep_time = asyncio.run(run_all())
    assert sync_time < 0.12
    assert block_time > 0.12
    assert sleep_time < 0.12
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "async_block_serial: True" in out
    assert "async_sleep_concurrent: True" in out