Skip to content

Commit a0744af

Browse files
committed
fix(cache): add test for FileCache stale file race condition in __init__
Signed-off-by: Pavan-Rana <psrbr157@gmail.com>
1 parent 0bf9979 commit a0744af

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/utils/test_cache.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import time
13
import typing as t
24
from pathlib import Path
35

@@ -131,3 +133,22 @@ def test_optimized_query_cache_macro_def_change(tmp_path: Path, mocker: MockerFi
131133
new_model.render_query_or_raise().sql()
132134
== 'SELECT "_0"."a" AS "a" FROM (SELECT 1 AS "a") AS "_0" WHERE "_0"."a" = 2'
133135
)
136+
137+
138+
def test_file_cache_init_handles_stale_file(tmp_path: Path, mocker: MockerFixture) -> None:
139+
cache: FileCache[_TestEntry] = FileCache(tmp_path)
140+
141+
stale_file = tmp_path / f"{cache._cache_version}__fake_deleted_model_9999999999"
142+
stale_file.touch()
143+
144+
original_stat = Path.stat
145+
146+
def flaky_stat(self, **kwargs):
147+
if self.name == stale_file.name:
148+
raise FileNotFoundError(f"Simulated stale file: {self}")
149+
return original_stat(self, **kwargs)
150+
151+
mocker.patch.object(Path, "stat", flaky_stat)
152+
153+
FileCache(tmp_path)
154+

0 commit comments

Comments
 (0)