A02: Atomic cooldown state with exclusive lock and flush

- _write_state: write to temp file, fsync, rename atomically
- Acquire exclusive lock before any file operations
- Flush and fsync before unlock to prevent data loss
- Remove stale .tmp file after successful write
- Add test for atomic write behavior
- 109 Python tests pass
This commit is contained in:
ik
2026-09-10 06:10:35 +07:00
parent 779d554057
commit 79245965ec
2 changed files with 28 additions and 2 deletions
+17
View File
@@ -57,3 +57,20 @@ def test_validate_url_host_rejects_missing_hostname() -> None:
_validate_url_host("not-a-valid-url")
with pytest.raises(ValueError, match="valid hostname"):
_validate_url_host("")
def test_write_state_is_atomic_with_flush(tmp_path: Path) -> None:
"""A02: _write_state uses exclusive lock, flush, and atomic rename."""
from rf4_research.community_cli import _read_state, _write_state
state_file = tmp_path / "state.json"
_write_state(state_file, {"key1": "value1"})
assert state_file.exists()
assert not (state_file.with_suffix(".tmp")).exists()
state = _read_state(state_file)
assert state == {"key1": "value1"}
_write_state(state_file, {"key1": "value2", "key2": "value3"})
state = _read_state(state_file)
assert state == {"key1": "value2", "key2": "value3"}
assert not (state_file.with_suffix(".tmp")).exists()