Files
DeepTutor/tests/co_writer/test_storage_lock.py
RRFZ 953a6106f5 fix(co_writer): stop the Windows write lock file growing on every save
CoWriterStorage.locked() is taken by every document save, note-thread
change, proposal and review. Its Windows branch appended b"0" to the
append-mode .write.lock file before each msvcrt lock, so the file grew
by one byte per acquisition (1,000 saves: 1,000 bytes) and never shrank.

Write the byte only when the file is empty, as the other msvcrt locks
in the codebase do.
2026-10-31 22:25:42 -04:00

33 lines
1.1 KiB
Python

"""The Co-Writer write lock's Windows (msvcrt) branch."""
from __future__ import annotations
import sys
from types import SimpleNamespace
from deeptutor.co_writer.storage import CoWriterStorage
from deeptutor.services.path_service import PathService
def test_windows_write_lock_file_keeps_its_single_byte(tmp_path, monkeypatch) -> None:
# Every save, note-thread change and proposal takes this lock; the
# Windows branch used to append a byte to the lock file each time.
calls: list[int] = []
def locking(_fileno: int, mode: int, length: int) -> None:
assert length == 1
calls.append(mode)
monkeypatch.setitem(sys.modules, "fcntl", None) # `import fcntl` fails, as on Windows
monkeypatch.setitem(
sys.modules, "msvcrt", SimpleNamespace(LK_LOCK=1, LK_UNLCK=0, locking=locking)
)
storage = CoWriterStorage(path_service=PathService(workspace_root=tmp_path))
for _ in range(5):
with storage.locked():
pass
assert (storage.docs_root() / ".write.lock").stat().st_size == 1
assert calls == [1, 0] * 5