MCPcopy
hub / github.com/commaai/openpilot / test_prune_cache

Method test_prune_cache

tools/lib/tests/test_caching.py:139–167  ·  view source on GitHub ↗
(self, monkeypatch)

Source from the content-addressed store, hash-verified

137
138class TestCache:
139 def test_prune_cache(self, monkeypatch):
140 with tempfile.TemporaryDirectory() as tmpdir:
141 monkeypatch.setattr(Paths, 'download_cache_root', staticmethod(lambda: tmpdir + "/"))
142
143 # setup test files and manifest
144 manifest_lines = []
145 for i in range(3):
146 fname = f"hash_{i}"
147 with open(tmpdir + "/" + fname, "wb") as f:
148 f.truncate(1000)
149 manifest_lines.append(f"{fname} {1000 + i}")
150 with open(tmpdir + "/manifest.txt", "w") as f:
151 f.write('\n'.join(manifest_lines))
152
153 # under limit, shouldn't prune
154 assert len(os.listdir(tmpdir)) == 4
155 prune_cache()
156 assert len(os.listdir(tmpdir)) == 4
157
158 # set a tiny cache limit to force eviction (1.5 chunks worth)
159 monkeypatch.setattr(url_file_module, 'CACHE_SIZE', url_file_module.CHUNK_SIZE + url_file_module.CHUNK_SIZE // 2)
160
161 # prune_cache should evict oldest files to get under limit
162 prune_cache()
163 remaining = os.listdir(tmpdir)
164 # should have evicted at least one file + manifest
165 assert len(remaining) < 4
166 # newest file should remain
167 assert manifest_lines[2].split()[0] in remaining

Callers

nothing calls this directly

Calls 4

prune_cacheFunction · 0.90
appendMethod · 0.80
writeMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected