112 lines
5.1 KiB
Python
112 lines
5.1 KiB
Python
"""Completed release work renews inactivity; a late proof cannot release a lease."""
|
|
from contextlib import contextmanager
|
|
import json
|
|
from pathlib import Path
|
|
from threading import Event
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from test_issue15_resource_release import library_file, cleanup
|
|
from cbb_refresh_verify import library_lock, operation_queue, owner_probe, file_probe, residual_sessions
|
|
|
|
|
|
@pytest.fixture
|
|
def release_work(tmp_path, monkeypatch):
|
|
library = library_file(tmp_path)
|
|
with library_lock.LibraryLock(library, safe_to_release=lambda: False) as lease:
|
|
pass
|
|
offset = [0]
|
|
clock = time.monotonic
|
|
monkeypatch.setattr(operation_queue, 'time', SimpleNamespace(monotonic=lambda: clock()+offset[0]))
|
|
steps = []
|
|
state = {'delay': lambda name: None}
|
|
def complete(name):
|
|
steps.append(name)
|
|
state['delay'](name)
|
|
monkeypatch.setattr(owner_probe, 'query_owners', lambda *_: (complete('owners') or []))
|
|
monkeypatch.setattr(residual_sessions, 'query_process_snapshot', lambda: (complete('snapshot') or []))
|
|
monkeypatch.setattr(residual_sessions, '_process_windows', lambda *_: {})
|
|
hold = file_probe.hold_release_files
|
|
@contextmanager
|
|
def files(*args, **kwargs):
|
|
with hold(*args, **kwargs) as proof:
|
|
complete('files')
|
|
yield proof
|
|
monkeypatch.setattr(file_probe, 'hold_release_files', files)
|
|
write_text, write_bytes = Path.write_text, Path.write_bytes
|
|
def text(path, content, *args, **kwargs):
|
|
result = write_text(path, content, *args, **kwargs)
|
|
if path.name == 'release.json' and json.loads(content)['status'] == 'RELEASE_PENDING':
|
|
complete('pending_record')
|
|
return result
|
|
def data(path, content, *args, **kwargs):
|
|
result = write_bytes(path, content, *args, **kwargs)
|
|
if path.name.startswith('blocker-before-'): complete('archive')
|
|
return result
|
|
monkeypatch.setattr(Path, 'write_text', text)
|
|
monkeypatch.setattr(Path, 'write_bytes', data)
|
|
yield library, lease, offset, state, steps
|
|
cleanup(lease)
|
|
|
|
|
|
@pytest.mark.parametrize('ui', [False, True])
|
|
def test_aggregate_completed_release_checks_keep_final_proof_visible(release_work, tmp_path, monkeypatch, qtbot, ui):
|
|
library, lease, offset, state, steps = release_work
|
|
def advancing(name):
|
|
offset[0] += 6 if name in ('pending_record', 'archive') else 4
|
|
time.sleep(.06) # The real context waiter observes each completed boundary.
|
|
state['delay'] = advancing
|
|
if ui:
|
|
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
|
|
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: {'library':str(library),'sessions':[]})
|
|
dialog = ResidualReleaseDialog(library, tmp_path/'evidence')
|
|
qtbot.addWidget(dialog); dialog.show()
|
|
qtbot.waitUntil(lambda: not dialog.busy)
|
|
proofs = []; dialog.released.connect(proofs.append)
|
|
dialog.confirm_button.click()
|
|
qtbot.waitUntil(lambda: not dialog.busy, timeout=5000)
|
|
assert len(proofs) == 1, dialog.message.text()
|
|
assert '已释放 0 个会话' in dialog.message.text()
|
|
proof = proofs[0]
|
|
else:
|
|
context = operation_queue.BatchTaskContext(str(library))
|
|
try:
|
|
proof = context.run(lambda: library_lock.release_retained_library(library, tmp_path/'evidence'),
|
|
timeout_seconds=10)
|
|
finally: context.close(timeout_seconds=3)
|
|
assert proof['status'] == 'RELEASE_CONFIRMED'
|
|
assert proof['exclusive_files'] == [str(library)]
|
|
assert offset[0] >= 24 and steps.count('owners') == 2
|
|
assert not library_lock.has_retained_library(library)
|
|
assert not library_lock.blocker_path(library).exists()
|
|
|
|
|
|
@pytest.mark.parametrize('step', ['owners', 'files', 'pending_record', 'archive'])
|
|
@pytest.mark.parametrize('cancel', [False, True])
|
|
def test_stalled_or_cancelled_completed_step_cannot_commit_release(release_work, tmp_path, step, cancel):
|
|
library, lease, offset, state, steps = release_work
|
|
marker = library_lock.blocker_path(library); original = marker.read_bytes()
|
|
context = operation_queue.BatchTaskContext(str(library)); done = Event()
|
|
delayed = []
|
|
def delayed_step(name):
|
|
if name == step and not delayed:
|
|
delayed.append(name)
|
|
if cancel: context.cancel()
|
|
offset[0] += 11
|
|
time.sleep(.08)
|
|
state['delay'] = delayed_step
|
|
def release():
|
|
try: return library_lock.release_retained_library(library, tmp_path/'evidence', stop=context.stop)
|
|
finally: done.set()
|
|
try:
|
|
with pytest.raises(operation_queue.QueueCancelled): context.run(release, timeout_seconds=10)
|
|
assert done.wait(3), 'Observe completion before closing the context; close must not mask late release'
|
|
assert delayed == [step]
|
|
assert marker.read_bytes() == original
|
|
assert library_lock.has_retained_library(library)
|
|
for path in (tmp_path/'evidence').rglob('release.json'):
|
|
assert json.loads(path.read_text())['status'] != 'RELEASE_CONFIRMED'
|
|
finally: context.close(timeout_seconds=3)
|