示例工程 18-asyncio-event-loop
18-asyncio-event-loop/01-serial-vs-gather/test_serial_vs_gather.py
"""test_serial_vs_gather.py"""
from __future__ import annotations
import asyncio
import pytest
from serial_vs_gather import concurrent, main, serial, work
def test_work_returns_name() -> None:
assert asyncio.run(work("x", 0.01)) == "x"
def test_gather_is_faster_than_serial() -> None:
serial_results, serial_time = asyncio.run(serial())
gather_results, gather_time = asyncio.run(concurrent())
assert serial_results == gather_results == ["a", "b", "c"]
assert serial_time >= 0.12
assert gather_time < 0.12
assert serial_time > gather_time * 1.8
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
asyncio.run(main())
out = capsys.readouterr().out
assert "serial: ['a', 'b', 'c']" in out
assert "speedup:" in out