Harden canonical publication against races
This commit is contained in:
parent
8919e2af32
commit
a901c9705b
8 changed files with 1059 additions and 78 deletions
|
|
@ -2,9 +2,11 @@ from __future__ import annotations
|
|||
|
||||
import hashlib
|
||||
import os
|
||||
import stat
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from docforge.errors import DocForgeError
|
||||
from docforge.incremental import (
|
||||
|
|
@ -97,6 +99,24 @@ class ExtractionCacheBoundsTests(unittest.TestCase):
|
|||
)
|
||||
)
|
||||
|
||||
def test_cache_publication_fsyncs_the_parent_directory(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "extractions.json"
|
||||
real_fsync = os.fsync
|
||||
fsynced_modes: list[int] = []
|
||||
|
||||
def record_fsync(descriptor: int) -> None:
|
||||
fsynced_modes.append(os.fstat(descriptor).st_mode)
|
||||
real_fsync(descriptor)
|
||||
|
||||
with mock.patch(
|
||||
"docforge._fs_safety.os.fsync",
|
||||
side_effect=record_fsync,
|
||||
):
|
||||
write_extraction_cache(path, self.cache(), max_bytes=1_000, max_sources=2)
|
||||
|
||||
self.assertTrue(any(stat.S_ISDIR(mode) for mode in fsynced_modes))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue