fix: preserve native reads and advancing metadata operations

This commit is contained in:
阿白叔 committed 2026-11-17 00:39:30 +08:00
1 parent 5b7e7bea2f
commit 07206ce0d1
5 files changed
+128 -8

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
"""Large advancing manifests stay usable; a stalled member still hands off."""
from threading import Event
import time
import pytest
from cbb_refresh_verify.operation_queue import BatchTaskContext, TaskHandoff, check_active
from cbb_refresh_verify.scoped_manifest import build_manifest, hash_file
def test_advancing_manifest_outlives_aggregate_metadata_deadline(tmp_path, monkeypatch):
for i in range(768):
(tmp_path/f'part-{i:04}.dat').write_bytes(str(i).encode())
clock, offset = time.monotonic, [0]
monkeypatch.setattr(time, 'monotonic', lambda:clock()+offset[0])
def progress(event):
if event['hashed_files']:
offset[0] += 8
time.sleep(.06) # Let the actual context waiter observe each advancing batch.
context = BatchTaskContext(str(tmp_path/'library.lmc'), native_policy='external')
try:
result = context.run(lambda:build_manifest(tmp_path, progress=progress), timeout_seconds=10)
assert len(result['files']) == 768
assert offset[0] == 24
finally:
context.close(timeout_seconds=2)
def test_stalled_manifest_member_cannot_resume_into_native_write(tmp_path):
(tmp_path/'one.dat').write_bytes(b'content')
entered, release = Event(), Event()
writes = []
def stalled(path):
entered.set()
assert release.wait(3)
return hash_file(path)
def action():
build_manifest(tmp_path, hash_function=stalled)
check_active()
writes.append('native')
context = BatchTaskContext(str(tmp_path/'library.lmc'), native_policy='external')
try:
with pytest.raises(TaskHandoff):
context.run(action, timeout_seconds=.1)
assert entered.is_set()
release.set()
context.close(timeout_seconds=2)
assert writes == []
finally:
release.set()
def test_manifest_progress_does_not_interrupt_admitted_native_continuation(tmp_path):
from cbb_refresh_verify.operation_queue import native_phase
(tmp_path/'one.dat').write_bytes(b'content')
context = BatchTaskContext(str(tmp_path/'library.lmc'))
effects = []
def transaction():
with native_phase():
effects.append('Remove')
context.cancel()
assert len(build_manifest(tmp_path)['files']) == 1
effects.append('Add')
context.run(transaction)
assert effects == ['Remove', 'Add']
+35
View File
@@ -203,3 +203,38 @@ def test_catalog_read_role_requires_the_actual_readonly_worker_action(tmp_path):
assert effects==[]
assert catalog_dialog_action('Library Manager','Would you like to save?',read_only=True)=='cancel'
assert catalog_dialog_action('Upgrade library','Continue?',read_only=True)=='cancel'
def test_catalog_native_wait_outlives_metadata_deadline(tmp_path, transport, monkeypatch):
from threading import Thread
from cbb_refresh_verify.native_management import NativeManagementAdapter
from cbb_refresh_verify.operation_queue import BatchTaskContext
Transport, workers = transport
evidence = tmp_path/'slow-catalog'
original = Transport.__init__
def start(self, command, *args, **kwargs):
Path(command[-1]).with_name('hold-handler').touch()
original(self, command, *args, **kwargs)
Transport.__init__ = start
clock = time.monotonic
offset = [0]
monkeypatch.setattr(time, 'monotonic', lambda: clock()+offset[0])
def complete():
deadline = clock()+5
while not (evidence/'handler-entered').exists() and clock()<deadline:
time.sleep(.01)
offset[0] = 20 # Only the admitted native wait crosses the metadata deadline.
time.sleep(.15)
(evidence/'finish').touch()
finisher = Thread(target=complete)
finisher.start()
context = BatchTaskContext(str(tmp_path/'library.lmc'), native_policy='external')
try:
result = context.run(lambda: NativeManagementAdapter(worker_factory=Transport).catalog(
tmp_path/'library.lmc', evidence), timeout_seconds=10)
assert [entry.name for entry in result.entries] == ['A']
assert result.safe_to_continue
finally:
(evidence/'finish').touch()
finisher.join(6)
context.close(timeout_seconds=6)