Files
smoovplay/tests/test_oslayer.py
bonzi b206fb2c31 Linux: OS layer, frame tap over POSIX shared memory, XDG folders, LUT path fix
- framegen/oslayer.py: config/cache/Videos folders (XDG on Linux, unchanged on
  Windows), the frame tap's clock (CLOCK_MONOTONIC ns, qpc_freq 1e9) and its
  shared memory (/dev/shm, mode 0600) with a futex wake-up
- frame tap (chiaki_tap/frametap.c, framegen/chiaki_tap.py, docs writer):
  Linux backend; layout unchanged apart from a wake word at offset 96 behind
  feature bit 16. Windows code path unchanged (cross-compiled with mingw)
- looks: LUT names and look ids lose folders on every OS. On Linux a shared
  look code could carry ..\\..\\x.cube through Path.name untouched
- Windows-only modules import on Linux (WINFUNCTYPE stand-in)
- winutil.boost_thread: SCHED_RR 10 (direct or via rtkit) on Linux
- glcuda: finds libcudart.so from torch's nvidia wheels
- tests: frame tap tests run on Linux; tests/test_oslayer.py

pytest on Linux (CPU torch): 370 passed, 0 failed (was 328 passed, 6 failed).
chiaki_tap/selftest.py passes on Linux (C writer vs Python reader).
2026-11-19 08:28:52 +00:00

124 lines
4.5 KiB
Python

"""framegen/oslayer.py: folders, the frame tap's clock and its shared memory, and look/LUT names that can't escape
their folder on any OS."""
import ctypes
import os
import sys
import threading
import time
from pathlib import Path
import pytest
from framegen import looks, oslayer
linux_only = pytest.mark.skipif(not sys.platform.startswith("linux"), reason="Linux behaviour")
@pytest.mark.parametrize("name, want", [
("Local\\chiaki-frametap", "chiaki-frametap"),
("Global\\smoovplay-tap", "smoovplay-tap"),
("Local\\a\\b/c", "a-b-c"),
("plain", "plain"),
])
def test_shm_names_match_frametap_c(name, want):
assert oslayer.shm_name(name) == want
@pytest.mark.parametrize("bad", ["", "Local\\", "Local\\..", ".", "x" * 201])
def test_unusable_shm_names_are_refused(bad):
with pytest.raises(ValueError):
oslayer.shm_name(bad)
@linux_only
def test_shared_memory_is_shared_and_private_to_this_user():
name = f"Local\\sp-oslayer-test-{os.getpid()}"
a, b = oslayer.shared_memory(name, 8192), oslayer.shared_memory(name, 8192)
try:
a[0:4] = b"ping"
assert b[0:4] == b"ping"
path = oslayer.SHM_DIR / oslayer.shm_name(name)
assert path.stat().st_mode & 0o777 == 0o600
finally:
a.close()
b.close()
oslayer.remove_shared_memory(name)
assert not (oslayer.SHM_DIR / oslayer.shm_name(name)).exists()
@linux_only
def test_clock_is_perf_counters():
# The tap converts arrival times with time.perf_counter() - (qpc() - q) / qpc_freq: both must be one clock.
assert oslayer.qpc_freq() == 1_000_000_000
q, t = oslayer.qpc() / 1e9, time.perf_counter()
assert abs(q - t) < 0.05
@linux_only
def test_futex_wakes_a_waiter_and_times_out_without_one():
word = ctypes.c_uint32(0)
t0 = time.perf_counter()
oslayer.futex_wait(ctypes.addressof(word), 0, 0.05)
assert 0.03 < time.perf_counter() - t0 < 1.0 # nobody woke it: the timeout
woke = []
def waiter():
oslayer.futex_wait(ctypes.addressof(word), 0, 5.0)
woke.append(time.perf_counter())
th = threading.Thread(target=waiter)
th.start()
time.sleep(0.05)
t_wake = time.perf_counter()
word.value = 1
oslayer.futex_wake(ctypes.addressof(word))
th.join(2)
assert woke and woke[0] - t_wake < 0.5
t0 = time.perf_counter()
oslayer.futex_wait(ctypes.addressof(word), 0, 5.0) # the word moved on already: no wait
assert time.perf_counter() - t0 < 0.1
@linux_only
def test_linux_folders_follow_xdg(monkeypatch, tmp_path):
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "cfg"))
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
assert oslayer.config_dir() == tmp_path / "cfg" / "smoovplay"
assert oslayer.cache_dir() == tmp_path / "cache" / "smoovplay"
monkeypatch.setenv("XDG_CONFIG_HOME", "relative/ignored") # the spec says to ignore relative values
assert oslayer.config_dir() == Path.home() / ".config" / "smoovplay"
@linux_only
def test_videos_folder_from_user_dirs(monkeypatch, tmp_path):
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setattr(Path, "home", lambda: tmp_path / "home")
assert oslayer.videos_dir() == tmp_path / "home" / "Videos"
(tmp_path / "user-dirs.dirs").write_text('XDG_DESKTOP_DIR="$HOME/Desktop"\nXDG_VIDEOS_DIR="$HOME/Filme"\n')
assert oslayer.videos_dir() == tmp_path / "home" / "Filme"
(tmp_path / "user-dirs.dirs").write_text('XDG_VIDEOS_DIR="$HOME/"\n') # "disabled" in xdg-user-dirs terms
assert oslayer.videos_dir() == tmp_path / "home" / "Videos"
@pytest.mark.parametrize("raw", ["..\\..\\secret\\grade.cube", "../../secret/grade.cube", "/etc/x/grade.cube",
"C:\\Users\\x\\grade.cube", "a\\b/grade.cube"])
def test_lut_names_lose_their_folders_on_every_os(raw):
assert looks._lut_name(raw) == "grade.cube"
@pytest.mark.parametrize("raw", ["..", ".", "x.png", "", None, "bad\0.cube"])
def test_lut_names_that_are_no_lut(raw):
assert looks._lut_name(raw) == ""
@pytest.mark.parametrize("look_id", ["my:../x", "my:..\\x", "my:..", "my:a/b", "my:", "builtin", "my:x\0y"])
def test_look_ids_cant_name_a_file_outside_the_folder(tmp_path, monkeypatch, look_id):
monkeypatch.setenv("SMOOVPLAY_LOOKS_DIR", str(tmp_path))
assert looks.Library().path(look_id) is None
def test_a_users_look_id_is_a_file_in_the_folder(tmp_path, monkeypatch):
monkeypatch.setenv("SMOOVPLAY_LOOKS_DIR", str(tmp_path))
assert looks.Library().path("my:golden") == tmp_path / "golden.json"