缓存功能windows平台适配
This commit is contained in:
1 parent
151e6e4b97
commit
22579e51c9
8 files changed
+333
-1
No files matched your search
@@ -6,6 +6,7 @@ import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -108,6 +109,63 @@ with acquire_cache_lease(Path(sys.argv[1]), 'models', sys.argv[2], exclusive=Tru
|
||||
gc.collect()
|
||||
self.assertIsNotNone(self.lease(3, exclusive=True, blocking=False))
|
||||
|
||||
def test_touch_uses_available_utime_operation_and_preserves_lru(self):
|
||||
real_utime = os.utime
|
||||
for supported in (False, True):
|
||||
with self.subTest(follow_symlinks_supported=supported):
|
||||
self.cache = self.folder / f"cache-capability-{supported}"
|
||||
touched = self.entry(0, 60, 1)
|
||||
old = self.entry(1, 60, 2)
|
||||
artifact_bytes = (touched / "artifact").read_bytes()
|
||||
before = touched.stat().st_mtime_ns
|
||||
calls = []
|
||||
|
||||
def platform_utime(path, times=None, **kwargs):
|
||||
calls.append(kwargs)
|
||||
if not supported and kwargs.get("follow_symlinks") is False:
|
||||
raise NotImplementedError("utime: follow_symlinks unavailable on this platform")
|
||||
# Both capability branches can run on either host. The
|
||||
# real timestamp change is on a validated ordinary folder.
|
||||
return real_utime(path, times)
|
||||
|
||||
with patch.object(storage.os, "utime", platform_utime), patch.object(
|
||||
storage.os, "supports_follow_symlinks", {platform_utime} if supported else set(),
|
||||
):
|
||||
with self.lease(0):
|
||||
storage.touch_cache_entry(self.cache, "models", key(0))
|
||||
self.assertEqual(calls, [{"follow_symlinks": False}] if supported else [{}])
|
||||
self.assertGreater(touched.stat().st_mtime_ns, before)
|
||||
self.assertEqual((touched / "artifact").read_bytes(), artifact_bytes)
|
||||
storage.prune_cache(self.cache, model_limit_bytes=60)
|
||||
self.assertTrue(touched.exists())
|
||||
self.assertFalse(old.exists())
|
||||
|
||||
def test_touch_rejects_windows_reparse_directory_before_timestamp_update(self):
|
||||
entry = self.entry(0, 60, 1)
|
||||
real_lstat = Path.lstat
|
||||
attributes = SimpleNamespace(
|
||||
st_mode=entry.lstat().st_mode, st_file_attributes=0x400,
|
||||
)
|
||||
|
||||
def reparse_lstat(path, *args, **kwargs):
|
||||
return attributes if path == entry else real_lstat(path, *args, **kwargs)
|
||||
|
||||
with patch.object(Path, "lstat", reparse_lstat), patch.object(
|
||||
storage.os, "supports_follow_symlinks", set(),
|
||||
), patch.object(storage.os, "utime") as update:
|
||||
with self.assertRaisesRegex(RuntimeError, "real directory"):
|
||||
storage.touch_cache_entry(self.cache, "models", key(0))
|
||||
update.assert_not_called()
|
||||
|
||||
def test_touch_does_not_hide_timestamp_permission_errors(self):
|
||||
self.entry(0, 60, 1)
|
||||
with patch.object(storage.os, "supports_follow_symlinks", set()), patch.object(
|
||||
storage.os, "utime", side_effect=PermissionError("timestamp denied"),
|
||||
) as update:
|
||||
with self.assertRaisesRegex(PermissionError, "timestamp denied"):
|
||||
storage.touch_cache_entry(self.cache, "models", key(0))
|
||||
update.assert_called_once()
|
||||
|
||||
def test_lru_removes_oldest_until_separate_budgets_are_met(self):
|
||||
oldest = self.entry(0, 60, 1)
|
||||
middle = self.entry(1, 60, 2)
|
||||
|
||||
Reference in new issue
Block a user