136 lines
5.3 KiB
Python
136 lines
5.3 KiB
Python
"""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']
|
|
|
|
|
|
def test_completed_release_and_idle_checks_outlive_aggregate_deadline(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock, owner_probe
|
|
from cbb_refresh_verify.backup_recovery import _idle_check
|
|
library = tmp_path/'library.lmc'; library.touch()
|
|
clock, offset = time.monotonic, [0]
|
|
monkeypatch.setattr(time, 'monotonic', lambda:clock()+offset[0])
|
|
def released(*args, **kwargs):
|
|
offset[0] += 7
|
|
return {'status': 'RELEASE_CONFIRMED'}
|
|
def owners(names):
|
|
offset[0] += 4
|
|
time.sleep(.06)
|
|
return []
|
|
monkeypatch.setattr(library_lock, 'release_retained_library', released)
|
|
monkeypatch.setattr(owner_probe, 'query_owners', owners)
|
|
context = BatchTaskContext(str(library), native_policy='external')
|
|
def prepare():
|
|
library_lock.recheck_previous_release(library, tmp_path/'evidence', required=True)
|
|
for name in ('target', 'source'):
|
|
_idle_check(library, tmp_path/name/'main.prj', tmp_path/name/'PCB/main.pcb')
|
|
check_active()
|
|
return 'ready for native dispatch'
|
|
try:
|
|
assert context.run(prepare, timeout_seconds=10) == 'ready for native dispatch'
|
|
assert offset[0] == 15
|
|
finally:
|
|
context.close(timeout_seconds=2)
|
|
|
|
|
|
@pytest.mark.parametrize('cancel', [False, True])
|
|
def test_completed_idle_check_cannot_revive_stalled_or_cancelled_work(tmp_path, monkeypatch, cancel):
|
|
from cbb_refresh_verify import owner_probe
|
|
from cbb_refresh_verify.backup_recovery import _idle_check
|
|
from cbb_refresh_verify.operation_queue import QueueCancelled
|
|
library = tmp_path/'library.lmc'; library.touch()
|
|
release = Event(); writes = []
|
|
context = BatchTaskContext(str(library), native_policy='external')
|
|
def owners(names):
|
|
if cancel: context.cancel()
|
|
else: assert release.wait(3)
|
|
return []
|
|
monkeypatch.setattr(owner_probe, 'query_owners', owners)
|
|
def prepare():
|
|
_idle_check(library, tmp_path/'main.prj', tmp_path/'PCB/main.pcb')
|
|
writes.append('dispatch')
|
|
try:
|
|
with pytest.raises(QueueCancelled):
|
|
context.run(prepare, timeout_seconds=.1)
|
|
release.set(); context.close(timeout_seconds=2)
|
|
assert writes == []
|
|
finally:
|
|
release.set()
|
|
|
|
|
|
def test_successful_idle_check_does_not_abort_admitted_native_continuation(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import owner_probe
|
|
from cbb_refresh_verify.backup_recovery import _idle_check
|
|
from cbb_refresh_verify.operation_queue import native_phase
|
|
library = tmp_path/'library.lmc'; library.touch()
|
|
monkeypatch.setattr(owner_probe, 'query_owners', lambda names: [])
|
|
context = BatchTaskContext(str(library)); writes = []
|
|
def transaction():
|
|
with native_phase():
|
|
writes.append('Remove'); context.cancel()
|
|
_idle_check(library, tmp_path/'main.prj', tmp_path/'PCB/main.pcb')
|
|
writes.append('Add')
|
|
context.run(transaction)
|
|
assert writes == ['Remove', 'Add']
|