import threading import unittest from kurgan.telemetry.readings import PowerReadings, Readings from kurgan.telemetry.service import Telemetry class FakeSource: """A cpu/gpu-shaped source: readings, plus the memory query the GPU one provides.""" def __init__(self): self.fail = False self.memory_fails = False def read(self, *args): if self.fail: raise OSError("device gone") return Readings(temp=50, load=10, clock=4000, power=30) def memory(self): if self.memory_fails: raise OSError("nvml busy") return (8 * 1024 ** 3, 24 * 1024 ** 3) class FakeSystem: def __init__(self): self.fail = False def read(self, cpu, gpu): if self.fail: raise OSError("commander gone") return PowerReadings(cpu=cpu.power, gpu=gpu.power, other=40, parts={"fans": 2.0}) class TelemetryHealthTest(unittest.TestCase): def setUp(self): self.cpu, self.gpu, self.system = FakeSource(), FakeSource(), FakeSystem() self.telemetry = Telemetry(self.cpu, self.gpu, self.system) def test_before_the_first_read_there_is_nothing_to_report(self): self.assertEqual(self.telemetry.snapshot(), {"seq": 0, "health": {}}) def test_a_healthy_run_has_no_health_entries(self): self.telemetry.read() self.assertEqual(self.telemetry.health(), {}) self.assertEqual(self.telemetry.snapshot()["health"], {}) def test_a_failing_source_costs_only_itself_and_is_reported(self): self.cpu.fail = True with self.assertLogs("kurgan.telemetry.service", level="WARNING"): sample = self.telemetry.read() health = self.telemetry.health() self.assertEqual(list(health), ["CPU sensors"]) self.assertIn("device gone", health["CPU sensors"]["error"]) self.assertIsNone(sample.cpu.temp) # the fallback stands in ... self.assertEqual(sample.gpu.temp, 50) # ... while every other source still reads self.assertEqual(sample.power.total, 70) # GPU 30 W + other 40 W; the CPU part is missing def test_recovery_clears_the_entry(self): self.gpu.fail = True with self.assertLogs("kurgan.telemetry.service", level="WARNING"): self.telemetry.read() self.assertEqual(list(self.telemetry.health()), ["GPU sensors"]) self.gpu.fail = False self.telemetry.read() self.assertEqual(self.telemetry.health(), {}) def test_a_source_that_goes_quiet_without_raising_is_reported(self): # GpuSensors returns empty readings when the driver is lost (and while it backs off) self.gpu.read = lambda *args: Readings() self.telemetry.read() self.telemetry.read() self.assertEqual(self.telemetry.health(), {}) # a gap or two proves nothing with self.assertLogs("kurgan.telemetry.service", level="WARNING"): self.telemetry.read() health = self.telemetry.health() self.assertEqual(list(health), ["GPU sensors"]) self.assertIn("driver unavailable", health["GPU sensors"]["error"]) del self.gpu.read # the driver is back with self.assertLogs("kurgan.telemetry.service", level="INFO"): self.telemetry.read() self.assertEqual(self.telemetry.health(), {}) def test_the_snapshot_carries_health(self): self.system.fail = True self.gpu.memory_fails = True first, stop = threading.Event(), threading.Event() self.telemetry.subscribe(lambda sample: first.set()) with self.assertLogs("kurgan.telemetry.service", level="WARNING") as logs: thread = threading.Thread(target=self.telemetry.run, args=(stop,), daemon=True) thread.start() self.assertTrue(first.wait(5)) # the listener fires after read(): both warnings are out stop.set() thread.join(5) self.assertEqual(len(logs.output), 2) # each failing source is reported once health = self.telemetry.snapshot()["health"] self.assertEqual(set(health), {"system power", "GPU memory"}) snapshot = self.telemetry.snapshot() self.assertIsNone(snapshot["gpu"]["vram_used"]) self.assertEqual(snapshot["gpu"]["temp"], 50) if __name__ == "__main__": unittest.main()