fix(memory): clean atomic write test hygiene

Made-with: Cursor
This commit is contained in:
Xubin Ren 2026-04-29 08:54:51 +00:00 committed by Xubin Ren
parent 53ca2836e7
commit 3d7099b421
2 changed files with 11 additions and 16 deletions

View File

@ -369,7 +369,7 @@ class MemoryStore:
f.flush() f.flush()
os.fsync(f.fileno()) os.fsync(f.fileno())
os.replace(tmp_path, self.history_file) os.replace(tmp_path, self.history_file)
# fsync the directory so the rename is durable. # fsync the directory so the rename is durable.
# On Windows, opening a directory with O_RDONLY raises # On Windows, opening a directory with O_RDONLY raises
# PermissionError — skip the dir sync there (NTFS # PermissionError — skip the dir sync there (NTFS

View File

@ -148,14 +148,14 @@ class TestHistoryWithCursor:
store.append_history("event 2") store.append_history("event 2")
store.append_history("event 3") store.append_history("event 3")
entries = store.read_unprocessed_history(since_cursor=0) entries = store.read_unprocessed_history(since_cursor=0)
# Monitor temp file existence # Monitor temp file existence
tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp") tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp")
assert not tmp_path_obj.exists() # Should not exist initially assert not tmp_path_obj.exists() # Should not exist initially
# Call _write_entries # Call _write_entries
store._write_entries(entries) store._write_entries(entries)
# Temp file should be cleaned up # Temp file should be cleaned up
assert not tmp_path_obj.exists() assert not tmp_path_obj.exists()
# Original file should exist # Original file should exist
@ -166,26 +166,21 @@ class TestHistoryWithCursor:
store = MemoryStore(tmp_path) store = MemoryStore(tmp_path)
store.append_history("event 1") store.append_history("event 1")
entries = store.read_unprocessed_history(since_cursor=0) entries = store.read_unprocessed_history(since_cursor=0)
tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp") tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp")
# Mock os.replace to raise an exception # Mock os.replace to raise an exception
original_replace = __import__('os').replace
def failing_replace(*args, **kwargs): def failing_replace(*args, **kwargs):
raise RuntimeError("Simulated failure") raise RuntimeError("Simulated failure")
monkeypatch.setattr('os.replace', failing_replace) monkeypatch.setattr('os.replace', failing_replace)
try: with pytest.raises(RuntimeError):
store._write_entries(entries) store._write_entries(entries)
assert False, "Should have raised"
except RuntimeError:
pass
# Temp file should be cleaned up # Temp file should be cleaned up
assert not tmp_path_obj.exists() assert not tmp_path_obj.exists()
# Original file should still exist (because replace failed) # Original file should still exist (because replace failed)
assert store.history_file.exists() assert store.history_file.exists()