okami.mom/strata

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

Niko1221Merge f-142d-numa into f-1421bccef1cf8

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