Skip to content

Commit 99dc8a0

Browse files
test(helpers): cover dir size cache behavior
1 parent 4199fe1 commit 99dc8a0

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

dream-server/extensions/services/dashboard-api/tests/test_helpers.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import json
5+
from pathlib import Path
56
from unittest.mock import AsyncMock, MagicMock
67

78
import aiohttp
@@ -13,7 +14,7 @@
1314
get_uptime, get_cpu_metrics, get_ram_metrics,
1415
check_service_health, get_all_services,
1516
get_llama_metrics, get_loaded_model, get_llama_context_size,
16-
get_disk_usage, dir_size_gb,
17+
get_disk_usage, dir_size_gb, invalidate_dir_size_cache, clear_dir_size_cache,
1718
_get_aio_session, set_services_cache, get_cached_services,
1819
_get_lifetime_tokens,
1920
)
@@ -821,14 +822,17 @@ def test_invalid_eta_string(self, data_dir):
821822
class TestDirSizeGb:
822823

823824
def test_nonexistent_path_returns_zero(self, tmp_path):
825+
clear_dir_size_cache()
824826
assert dir_size_gb(tmp_path / "does-not-exist") == 0.0
825827

826828
def test_empty_directory_returns_zero(self, tmp_path):
829+
clear_dir_size_cache()
827830
empty = tmp_path / "empty"
828831
empty.mkdir()
829832
assert dir_size_gb(empty) == 0.0
830833

831834
def test_directory_with_files(self, tmp_path):
835+
clear_dir_size_cache()
832836
d = tmp_path / "data"
833837
d.mkdir()
834838
# Write 100 MiB (avoids allocating 1 GiB in CI)
@@ -837,6 +841,7 @@ def test_directory_with_files(self, tmp_path):
837841
assert dir_size_gb(d) == 0.1
838842

839843
def test_symlinks_are_skipped(self, tmp_path):
844+
clear_dir_size_cache()
840845
d = tmp_path / "withlinks"
841846
d.mkdir()
842847
real = d / "real.bin"
@@ -846,3 +851,40 @@ def test_symlinks_are_skipped(self, tmp_path):
846851
# Only real.bin should be counted (1024 B ≈ 0.0 GB when rounded to 2dp)
847852
result = dir_size_gb(d)
848853
assert result == 0.0 # 1024 bytes rounds to 0.0 GB
854+
855+
def test_uses_cached_value_until_invalidated(self, tmp_path, monkeypatch):
856+
clear_dir_size_cache()
857+
d = tmp_path / "cached"
858+
d.mkdir()
859+
(d / "data.bin").write_bytes(b"\x00" * 1024)
860+
861+
assert dir_size_gb(d) == 0.0
862+
863+
def _unexpected_rglob(self, pattern):
864+
raise AssertionError("dir_size_gb unexpectedly walked the filesystem")
865+
866+
monkeypatch.setattr(Path, "rglob", _unexpected_rglob)
867+
assert dir_size_gb(d) == 0.0
868+
869+
def test_invalidate_dir_size_cache_forces_refresh(self, tmp_path, monkeypatch):
870+
clear_dir_size_cache()
871+
d = tmp_path / "refresh"
872+
d.mkdir()
873+
(d / "data.bin").write_bytes(b"\x00" * 1024)
874+
875+
assert dir_size_gb(d) == 0.0
876+
877+
original_rglob = Path.rglob
878+
calls = {"count": 0}
879+
880+
def _tracking_rglob(self, pattern):
881+
calls["count"] += 1
882+
return original_rglob(self, pattern)
883+
884+
monkeypatch.setattr(Path, "rglob", _tracking_rglob)
885+
assert dir_size_gb(d) == 0.0
886+
assert calls["count"] == 0
887+
888+
invalidate_dir_size_cache(d)
889+
assert dir_size_gb(d) == 0.0
890+
assert calls["count"] == 1

0 commit comments

Comments
 (0)