"""Independent bounded checks for latency.py; no third-party packages.""" import hashlib import json from pathlib import Path import unittest from latency import ARRIVALS, evidence, replay def frontier(arrivals, delay): # Tick-based enumeration branches at completions, unlike prescribed replay. requests = dict(arrivals) states = {(None, (), 0, ())} for t in range(arrivals[-1][0] + 1): following = set() for resident, pending, total, hits in states: due = [obj for obj, end in pending if end == t] if len(due) > 1: raise ValueError("this oracle assumes one request per tick and fixed delay") remaining = tuple((obj, end) for obj, end in pending if end > t) choices = (resident, *due) for current in choices: if t not in requests: following.add((current, remaining, total, hits)) continue obj = requests[t] if obj == current: following.add((current, remaining, total, hits + (t,))) else: due_time = dict(remaining).get(obj) new_pending = remaining if due_time is None: due_time = t + delay new_pending = tuple(sorted((*remaining, (obj, due_time)))) following.add((current, new_pending, total + due_time - t, hits)) states = following return {(total, hits) for _, _, total, hits in states} class LatencyTests(unittest.TestCase): def test_full_witness(self): low, high = evidence()["rows"] self.assertEqual((low["totalWaitMs"], high["totalWaitMs"]), (45, 60)) self.assertEqual((low["residentHits"], high["residentHits"]), (10, 11)) self.assertEqual((low["joinedRequests"], high["joinedRequests"]), (5, 4)) self.assertEqual((low["backendFetches"], high["backendFetches"]), (3, 3)) self.assertEqual((low["requests"], high["requests"]), (18, 18)) for a, b in zip(low["events"], high["events"]): self.assertEqual((a["arrivalMs"], a["object"]), (b["arrivalMs"], b["object"])) if a["arrivalMs"] != 10: self.assertEqual(a["kind"] == "resident", b["kind"] == "resident") self.assertEqual([e["waitMs"] for e in low["events"][:8]], [10, 10, 10, 5, 4, 3, 2, 1]) self.assertEqual([e["waitMs"] for e in high["events"][:8]], [10, 10, 0, 10, 9, 8, 7, 6]) def test_independent_tick_frontier(self): possibilities = frontier(ARRIVALS, 10) self.assertIn((45, tuple(range(30, 40))), possibilities) self.assertIn((60, (10, *range(30, 40))), possibilities) self.assertEqual(min(total for total, hits in possibilities), 45) self.assertEqual(min(total for total, hits in possibilities if 10 in hits), 60) def test_completion_before_arrival(self): result = replay([(0, "A"), (9, "A"), (10, "A")], 10, [0]) self.assertEqual([e["waitMs"] for e in result["events"]], [10, 1, 0]) self.assertEqual([e["kind"] for e in result["events"]], ["fetch", "joined", "resident"]) bypass = replay([(0, "A"), (10, "A")], 10, []) self.assertEqual(bypass["backendFetches"], 2) def test_bypass_and_multiple_completions_between_requests(self): result = replay([(0, "A"), (1, "B"), (20, "A"), (21, "B")], 10, [0, 1]) self.assertEqual([e["kind"] for e in result["events"]], ["fetch", "fetch", "fetch", "resident"]) def test_waiting_area(self): for row in evidence()["rows"]: area = sum(sum(e["arrivalMs"] <= t < e["responseMs"] for e in row["events"]) for t in range(50)) self.assertEqual(area, row["totalWaitMs"]) self.assertEqual(row["requests"], row["residentHits"] + row["joinedRequests"] + row["backendFetches"]) self.assertEqual(sum(e["waitMs"] for e in row["events"]), row["totalWaitMs"]) def test_invalid_inputs(self): for arrivals, delay, admissions in [ ([(0, "A"), (0, "B")], 10, []), ([(1, "A"), (0, "B")], 10, []), ([(-1, "A")], 10, []), ([(True, "A")], 10, []), ([(0, "")], 10, []), ([(0, 1)], 10, []), ([(0,)], 10, []), ([(0, "A")], True, []), ([(0, "A")], 0, []), ([(0, "A")], 101, []), ([(0, "A")], 10, [1]), ([(0, "A")], 10, [False]), ([(0, "A")], 10, [0, 0]), ([(0, "A"), (1, "A")], 10, [1]), ([(1025, "A")], 10, []), ([(t, "A") for t in range(65)], 10, []), ]: with self.subTest(arrivals=arrivals, delay=delay, admissions=admissions): with self.assertRaises(ValueError): replay(arrivals, delay, admissions) def test_empty_and_input_preservation(self): arrivals = list(ARRIVALS) admissions = [1] self.assertEqual(replay([], 10, [])["requests"], 0) replay(arrivals, 10, admissions) self.assertEqual(arrivals, ARRIVALS) self.assertEqual(admissions, [1]) def test_published_evidence(self): source = Path(__file__).with_name("latency.py") stored = json.loads(source.with_name("latency.json").read_text()) fresh = json.loads(json.dumps(evidence())) fresh["generatedAt"] = stored["generatedAt"] self.assertEqual(stored, fresh) self.assertEqual(stored["modelSha256"], hashlib.sha256(source.read_bytes()).hexdigest()) if __name__ == "__main__": unittest.main()