Files
parallel-programming/tests/test_lab07.py
T
2026-10-07 13:06:47 +05:00

57 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
"""Автотесты лабораторной 7 (сводная).
solution.py запускается подпроцессом с LAB_SMOKE=1 (как в лабе 4),
т.к. в решении используется multiprocessing.
"""
import json
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
LAB_DIR = "lab07_comparison"
def run_smoke():
env = dict(os.environ)
env["LAB_SMOKE"] = "1"
env["PYTHONIOENCODING"] = "utf-8"
proc = subprocess.run(
[sys.executable, str(ROOT / LAB_DIR / "solution.py")],
capture_output=True, text=True, env=env, timeout=120,
cwd=str(ROOT))
if proc.returncode != 0:
raise AssertionError(
f"solution.py упал:\n--- stdout ---\n{proc.stdout}\n--- stderr ---\n{proc.stderr}")
for line in proc.stdout.splitlines():
if line.startswith("LAB_SMOKE_JSON:"):
return json.loads(line[len("LAB_SMOKE_JSON:"):])
raise AssertionError(f"нет LAB_SMOKE_JSON. stdout:\n{proc.stdout}")
def test_smoke_all_approaches_work():
out = run_smoke()
assert out.get("ok") is True
assert out.get("cpu_ok") is True, (
"CPU-подходы дают неверный checksum (потоки и/или процессы)")
assert out.get("io_ok") is True, "I/O-подходы вернули неверный объём результатов"
def test_smoke_io_times_present():
out = run_smoke()
times = out.get("io_times", {})
assert all(k in times for k in ("seq", "threads", "async")), times
assert all(t >= 0 for t in times.values())
def test_uses_all_four_models():
src = (ROOT / LAB_DIR / "solution.py").read_text(encoding="utf-8")
assert "ThreadPoolExecutor" in src, "нужны потоки"
assert "Pool" in src or "ProcessPoolExecutor" in src, "нужны процессы"
assert "asyncio" in src and "gather" in src, "нужен asyncio"
assert 'if __name__ == "__main__"' in src, "нужен __main__-guard"