okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bccef1cf8
main
1import os ,tempfile ,unittest 2from pathlib import Path 3from types import SimpleNamespace 4from serve import cache_drop 5 6 7class CacheDrop (unittest .TestCase ): 8def test_default_off (self ): 9self .assertFalse (cache_drop .enabled ({})) 10self .assertFalse (cache_drop .enabled ({"STRATA_DROP_CACHE_ON_EXIT" :"0" })) 11 12@ unittest . skipIf ( os . name == "nt" , "POSIX only" ) 13def test_on (self ): 14self .assertTrue (cache_drop .enabled ({"STRATA_DROP_CACHE_ON_EXIT" :"1" })) 15 16def test_files_and_advise (self ): 17with tempfile .TemporaryDirectory ()as d : 18Path (d ,"a.gguf" ).write_bytes (b"x" ) 19os .mkdir (os .path .join (d ,"sub" )) 20Path (d ,"sub" ,"b.bin" ).write_bytes (b"y" ) 21self .assertEqual (len (cache_drop .model_files ([d ,"/no/such" ,None ])),2 ) 22seen = [] 23self .assertEqual (cache_drop .drop ([d ],advise = lambda fd ,o ,n ,how :seen .append (how )),2 ) 24self .assertEqual (len (seen ),2 ) 25 26def test_not_while_engine_runs_or_not_opted_in (self ): 27with tempfile .TemporaryDirectory ()as d : 28env = {"STRATA_DROP_CACHE_ON_EXIT" :"1" } 29calls = [] 30run = lambda * a :calls .append (1 ) 31alive = SimpleNamespace (proc = SimpleNamespace (poll = lambda :None ),spawn = ("e" , ["--native" ,d ]),model_path = d ) 32self .assertEqual (cache_drop .drop_after_exit (alive ,env ,run ),0 ) 33gone = SimpleNamespace (proc = SimpleNamespace (poll = lambda :0 ),spawn = ("e" , ["--native" ,d ]),model_path = d ) 34self .assertEqual (cache_drop .drop_after_exit (gone , {},run ),0 ) 35self .assertEqual (calls , []) 36 37 38if __name__ == "__main__" : 39unittest .main ()