示例工程 20-taskgroup-cancellation-contextvars
20-taskgroup-cancellation-contextvars/03-semaphore-throttle/test_semaphore_throttle.py
"""test_semaphore_throttle.py"""
from __future__ import annotations
import asyncio
import pytest
from semaphore_throttle import main, run
def test_peak_equals_limit() -> None:
results, peak = asyncio.run(run(20, 5))
assert results == list(range(20))
assert peak == 5
def test_custom_limit() -> None:
results, peak = asyncio.run(run(10, 3))
assert len(results) == 10
assert peak == 3
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
asyncio.run(main())
out = capsys.readouterr().out
assert "completed: 20" in out
assert "peak: 5" in out