import itertools import unittest from model import allocation, capacity, route, results class BurstTests(unittest.TestCase): def test_twelve_job_audit(self): rows, best = allocation(12, (4, 10)) self.assertEqual(len(rows), 13) self.assertEqual(best, [{"jobs_a": 9, "jobs_b": 3, "completion_ms": 36}]) self.assertEqual([capacity(t, (4, 10)) for t in (32, 35, 36)], [11, 11, 12]) def test_independent_completion_slot_oracle(self): # The Nth completion slot is a separate construction, not min/max enumeration. for a, b, n in itertools.product(range(1, 9), range(1, 9), range(1, 31)): slots = sorted([a * k for k in range(1, n + 1)] + [b * k for k in range(1, n + 1)]) optimum = allocation(n, (a, b))[1][0]["completion_ms"] self.assertEqual(optimum, slots[n - 1]) self.assertLess(capacity(optimum - 1, (a, b)), n) self.assertGreaterEqual(capacity(optimum, (a, b)), n) def test_all_labeled_twelve_job_assignments(self): durations = [] for choice in itertools.product((0, 1), repeat=12): finish = [0, 0] for server in choice: finish[server] += (4, 10)[server] durations.append(max(finish)) self.assertEqual(len(durations), 4096) self.assertEqual(min(durations), 36) self.assertEqual(sum(t == 36 for t in durations), 220) def test_waiting_time_does_not_reconcile_both_labels(self): self.assertEqual(max((6 - 1) * 4, (6 - 1) * 10), 50) self.assertEqual(max((9 - 1) * 4, (3 - 1) * 10), 32) self.assertNotEqual(50, 60) def test_eleven_job_control(self): self.assertEqual(allocation(11, (4, 10))[1], [{"jobs_a": 8, "jobs_b": 3, "completion_ms": 32}]) def test_client_partition_contrast(self): cases = results()["client_partitions"] self.assertEqual([r["counts"] for r in cases], [[9, 3], [10, 2], [9, 3], [8, 4], [12, 0], [12, 0]]) self.assertEqual([r["completion_ms"] for r in cases], [36, 40, 36, 40, 48, 48]) self.assertEqual(route([4, 4, 4], (4, 10), "local-count")["counts"], [6, 6]) self.assertEqual(route([1] * 12, (4, 10), "global-time")["counts"], [9, 3]) def test_trace_conservation_and_global_oracle(self): for n, clients, a, b in itertools.product(range(1, 13), (1, 2, 3), (1, 4), (2, 10)): bursts = [n] * clients for policy in ("local-count", "local-time", "global-time"): output = route(bursts, (a, b), policy) self.assertEqual(len(output["trace"]), n * clients) self.assertEqual(sum(output["counts"]), n * clients) self.assertEqual(output["counts"], [sum(t["server"] == s for t in output["trace"]) for s in (0, 1)]) if policy == "global-time": self.assertEqual(output["completion_ms"], allocation(n * clients, (a, b))[1][0]["completion_ms"]) def test_symmetry_scale_empty(self): for n in range(20): self.assertEqual(allocation(n, (4, 10))[1][0]["completion_ms"], allocation(n, (10, 4))[1][0]["completion_ms"]) self.assertEqual(allocation(n, (8, 20))[1][0]["completion_ms"], 2 * allocation(n, (4, 10))[1][0]["completion_ms"]) self.assertEqual(route([0], (4, 10), "local-count")["completion_ms"], 0) self.assertEqual(capacity(0, (4, 10)), 0) def test_invalid_inputs(self): for bad in (-1, True, 1.5, "12", None): with self.assertRaises(ValueError): allocation(bad, (4, 10)) with self.assertRaises(ValueError): capacity(bad, (4, 10)) for bad in ((), (4,), (4, 10, 20), (0, 10), (-4, 10), (True, 10), (4.0, 10)): with self.assertRaises(ValueError): allocation(12, bad) for bursts in ([], [-1], [True], [1.5], "12"): with self.assertRaises(ValueError): route(bursts, (4, 10), "local-time") with self.assertRaises(ValueError): route([1], (4, 10), "unknown") if __name__ == "__main__": unittest.main()