示例工程 19-threading-gil-multiprocessing

19-threading-gil-multiprocessing/01-cpu-bound-threads/test_cpu_bound_threads.py

python · 0.6 KB

"""test_cpu_bound_threads.py"""
 
from __future__ import annotations
 
import pytest
from cpu_bound_threads import burn, main, serial, threaded
 
 
def test_burn_value() -> None:
    assert burn(10) == 45
 
 
def test_threads_do_not_speed_up_cpu_work() -> None:
    serial_results, serial_time = serial()
    threaded_results, threaded_time = threaded()
    assert serial_results == threaded_results
    assert threaded_time > serial_time * 0.5
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "serial: 499999500000" in out
    assert "speedup:" in out