Files
2026-10-07 13:06:47 +05:00

55 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
"""Автотесты лабораторной 6 (asyncio)."""
import asyncio
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from conftest import load_solution, source_of # noqa: E402
LAB = "lab06_asyncio"
def test_variant_number_set():
mod = load_solution(LAB)
assert 1 <= getattr(mod, "VARIANT_NUMBER", 0) <= 20, (
"Укажите свой VARIANT_NUMBER (1..20) в solution.py")
def test_fetch_all_async_correct_and_concurrent():
mod = load_solution(LAB)
results, seconds = asyncio.run(mod.fetch_all_async(8, 0.1))
assert results == [(i, i * i) for i in range(8)], (
"результаты должны быть (item, item*item) и упорядочены по item")
assert seconds < 0.5, (
f"8 запросов по 0.1 c заняли {seconds:.2f} c — конкурентности нет; "
"проверьте, что используется asyncio.sleep и await")
def test_fetch_all_limited_respects_limit():
mod = load_solution(LAB)
results, seconds = asyncio.run(mod.fetch_all_limited(10, 0.1, 2))
assert results == [(i, i * i) for i in range(10)]
assert seconds >= 0.45, (
f"10 запросов по 0.1 c при лимите 2 заняли {seconds:.2f} c "
"— лимит не соблюдается (должно быть ~0.5 c)")
# и без лимита было бы ~0.1 c
_, t_all = asyncio.run(mod.fetch_all_limited(10, 0.1, 10))
assert t_all < seconds * 0.5, "при большем лимите время должно заметно упасть"
def test_blocking_mistake_is_sequential():
"""time.sleep в корутине должен дать время ≈ items*delay."""
mod = load_solution(LAB)
seconds = mod.measure_blocking_mistake(5, 0.1)
assert seconds >= 0.45, (
f"заняло {seconds:.2f} c вместо ~0.5 c — time.sleep внезапно "
"конкурентен? Проверьте, что в _fetch_blocking именно time.sleep")
def test_uses_async_syntax():
src = source_of(LAB)
assert "async def" in src, "нужны корутины (async def)"
assert "asyncio.gather" in src, "нужен asyncio.gather"