"""Tests for CPU isolation and core-spec parsing.""" from __future__ import annotations import os from wikikb import config, cpu def test_parse_core_spec() -> None: assert config._parse_core_spec("0-3", 24) == {0, 1, 2, 3} assert config._parse_core_spec("8,10-11", 24) == {8, 10, 11} assert config._parse_core_spec("", 24) == set() assert config._parse_core_spec("20-100", 24) == {20, 21, 22, 23} # clamped def test_allowed_cores_excludes_reserved() -> None: assert config._allowed_cores(8, {0, 1}) == [2, 3, 4, 5, 6, 7] def test_allowed_cores_falls_back_when_all_reserved() -> None: assert config._allowed_cores(4, {0, 1, 2, 3}) == [0, 1, 2, 3] def test_isolate_sets_affinity_and_nice(monkeypatch) -> None: seen: dict[str, object] = {} monkeypatch.setattr( os, "sched_setaffinity", lambda pid, cores: seen.__setitem__("aff", (pid, set(cores))) ) monkeypatch.setattr(os, "nice", lambda inc: seen.__setitem__("nice", inc)) assert cpu.isolate(allowed=[4, 5, 6], nice_increment=7) == [4, 5, 6] assert seen["aff"] == (0, {4, 5, 6}) assert seen["nice"] == 7 def test_isolate_defaults_use_config(monkeypatch) -> None: seen: dict[str, object] = {} monkeypatch.setattr( os, "sched_setaffinity", lambda _pid, cores: seen.__setitem__("c", set(cores)) ) monkeypatch.setattr(os, "nice", lambda inc: seen.__setitem__("n", inc)) assert cpu.isolate() == list(config.ALLOWED_CORES) assert seen["c"] == set(config.ALLOWED_CORES) assert seen["n"] == config.NICE_INCREMENT def test_isolate_no_affinity_and_zero_nice(monkeypatch) -> None: monkeypatch.delattr(os, "sched_setaffinity", raising=False) called = {"nice": False} monkeypatch.setattr(os, "nice", lambda _inc: called.__setitem__("nice", True)) # nice_increment 0 -> nice() skipped; missing sched_setaffinity -> affinity skipped assert cpu.isolate(allowed=[0], nice_increment=0) == [0] assert called["nice"] is False