The Kurgan 1.0.0: one process for the LCD, fans, lighting, local LLM servers and the web UI
This commit is contained in:
commit
f631386618
71 files changed
+14616
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from kurgan.lighting.gpu import (
|
||||
AsusGpuLeds, GpuLighting, NvidiaI2c, gpu_chase_frame,
|
||||
)
|
||||
from kurgan.lighting.case_fans import ChaseTiming, side_chase_color
|
||||
|
||||
|
||||
class FakeBus(NvidiaI2c):
|
||||
def __init__(self, firmware=b"AUMA0-E6K5-1110"):
|
||||
self.register = 0
|
||||
self.values = {0x1000 + i: value for i, value in enumerate(firmware.ljust(16, b"\0"))}
|
||||
self.values[0x1C03] = 4
|
||||
self.values[0x8020] = 0
|
||||
self.writes = []
|
||||
|
||||
def transfer(self, command, data, *, read=False):
|
||||
if command == 0:
|
||||
self.register = int.from_bytes(data, "big")
|
||||
elif read:
|
||||
return bytes((self.values.get(self.register, 0),))
|
||||
else:
|
||||
self.writes.append((self.register, command, data))
|
||||
return data
|
||||
|
||||
|
||||
class GpuTransportTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.conflict = patch("kurgan.lighting.gpu.corsair_software_running", return_value=False)
|
||||
self.conflict.start()
|
||||
self.addCleanup(self.conflict.stop)
|
||||
self.processes = patch("kurgan.lighting.gpu._process_names", return_value=[])
|
||||
self.processes.start()
|
||||
self.addCleanup(self.processes.stop)
|
||||
|
||||
def test_direct_colors_use_rbg_order_skip_unchanged_and_restore_without_saving(self):
|
||||
bus = FakeBus()
|
||||
driver = AsusGpuLeds(lambda: bus)
|
||||
driver.open()
|
||||
self.assertEqual(bus.writes, [])
|
||||
colors = ((24, 0, 0), (255, 48, 0), (24, 0, 0), (24, 0, 0))
|
||||
driver.show_frame(colors)
|
||||
self.assertEqual(bus.writes[1], (0x8103, 3, bytes((3, 255, 0, 48))))
|
||||
self.assertEqual(bus.writes[-2:], [(0x8020, 1, b"\x01"), (0x80A0, 1, b"\x01")])
|
||||
bus.writes.clear()
|
||||
driver.show_frame(colors)
|
||||
self.assertEqual(bus.writes, [])
|
||||
driver.show_frame((colors[0], colors[0], colors[2], colors[3]))
|
||||
self.assertEqual(bus.writes, [(0x8103, 3, bytes((3, 24, 0, 0)))])
|
||||
driver.close()
|
||||
self.assertEqual(bus.writes[-2:], [(0x8020, 1, b"\x00"), (0x80A0, 1, b"\x01")])
|
||||
self.assertFalse(any(data == b"\xaa" for _, _, data in bus.writes))
|
||||
|
||||
def test_unknown_firmware_and_invalid_frames_do_not_write_lighting_registers(self):
|
||||
bus = FakeBus(b"UNKNOWN")
|
||||
driver = AsusGpuLeds(lambda: bus)
|
||||
with self.assertRaises(OSError):
|
||||
driver.open()
|
||||
self.assertEqual(bus.writes, [])
|
||||
bus = FakeBus()
|
||||
driver = AsusGpuLeds(lambda: bus)
|
||||
driver.open()
|
||||
with self.assertRaises(ValueError):
|
||||
driver.show_frame(((256, 0, 0),) * 4)
|
||||
self.assertEqual(bus.writes, [])
|
||||
|
||||
|
||||
class GpuChaseTest(unittest.TestCase):
|
||||
def test_row_reverses_at_each_end_once_per_shared_cycle(self):
|
||||
for count in (4, 30):
|
||||
route_length = 2 * (count - 1)
|
||||
heads = []
|
||||
for step in range(route_length + 1):
|
||||
phase = 6 + step * 36 / route_length + 0.000001
|
||||
frame = gpu_chase_frame(phase, count)
|
||||
heads.append(frame.index(side_chase_color(1)))
|
||||
self.assertEqual(heads, list(range(count - 1, -1, -1)) + list(range(1, count)))
|
||||
self.assertEqual(gpu_chase_frame(6, count), gpu_chase_frame(42, count))
|
||||
|
||||
def test_worker_joins_shared_clock_caps_writes_and_releases_on_off(self):
|
||||
clock = ChaseTiming()
|
||||
clock.reset(6)
|
||||
clock.set_power(550)
|
||||
driver = Mock()
|
||||
driver.led_count = 4
|
||||
lighting = GpuLighting(clock, lambda: driver)
|
||||
writes = []
|
||||
for tick in range(101):
|
||||
now = 100 + tick * 0.01
|
||||
before = driver.show_frame.call_count
|
||||
with patch("kurgan.lighting.gpu.time.monotonic", return_value=now):
|
||||
lighting.update(True)
|
||||
if driver.show_frame.call_count != before:
|
||||
writes.append(now)
|
||||
self.assertAlmostEqual(clock.phase, 46)
|
||||
self.assertTrue(all(b - a >= 0.045 for a, b in zip(writes, writes[1:])))
|
||||
lighting.update(False)
|
||||
driver.close.assert_called_once()
|
||||
self.assertEqual(lighting.status, "off")
|
||||
|
||||
def test_connection_failure_is_visible_and_retries_after_backoff(self):
|
||||
driver = Mock()
|
||||
driver.open.side_effect = OSError("GPU busy")
|
||||
factory = Mock(return_value=driver)
|
||||
lighting = GpuLighting(ChaseTiming(), factory)
|
||||
with patch("kurgan.lighting.gpu.time.monotonic", return_value=100), \
|
||||
self.assertLogs("kurgan.lighting.gpu", level="WARNING"):
|
||||
lighting.update(True)
|
||||
with patch("kurgan.lighting.gpu.time.monotonic", return_value=105):
|
||||
lighting.update(True)
|
||||
factory.assert_called_once()
|
||||
self.assertEqual(lighting.status, "unavailable: GPU busy")
|
||||
Reference in new issue
Block a user