Files
mandy/tests/test_gpu_test_timeout.py
NotRexed 00131bb85e main keeps its own runtime, tools and tests; the branch's variants stay on port/d3d12-nvidia
After the merge the tree held this branch's older states of files main maintains. Run against main's code,
tests/test_acphal_abi.py and 11 of the 15 translator tests that differed failed, and the C++ runtime would have
been built from two generations of its sources. So that the merge cannot break what main builds and tests:

- runtime/ is main's, file for file (68 files only this branch had are not carried over);
- every file under tools/, tests/ and docs/ that both sides have is main's (80 files);
- ten translator tests only this branch has, which test rules main's translator does not have, and their
  fixtures are left out.

What main gains: d3d12/ (the Rust D3D12 runtime), its launchers and helpers (Launcher.bat, scripts/play*.ps1,
play-linux.sh, port_ctl.py, smooth-motion.ps1, fetch_vkd3d_proton.py, sync_victory.py), mods/example_lua, eleven
notes under docs/, seven tools and the tests that pass with main's code, the README's sections and credits, and
the "Runtime traps" section of AGENTS.md. Everything left out here is on the branch port/d3d12-nvidia.

Checked: compileall; make_game.py --help; check_no_game_data.py over every file this adds to main; the
line-ending, docs-encoding, CI-registration and docs tests; each test file that differs from main, on its own.
Not run: the whole translator suite, the C++ build, cargo over d3d12/ after the merge (d3d12/ is byte for byte the
branch's), CI.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-27 17:58:38 -05:00

127 lines
5.4 KiB
Python

"""A GPU test program that hangs fails its own test instead of stalling the suite: run_gpu_tests.run_cmd and
check.run give each program LEONIDA_GPU_TEST_TIMEOUT seconds (default 120, 0 = no limit), then kill it. The hang
is a sleeping Python child, so no GPU is needed."""
from contextlib import redirect_stdout
import io
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import time
from types import SimpleNamespace
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
sys.path.insert(0, str(ROOT / "runtime" / "test" / "shaders"))
import check
import run_gpu_tests
HANG = [sys.executable, "-c", "import time; time.sleep(60)"]
QUICK = [sys.executable, "-c", "print('ok')"]
class Limit(unittest.TestCase):
def limit(self, value):
env = {k: v for k, v in os.environ.items() if k != "LEONIDA_GPU_TEST_TIMEOUT"}
if value is not None:
env["LEONIDA_GPU_TEST_TIMEOUT"] = value
with patch.dict(os.environ, env, clear=True):
return check.gpu_test_timeout()
def test_default_and_override(self):
self.assertEqual(self.limit(None), check.DEFAULT_GPU_TEST_TIMEOUT)
self.assertEqual(self.limit(""), check.DEFAULT_GPU_TEST_TIMEOUT)
self.assertEqual(self.limit(" 2.5 "), 2.5)
def test_zero_or_negative_means_no_limit(self):
self.assertIsNone(self.limit("0"))
self.assertIsNone(self.limit("-1"))
def test_non_numeric_or_non_finite_limit_is_an_error(self):
for bad in ("soon", "nan", "inf", "-inf", "1e9999"):
with self.subTest(bad=bad):
with self.assertRaises(SystemExit) as ctx:
self.limit(bad)
message = str(ctx.exception.code)
self.assertIn("LEONIDA_GPU_TEST_TIMEOUT", message)
self.assertIn(repr(bad), message)
class HungProgram(unittest.TestCase):
def setUp(self):
patcher = patch.dict(os.environ, {"LEONIDA_GPU_TEST_TIMEOUT": "0.5"})
patcher.start()
self.addCleanup(patcher.stop)
def assert_killed(self, call):
start = time.monotonic()
rc, msg = call()
self.assertLess(time.monotonic() - start, 30)
self.assertNotEqual(rc, 0)
self.assertIn("timed out after 0.5 s", msg)
self.assertIn(os.path.basename(sys.executable), msg)
def test_run_gpu_tests_run_cmd(self):
self.assert_killed(lambda: run_gpu_tests.run_cmd(HANG))
def test_check_run(self):
self.assert_killed(lambda: check.run(*HANG))
def test_texture_cases_use_the_timeout_from_the_shader_directory(self) -> None:
for name in ("ps_texture_sample", "ps_gather_wrap"):
with self.subTest(name=name), tempfile.TemporaryDirectory() as work:
args = SimpleNamespace(filter=f"{name}.asm", llvm_mc="llvm-mc", build=work)
spirv = Path(work, "spirv")
out = io.StringIO()
def fake_run(cmd: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
if cmd[0] == sys.executable:
(spirv / f"{name}.spv").touch()
return subprocess.CompletedProcess(cmd, 0, "", "")
self.assertEqual(Path(cmd[0]).stem, "vk_texture_test")
self.assertEqual(kwargs.get("cwd"), str(spirv))
self.assertEqual(kwargs.get("timeout"), 0.5)
raise subprocess.TimeoutExpired(cmd, 0.5)
with (patch.object(run_gpu_tests, "assemble_to_s",
side_effect=lambda _mc, _src, dst, _name: Path(dst).touch()),
patch.object(run_gpu_tests, "spirv_as"),
patch.object(run_gpu_tests, "bary_fallback", return_value=False),
patch.object(run_gpu_tests, "f16_interp_fallback", return_value=False),
patch.object(run_gpu_tests, "MESH_COMPUTE", False),
patch.object(run_gpu_tests.subprocess, "run", side_effect=fake_run),
redirect_stdout(out)):
self.assertEqual(run_gpu_tests.run_suite(args, work), 1)
self.assertRegex(out.getvalue(), rf"FAIL\s+{name}\s+.*timed out after 0\.5 s")
self.assertIn("0/1 passed", out.getvalue())
def test_quick_program_is_unaffected(self):
# A limit is still in force, but not the hang cases' 0.5 s: starting a Python child alone can take longer than
# that on a loaded Windows host, which killed the quick program and failed this test for the wrong reason.
with patch.dict(os.environ, {"LEONIDA_GPU_TEST_TIMEOUT": "60"}):
self.assertEqual(check.gpu_test_timeout(), 60)
self.assertEqual(run_gpu_tests.run_cmd(QUICK), (0, "ok"))
rc, out = check.run(*QUICK)
self.assertEqual((rc, out.strip()), (0, "ok"))
def test_translator_step_has_no_limit(self):
seen = {}
def fake_run(cmd, **kw):
seen.update(kw)
raise OSError("stop here")
with patch.object(run_gpu_tests.subprocess, "run", side_effect=fake_run):
run_gpu_tests.run_cmd(QUICK, gpu=False)
self.assertIsNone(seen["timeout"])
with patch.object(run_gpu_tests.subprocess, "run", side_effect=fake_run):
run_gpu_tests.run_cmd(QUICK)
self.assertEqual(seen["timeout"], 0.5)
if __name__ == "__main__":
unittest.main()