"""Tests on Lab 05's recorded runs (October 2026, Apple M4 Max, Docker Desktop with 16 vCPU,
Postgres 18.6): 02_deadlocks.py (one deadlock played exactly; 16 clients making random transfers
among 50 accounts for 20 s) and 05_pool_deadlock.py (a pool of 10 connections, requests that ask
it for a second one).
Run: pytest test_deadlocks_practice.py"""
import math

from deadlocks_practice import (burst, deadlocks_in_progress, failed_share, find_cycle, hang_threshold,
                                implied_hold_ms, lock_order, per_transfer, transfers_locking)

# 02, Exercise 2: (transfers, seconds, deadlocks counted by Postgres, transfers a second)
SENDER_FIRST = (6852, 20.023672125011217, 26, 342.1949758876289)
SENDER_FIRST_50MS = (36153, 20.022481540974695, 114, 1805.620343613016)
LOWEST_FIRST = (44863, 20.042366791982204, 0, 2238.408291078033)
UPDATE_SENDER_FIRST = (6486, 20.953996500000358, 26, 309.53522398459353)
UPDATE_LOWEST_FIRST = (58384, 20.02176287502516, 0, 2916.026943502927)

# 05, Exercise 1: requests at once -> finished within 5 s (pool of 10)
BURST = {1: 1, 5: 5, 9: 9, 10: 0, 11: 0, 20: 0, 50: 0}


def test_lock_order():
    assert tuple(lock_order(1, 2, False)) == (1, 2)
    assert tuple(lock_order(2, 1, False)) == (2, 1)  # sender first: the two transfers cross
    assert tuple(lock_order(2, 1, True)) == (1, 2)  # lowest number first: both start at 1
    assert tuple(lock_order(37, 4, True)) == (4, 37)


def test_find_cycle():
    assert sorted(find_cycle({"A": "B", "B": "A"})) == ["A", "B"]  # the one deadlock, played exactly
    assert find_cycle({"B": "A"}) == []  # B waits for A, A isn't waiting: just a queue
    assert find_cycle({}) == []
    assert sorted(find_cycle({"A": "B", "B": "C", "C": "A", "D": "A"})) == ["A", "B", "C"]  # D waits, not in it
    assert find_cycle({"A": "B", "B": "C"}) == []


def test_transfers_locking():
    # Exercise 1: A moves money 1 -> 2, B moves 2 -> 1, at the same moment.
    assert sorted(transfers_locking([(1, 2), (2, 1)], ordered=False)) == [0, 1]
    assert transfers_locking([(1, 2), (2, 1)], ordered=True) == []
    # Three transfers around a ring deadlock in request order; lowest first, never.
    ring = [(1, 2), (2, 3), (3, 1)]
    assert sorted(transfers_locking(ring, ordered=False)) == [0, 1, 2]
    assert transfers_locking(ring, ordered=True) == []


def test_deadlock_cost_under_load():
    n, s, d, _ = SENDER_FIRST
    assert round(deadlocks_in_progress(d, 1.0, s), 1) == 1.3  # "about one at any moment", an average
    n50, s50, d50, _ = SENDER_FIRST_50MS
    assert round(deadlocks_in_progress(d50, 0.05, s50), 2) == 0.28
    # Checking sooner: more deadlocks, but more transfers ran, and per transfer they were rarer
    assert round(n50 / n, 1) == 5.3
    assert round(100 * per_transfer(d, n), 2) == 0.38
    assert round(100 * per_transfer(d50, n50), 2) == 0.32
    # The fix: lowest number first
    assert round(LOWEST_FIRST[3] / SENDER_FIRST[3], 1) == 6.5
    assert round(UPDATE_LOWEST_FIRST[3] / UPDATE_SENDER_FIRST[3], 1) == 9.4
    assert LOWEST_FIRST[2] == UPDATE_LOWEST_FIRST[2] == 0


def test_hang_threshold():
    assert hang_threshold(10) == 10  # exactly the pool size
    assert hang_threshold(10, 3) == 5  # each holds 2 and wants a 3rd
    assert hang_threshold(10, 1) == math.inf  # one connection per request: it can't hang this way


def test_burst_matches_the_lab():
    for c, finished in BURST.items():
        assert burst(c, 10) == finished, c
    assert burst(50, 10, connections_per_request=1) == 50  # the flat version


def test_timeout_and_flat():
    # 05, Exercise 2: 1 s acquire timeout, 10 clients for 15 s: 32 finished, 118 got a 503
    assert round(100 * failed_share(32, 118)) == 79
    assert round(100 * failed_share(34, 266)) == 89  # 20 clients
    # Flat: 6,755 finished in 15 s with 20 clients, all 10 connections busy
    assert round(implied_hold_ms(10, 6755 / 15), 1) == 22.2
