"""Tests on Lab 02's freezing service (GET /stall/k/{id}: frozen for 1 s of every 10 s) and its
recorded run (October 2026, Apple M4 Max): the known answer, three testers against it, and the
checks on k6. Run: pytest test_load_practice.py"""
from load_practice import (can_trust, closed_loop, corrected, in_freeze, known_answer, latencies,
                           open_loop, paced_loop, percentile, share_slower, vus_needed, wait_at)


def test_known_answer():
    assert round(known_answer(99), 3) == 0.9  # 1 in 100 waits 900 ms or more
    assert round(known_answer(99.9), 3) == 0.99
    assert known_answer(50) == 0 and round(known_answer(90), 6) == 0  # 9 in 10 never meet a freeze
    assert round(known_answer(95), 3) == 0.5


def test_share_slower():
    assert round(share_slower(0.1), 4) == 0.09  # 9% of requests slower than 100 ms
    assert round(share_slower(0), 4) == 0.1  # a tenth land in a freeze
    assert share_slower(1.0) == 0 and share_slower(2.0) == 0


def test_wait_and_freeze():
    assert wait_at(0) == 1.0  # arrive as a freeze starts: wait the full second
    assert round(wait_at(20.5), 6) == 0.5  # halfway through the third freeze
    assert wait_at(1.0) == 0 and wait_at(9.99) == 0
    assert in_freeze(30.2) and not in_freeze(31.5)


def test_closed_loop_misses_the_freeze():
    reqs = closed_loop(4, 60)  # 4 workers for a minute, 1 ms a request
    lat = latencies(reqs, "sent")
    # it ran flat out: far more than 500 a second, a rate nobody chose
    assert len(reqs) / 60 > 3000
    # each worker got stuck once per freeze, and sent nothing else during it
    assert sum(in_freeze(s) for s, _ in reqs) == 4 * 6
    assert round(max(lat), 3) == 1.001  # the max is the only hint
    assert percentile(lat, 99) < 0.0011  # p99 ~1 ms against a known 900 ms


def test_open_loop_finds_it():
    reqs = open_loop(500, 60)
    lat = latencies(reqs, "due")
    assert len(reqs) == 30_000
    assert sum(in_freeze(d) for d, _, _ in reqs) == 3_000  # a tenth of the requests
    assert abs(percentile(lat, 99) - 0.9) < 0.005  # the lab measured 925.5 ms
    assert abs(sum(x > 0.1 for x in lat) / len(lat) - 0.09) < 0.002  # the lab: 9.9%


def test_paced_depends_on_the_clock():
    reqs = paced_loop(4, 500, 60)  # one request every 8 ms per worker
    assert len(reqs) == 30_000
    due_in_freeze = sum(in_freeze(d) for d, _, _ in reqs)
    sent_in_freeze = sum(in_freeze(s) for _, s, _ in reqs)
    assert due_in_freeze == 3_000 and sent_in_freeze == 4 * 6  # due there, but not sent there
    from_sent = percentile(latencies(reqs, "sent"), 99)
    from_due = percentile(latencies(reqs, "due"), 99)
    assert from_sent < 0.0011  # the lab: 3.3 ms
    assert abs(from_due - 0.9) < 0.02  # the lab: 919.5 ms, the same run timed from when it was due


def test_correction():
    # one request stuck for a whole 1 s freeze, from a client meaning to send every 8 ms
    assert len(corrected([1_000_000], 8_000)) == 125  # it + the 124 it should have sent
    assert corrected([20_000], 8_000) == [20_000, 12_000]
    assert corrected([5_000, 7_999], 8_000) == [5_000, 7_999]  # nothing under the interval


def test_k6_checks():
    assert vus_needed(500, 1.0) == 500  # 500 a second x a 1 s freeze
    assert vus_needed(1000, 0.0021) == 3  # a normal p99 needs few
    assert can_trust(0)
    assert not can_trust(2874)  # k6 with 20 virtual users: p99 2.0 ms, and 2,874 dropped
    # nearly all the drops were due during freezes: 6 freezes x ~0.96 s x 500 a second
    assert abs(6 * 0.96 * 500 - 2874) < 10
