okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b247f4ecd
main
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 7python -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 ): 26def setUp (self ): 27ec ._CIM_CACHE .clear () 28self .addCleanup (ec ._CIM_CACHE .clear ) 29 30def _windows (self ,run ): 31return (mock .patch .object (ec .os ,"name" ,"nt" ),mock .patch .object (ec .shutil ,"which" ,lambda name :"powershell" ), 32mock .patch .object (ec .subprocess ,"run" ,run )) 33 34def test_many_callers_ask_powershell_once (self ): 35calls = [] 36 37def run (* a ,** k ): 38calls .append (a ) 39return subprocess .CompletedProcess (a ,0 ,GOOD ,"" ) 40p1 ,p2 ,p3 = self ._windows (run ) 41with p1 ,p2 ,p3 : 42first = ec ._windows_adapters_cim () 43for _ in range (17 ): 44self .assertEqual (ec ._windows_adapters_cim (),first ) 45self .assertEqual (len (calls ),1 ) 46self .assertEqual ({a ["vendor" ]for a in first }, {"10de" ,"1002" }) 47 48def test_a_stuck_query_costs_its_timeout_once (self ): 49calls = [] 50 51def run (* a ,** k ): 52calls .append (k .get ("timeout" )) 53raise subprocess .TimeoutExpired (a ,k .get ("timeout" )) 54p1 ,p2 ,p3 = self ._windows (run ) 55with p1 ,p2 ,p3 : 56for _ in range (18 ): 57self .assertEqual (ec ._windows_adapters_cim (), []) 58self .assertEqual (calls , [15 ]) 59 60def test_the_answer_is_asked_again_after_it_expires (self ): 61calls = [] 62 63def run (* a ,** k ): 64calls .append (1 ) 65return subprocess .CompletedProcess (a ,0 ,GOOD ,"" ) 66p1 ,p2 ,p3 = self ._windows (run ) 67with p1 ,p2 ,p3 : 68ec ._windows_adapters_cim () 69stamp ,found = ec ._CIM_CACHE ["adapters" ] 70ec ._CIM_CACHE ["adapters" ]= (stamp - ec .CIM_CACHE_SECONDS - 1 ,found ) 71ec ._windows_adapters_cim () 72self .assertEqual (len (calls ),2 ) 73 74def test_a_caller_cannot_change_what_the_next_one_gets (self ): 75p1 ,p2 ,p3 = self ._windows (lambda * a ,** k :subprocess .CompletedProcess (a ,0 ,GOOD ,"" )) 76with p1 ,p2 ,p3 : 77a = ec ._windows_adapters_cim () 78a [0 ]["vendor" ]= "changed" 79self .assertNotEqual (ec ._windows_adapters_cim ()[0 ]["vendor" ],"changed" ) 80 81def test_not_windows_asks_nothing (self ): 82with mock .patch .object (ec .os ,"name" ,"posix" ),mock .patch .object (ec .subprocess ,"run" ,side_effect = AssertionError ("must not run" )): 83self .assertEqual (ec ._windows_adapters_cim (), []) 84self .assertNotIn ("adapters" ,ec ._CIM_CACHE ) 85 86 87if __name__ == "__main__" : 88unittest .main ()