fix save_attempts trimming (#24246)

This commit is contained in:
Josh Hawkins
2026-09-12 07:30:04 -06:00
committed by Nicolas Mowen
parent 7bc32fd4c9
commit 37f338d5d6
4 changed files with 89 additions and 24 deletions
+50
View File
@@ -1,5 +1,6 @@
import os
import tempfile
import time
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import patch
@@ -86,3 +87,52 @@ class TestFileUtils(TestCase):
pass
assert file_util.get_event_thumbnail_bytes(event) is None
class TestTrimOldestFiles(TestCase):
def _fill(self, folder: str, names: list[str]) -> None:
# a short gap keeps the ctime order deterministic
for name in names:
with open(os.path.join(folder, name), "wb"):
pass
time.sleep(0.01)
def test_trims_folder_already_over_limit(self):
"""Verify one call trims a folder that is far over the limit."""
with tempfile.TemporaryDirectory() as folder:
self._fill(folder, [f"{i:03d}.webp" for i in range(10)])
file_util.trim_oldest_files(folder, 4)
assert sorted(os.listdir(folder)) == [
"006.webp",
"007.webp",
"008.webp",
"009.webp",
]
def test_counts_every_listed_image_extension(self):
"""Verify the trim counts the same images the train listing shows."""
with tempfile.TemporaryDirectory() as folder:
self._fill(
folder, ["notes.txt", "a.jpg", "b.jpeg", "c.png", "d.webp", "e.webp"]
)
file_util.trim_oldest_files(folder, 2)
assert sorted(os.listdir(folder)) == ["d.webp", "e.webp", "notes.txt"]
def test_file_removed_during_scan_does_not_skip_trim(self):
"""Verify a file deleted between listing and stat still trims the rest."""
real_listdir = os.listdir
with tempfile.TemporaryDirectory() as folder:
self._fill(folder, [f"{i:03d}.webp" for i in range(10)])
with patch(
"os.listdir", side_effect=lambda p: real_listdir(p) + ["gone.webp"]
):
file_util.trim_oldest_files(folder, 4)
assert len(os.listdir(folder)) == 4