okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-qatui (final d33d45ade) into f-0213-intb247f4ecd

main
3.5 KiB88 linesraw
1"""Round 0213 QATUI: the Windows display-adapter CIM query is asked once per process, not once per caller (#4975 family).
2
3Found on the owner PC (packaged Windows Terminal, `strata launch --plan`): the same PowerShell `Get-CimInstance Win32_VideoController` command ran 18 times in one
4command (26 child processes, 17 s). With PowerShell/WMI stuck (a fake `powershell` that never answers) the plan was still running after 300 s: 18 times the
5query's 15 s timeout, far beyond the 60 s the app waits for Control. After the change a stuck query costs its timeout once.
6
7    python -m pytest tests/terminal/test_cim_cache_0213_qatui.py
8"""
9from __future__ import annotations
10
11import subprocess
12import sys
13import unittest
14from pathlib import Path
15from unittest import mock
16
17ROOT = Path(__file__).resolve().parents[2]
18sys.path.insert(0, str(ROOT))
19
20from strata_control import engine_component as ec  # noqa: E402
21
22GOOD = "PCI\\VEN_10DE&DEV_2F04&SUBSYS_1|NVIDIA GeForce RTX 5070|12884901888\nPCI\\VEN_1002&DEV_164E&SUBSYS_2|AMD Radeon(TM) Graphics|536870912\n"
23
24
25class CimCache(unittest.TestCase):
26    def setUp(self):
27        ec._CIM_CACHE.clear()
28        self.addCleanup(ec._CIM_CACHE.clear)
29
30    def _windows(self, run):
31        return (mock.patch.object(ec.os, "name", "nt"), mock.patch.object(ec.shutil, "which", lambda name: "powershell"),
32                mock.patch.object(ec.subprocess, "run", run))
33
34    def test_many_callers_ask_powershell_once(self):
35        calls = []
36
37        def run(*a, **k):
38            calls.append(a)
39            return subprocess.CompletedProcess(a, 0, GOOD, "")
40        p1, p2, p3 = self._windows(run)
41        with p1, p2, p3:
42            first = ec._windows_adapters_cim()
43            for _ in range(17):
44                self.assertEqual(ec._windows_adapters_cim(), first)
45        self.assertEqual(len(calls), 1)
46        self.assertEqual({a["vendor"] for a in first}, {"10de", "1002"})
47
48    def test_a_stuck_query_costs_its_timeout_once(self):
49        calls = []
50
51        def run(*a, **k):
52            calls.append(k.get("timeout"))
53            raise subprocess.TimeoutExpired(a, k.get("timeout"))
54        p1, p2, p3 = self._windows(run)
55        with p1, p2, p3:
56            for _ in range(18):
57                self.assertEqual(ec._windows_adapters_cim(), [])
58        self.assertEqual(calls, [15])
59
60    def test_the_answer_is_asked_again_after_it_expires(self):
61        calls = []
62
63        def run(*a, **k):
64            calls.append(1)
65            return subprocess.CompletedProcess(a, 0, GOOD, "")
66        p1, p2, p3 = self._windows(run)
67        with p1, p2, p3:
68            ec._windows_adapters_cim()
69            stamp, found = ec._CIM_CACHE["adapters"]
70            ec._CIM_CACHE["adapters"] = (stamp - ec.CIM_CACHE_SECONDS - 1, found)
71            ec._windows_adapters_cim()
72        self.assertEqual(len(calls), 2)
73
74    def test_a_caller_cannot_change_what_the_next_one_gets(self):
75        p1, p2, p3 = self._windows(lambda *a, **k: subprocess.CompletedProcess(a, 0, GOOD, ""))
76        with p1, p2, p3:
77            a = ec._windows_adapters_cim()
78            a[0]["vendor"] = "changed"
79            self.assertNotEqual(ec._windows_adapters_cim()[0]["vendor"], "changed")
80
81    def test_not_windows_asks_nothing(self):
82        with mock.patch.object(ec.os, "name", "posix"), mock.patch.object(ec.subprocess, "run", side_effect=AssertionError("must not run")):
83            self.assertEqual(ec._windows_adapters_cim(), [])
84        self.assertNotIn("adapters", ec._CIM_CACHE)
85
86
87if __name__ == "__main__":
88    unittest.main()