"""Scheduling headroom, including small PCs and restricted process CPU capacity.""" import sys import unittest from pathlib import Path from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import cpu_budget class CpuBudgetTest(unittest.TestCase): def test_reserves_headroom_and_caps_workers(self) -> None: for cpus, expected in [(1, 1), (2, 1), (3, 1), (4, 2), (6, 4), (8, 4), (16, 4), (64, 4)]: with self.subTest(cpus=cpus): self.assertEqual(cpu_budget.inference_cpu_threads(cpus), expected) def test_unknown_capacity_uses_one_worker(self) -> None: for cpus in (None, 0, -1): with self.subTest(cpus=cpus): self.assertEqual(cpu_budget.inference_cpu_threads(cpus), 1) def test_prefers_process_capacity_when_available(self) -> None: with mock.patch.object(cpu_budget.os, "process_cpu_count", return_value=4, create=True): with mock.patch.object(cpu_budget.os, "cpu_count", return_value=64): self.assertEqual(cpu_budget.local_inference_cpu_threads(), 2) if __name__ == "__main__": unittest.main()