okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d363f11e
main
1"""Round 0213 QATUI (final pass, owner PC): with an engine installed, one `strata launch --plan` on Windows started `nvidia-smi` 19 times (10 x compute_cap, 9 x 2driver_version): every caller asked again. The answer is kept for 30 s per field now. 3 4python -m pytest tests/terminal/test_smi_cache_0213_qatui.py 5""" 6from __future__import annotations 7 8import subprocess 9import sys 10import unittest 11from pathlib import Path 12from unittest import mock 13 14ROOT = Path (__file__ ).resolve ().parents [2 ] 15sys .path .insert (0 ,str (ROOT )) 16 17from strata_control import engine_component as ec # noqa: E402 18 19 20class SmiCache (unittest .TestCase ): 21def setUp (self ): 22ec ._SMI_CACHE .clear () 23self .addCleanup (ec ._SMI_CACHE .clear ) 24 25def _smi (self ,run ): 26return (mock .patch .object (ec .shutil ,"which" ,lambda name :"nvidia-smi" ),mock .patch .object (ec .subprocess ,"run" ,run )) 27 28def test_many_callers_start_nvidia_smi_once_per_field (self ): 29calls = [] 30 31def run (cmd ,** kw ): 32calls .append (cmd [1 ]) 33return subprocess .CompletedProcess (cmd ,0 ,"12.0\n" if "compute_cap" in cmd [1 ]else "610.47\n" ,"" ) 34p1 ,p2 = self ._smi (run ) 35with p1 ,p2 : 36for _ in range (10 ): 37self .assertEqual (ec .cuda_archs_present (), ["sm_120" ]) 38self .assertEqual (ec .nvidia_driver_version (),"610.47" ) 39self .assertEqual (sorted (calls ), ["--query-gpu=compute_cap" ,"--query-gpu=driver_version" ]) 40 41def test_a_stuck_nvidia_smi_costs_its_timeout_once (self ): 42calls = [] 43 44def run (cmd ,** kw ): 45calls .append (kw .get ("timeout" )) 46raise subprocess .TimeoutExpired (cmd ,kw .get ("timeout" )) 47p1 ,p2 = self ._smi (run ) 48with p1 ,p2 : 49for _ in range (10 ): 50self .assertEqual (ec .cuda_archs_present (), []) 51self .assertEqual (calls , [8 ]) 52 53def test_it_is_asked_again_after_the_time_is_up (self ): 54calls = [] 55 56def run (cmd ,** kw ): 57calls .append (1 ) 58return subprocess .CompletedProcess (cmd ,0 ,"8.6\n" ,"" ) 59p1 ,p2 = self ._smi (run ) 60with p1 ,p2 : 61ec .cuda_archs_present () 62stamp ,found = ec ._SMI_CACHE ["compute_cap" ] 63ec ._SMI_CACHE ["compute_cap" ]= (stamp - ec .SMI_CACHE_SECONDS - 1 ,found ) 64ec .cuda_archs_present () 65self .assertEqual (len (calls ),2 ) 66 67def test_a_caller_cannot_change_the_next_answer (self ): 68p1 ,p2 = self ._smi (lambda cmd ,** kw :subprocess .CompletedProcess (cmd ,0 ,"8.6\n8.9\n" ,"" )) 69with p1 ,p2 : 70first = ec ._smi_query ("compute_cap" ) 71first .append ("x" ) 72self .assertEqual (ec ._smi_query ("compute_cap" ), ["8.6" ,"8.9" ]) 73 74 75if __name__ == "__main__" : 76unittest .main ()