249 lines
13 KiB
Python
249 lines
13 KiB
Python
"""An item failure must not strand a batch or masquerade as ICDB damage."""
|
|
from dataclasses import replace
|
|
from threading import Event
|
|
|
|
import pytest
|
|
|
|
from cbb_refresh_verify import management, operation_lifecycle, session_record
|
|
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue
|
|
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult, run_batch
|
|
|
|
|
|
def test_bulk_admission_skips_live_observer_without_starting_or_cancelling(tmp_path, monkeypatch):
|
|
from test_live_observer_queue import observed
|
|
library, _ = observed(tmp_path, monkeypatch)
|
|
queue = NativeQueue()
|
|
context = BatchTaskContext(str(library))
|
|
calls = []
|
|
with pytest.raises(RuntimeError, match='NATIVE_SESSION_PENDING'):
|
|
queue.run(lambda: calls.append('unexpected'), context=context, skip_unavailable=True)
|
|
assert not calls and not queue.pending and not queue.active
|
|
assert not context.stop.is_set()
|
|
assert queue.run(lambda: 'other library', context=BatchTaskContext(str(tmp_path/'other.lmc')),
|
|
skip_unavailable=True) == 'other library'
|
|
assert not session_record.library_available(library, live_only=True)
|
|
|
|
|
|
@pytest.mark.parametrize('kind', ['verify', 'management'])
|
|
def test_pre_admission_busy_is_one_skip_not_a_retained_or_cancelled_batch(tmp_path, monkeypatch, kind):
|
|
from test_live_observer_queue import observed
|
|
library, state = observed(tmp_path, monkeypatch)
|
|
queue = NativeQueue()
|
|
calls = []
|
|
def item(req, action):
|
|
path = req.lmc_file if kind == 'verify' else req.library
|
|
context = BatchTaskContext(str(path))
|
|
try:
|
|
return queue.run(action, context=context, skip_unavailable=True)
|
|
finally:
|
|
# The original observer finishes between submitted targets.
|
|
state['status'] = 'RELEASED'
|
|
if kind == 'verify':
|
|
def execute(req):
|
|
calls.append(req.block_name)
|
|
return VerifyResult('PASS')
|
|
results = run_batch([VerifyRequest(library, name, tmp_path) for name in ('Busy', 'Good')],
|
|
verifier=execute, item_executor=item)
|
|
else:
|
|
def execute(req, **kwargs):
|
|
calls.append(req.name)
|
|
return management.ManagementResult(req.operation, req.library, req.name, req.partition,
|
|
req.source, req.auto_verify, 'complete', 'REMOVED', True)
|
|
monkeypatch.setattr(management, 'run_management', execute)
|
|
results = management.run_management_batch([
|
|
management.ManagementRequest('remove', library, name) for name in ('Busy', 'Good')],
|
|
item_executor=item)
|
|
assert [r.status for r in results] == ['SKIPPED', 'PASS' if kind == 'verify' else 'REMOVED']
|
|
assert all(r.safe_to_continue for r in results)
|
|
assert calls == ['Good']
|
|
assert 'NATIVE_SESSION_PENDING' in (results[0].stderr if kind == 'verify' else results[0].detail)
|
|
|
|
|
|
@pytest.mark.parametrize('released', [True, False])
|
|
def test_management_rechecks_original_owner_and_preserves_failure(tmp_path, monkeypatch, released):
|
|
library = tmp_path/'lib.lmc'
|
|
requests = [management.ManagementRequest('remove', library, name) for name in ('Bad', 'Next')]
|
|
failed = management.ManagementResult('remove', library, 'Bad', 'Logical-Physical', None,
|
|
True, 'complete', 'RECOVERY_REQUIRED', False, 'original failure', tmp_path/'result.json')
|
|
checks, calls = [], []
|
|
def recheck(lib, block, result, **kwargs):
|
|
assert lib == library and block == 'Bad' and result.name == 'Bad'
|
|
checks.append(block)
|
|
return replace(result, safe_to_continue=True) if released and len(checks) > 1 else result
|
|
def execute(req, **kwargs):
|
|
calls.append(req.name)
|
|
return failed if req.name == 'Bad' else replace(failed, name=req.name, status='REMOVED', safe_to_continue=True)
|
|
monkeypatch.setattr(operation_lifecycle, 'recheck_failed_release', recheck)
|
|
monkeypatch.setattr(management, 'run_management', execute)
|
|
results = management.run_management_batch(requests)
|
|
assert checks == ['Bad', 'Bad']
|
|
assert calls == (['Bad', 'Next'] if released else ['Bad'])
|
|
assert results[0].status == 'RECOVERY_REQUIRED' and results[0].detail == 'original failure'
|
|
assert results[0].safe_to_continue is released
|
|
if not released:
|
|
assert 'Bad' in results[1].detail and results[1].stage == 'admission'
|
|
|
|
|
|
def test_management_unexpected_item_exception_keeps_independent_targets(tmp_path, monkeypatch):
|
|
libraries = [tmp_path/'same.lmc', tmp_path/'same.lmc', tmp_path/'other.lmc']
|
|
requests = [management.ManagementRequest('remove', library, name)
|
|
for library, name in zip(libraries, ('Crash', 'Same', 'Other'))]
|
|
calls = []
|
|
def execute(req, **kwargs):
|
|
calls.append(req.name)
|
|
if req.name == 'Crash':
|
|
raise OSError('unexpected evidence disk failure')
|
|
return management.ManagementResult(req.operation, req.library, req.name, req.partition,
|
|
None, True, 'complete', 'REMOVED', True)
|
|
monkeypatch.setattr(management, 'run_management', execute)
|
|
results = management.run_management_batch(requests)
|
|
assert [r.status for r in results] == ['RECOVERY_REQUIRED', 'SKIPPED', 'REMOVED']
|
|
assert not results[0].safe_to_continue
|
|
assert 'BATCH_ITEM_EXCEPTION' in results[0].detail
|
|
assert calls == ['Crash', 'Other']
|
|
|
|
|
|
@pytest.mark.parametrize('released', [True, False])
|
|
def test_icdb_rechecks_failed_owner_before_next_target(qtbot, tmp_path, monkeypatch, released):
|
|
from cbb_refresh_verify.ui.icdb_check_task import IcdbCheckTask
|
|
library = tmp_path/'lib.lmc'
|
|
checks, calls = [], []
|
|
failed = VerifyResult('FAIL', stderr='original database load error', safe_to_continue=False,
|
|
evidence_path=tmp_path/'result.json', error_category='MENTOR_DATABASE_LOAD_FAILED')
|
|
def execute(req):
|
|
calls.append(req.block_name)
|
|
return failed if req.block_name == 'Bad' else VerifyResult('CHECKED')
|
|
def recheck(lib, block, result, **kwargs):
|
|
assert lib == library and block == 'Bad' and result is failed
|
|
checks.append(block)
|
|
return replace(result, safe_to_continue=True) if released and len(checks) > 1 else result
|
|
monkeypatch.setattr(operation_lifecycle, 'recheck_failed_release', recheck)
|
|
task = IcdbCheckTask([VerifyRequest(library, name, tmp_path) for name in ('Bad', 'Next')],
|
|
NativeQueue(), tmp_path/'reports', verifier=execute)
|
|
task.start()
|
|
qtbot.waitUntil(lambda: not task.active, timeout=5000)
|
|
assert checks == ['Bad', 'Bad']
|
|
assert calls == (['Bad', 'Next'] if released else ['Bad'])
|
|
assert task.results[0].status == 'FAIL' and task.results[0].safe_to_continue is released
|
|
if not released:
|
|
assert 'Bad' in task.results[1].stderr
|
|
assert task.results[1].error_category == 'PREVIOUS_SESSION_UNRELEASED'
|
|
|
|
|
|
def test_icdb_live_observer_finishes_batch_without_cancel_button(qtbot, tmp_path, monkeypatch):
|
|
from test_live_observer_queue import observed
|
|
from cbb_refresh_verify.ui.icdb_check_task import IcdbCheckTask
|
|
library, _ = observed(tmp_path, monkeypatch)
|
|
calls = []
|
|
task = IcdbCheckTask([VerifyRequest(library, name, tmp_path) for name in ('A', 'B')],
|
|
NativeQueue(), tmp_path/'reports',
|
|
verifier=lambda req: calls.append(req.block_name) or VerifyResult('CHECKED'))
|
|
task.start()
|
|
try:
|
|
qtbot.waitUntil(lambda: not task.active, timeout=1500)
|
|
assert [r.status for r in task.results] == ['SKIPPED', 'SKIPPED']
|
|
assert all(r.safe_to_continue and r.error_category == 'NATIVE_SESSION_PENDING' for r in task.results)
|
|
assert not calls and not task.stop.is_set()
|
|
finally:
|
|
task.cancel()
|
|
qtbot.waitUntil(lambda: not task.active, timeout=3000)
|
|
|
|
|
|
def test_cancel_has_priority_over_busy_admission(tmp_path, monkeypatch):
|
|
from test_live_observer_queue import observed
|
|
from cbb_refresh_verify.operation_queue import QueueCancelled
|
|
library, _ = observed(tmp_path, monkeypatch)
|
|
context = BatchTaskContext(str(library))
|
|
context.cancel()
|
|
with pytest.raises(QueueCancelled):
|
|
NativeQueue().run(lambda: pytest.fail('cancelled action dispatched'),
|
|
context=context, skip_unavailable=True)
|
|
|
|
|
|
def test_cancel_during_release_recheck_preserves_original_result(tmp_path):
|
|
stop = Event()
|
|
stop.set()
|
|
failed = VerifyResult('FAIL', safe_to_continue=False, evidence_path=tmp_path/'result.json')
|
|
result = operation_lifecycle.recheck_failed_release(tmp_path/'lib.lmc', 'Bad', failed, stop=stop)
|
|
assert result is failed
|
|
|
|
|
|
def test_broken_management_progress_sink_does_not_abort_completed_items(tmp_path, monkeypatch):
|
|
requests = [management.ManagementRequest('remove', tmp_path/'lib.lmc', name) for name in ('A', 'B')]
|
|
def execute(req, **kwargs):
|
|
return management.ManagementResult(req.operation, req.library, req.name, req.partition,
|
|
None, True, 'complete', 'REMOVED', True)
|
|
def disconnected(event):
|
|
raise RuntimeError('UI receiver deleted')
|
|
monkeypatch.setattr(management, 'run_management', execute)
|
|
results = management.run_management_batch(requests, progress=disconnected)
|
|
assert [r.status for r in results] == ['REMOVED', 'REMOVED']
|
|
|
|
|
|
def test_verify_ui_busy_admission_reports_each_skip_and_remains_usable(qtbot, tmp_path, monkeypatch):
|
|
from test_ui_filters import load_window
|
|
window = load_window(qtbot, tmp_path, monkeypatch,
|
|
verifier=lambda req: pytest.fail('live observer must prevent native launch'))
|
|
library = window.mentor_lmc_path or window.loaded_path
|
|
# Preserve the real admission/Qt paths; only emulate the cached native observer.
|
|
from types import SimpleNamespace
|
|
monkeypatch.setattr(session_record, '_retained_libraries', {'owned':{session_record._library_key(library)}})
|
|
monkeypatch.setattr(session_record, '_retained_results', {'owned':{'status':'ACTIVE'}})
|
|
monkeypatch.setattr(session_record, '_observers', {'owned':(SimpleNamespace(is_alive=lambda:True),None)})
|
|
window._run(['Beta', 'Gamma'])
|
|
try:
|
|
qtbot.waitUntil(lambda: not window.busy, timeout=2500)
|
|
assert window.search_edit.isEnabled()
|
|
reports = list((tmp_path/'reports').glob('batch-*.log'))
|
|
assert reports
|
|
text = reports[0].read_text(encoding='utf-8')
|
|
assert text.count('NATIVE_SESSION_PENDING') >= 2
|
|
assert 'CANCELLED' not in text and 'PREVIOUS_SESSION_UNRELEASED' not in text
|
|
assert window.status_label.text().startswith('已结束:')
|
|
finally:
|
|
window.stop.set()
|
|
window.native_queue.wake()
|
|
qtbot.waitUntil(lambda: not window.busy, timeout=3000)
|
|
|
|
|
|
@pytest.mark.parametrize('kind', ['backup', 'management'])
|
|
def test_dialog_busy_before_dispatch_never_claims_own_unreleased_session(qtbot, tmp_path, monkeypatch, kind):
|
|
from test_live_observer_queue import observed
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
from PySide6.QtWidgets import QMessageBox
|
|
library, _ = observed(tmp_path, monkeypatch)
|
|
monkeypatch.setattr(timed_prompt, 'question', lambda *a, **kw: QMessageBox.Yes)
|
|
results = []
|
|
if kind == 'backup':
|
|
from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog
|
|
dialog = BackupRecoveryDialog(('A',), lambda name: VerifyRequest(library, name, tmp_path),
|
|
recoverer=lambda *a, **kw: pytest.fail('blocked recovery dispatched'))
|
|
source=tmp_path/'A.prj'; source.touch()
|
|
dialog.source_edit.setText(str(source))
|
|
dialog.completed.connect(lambda name,result: results.append(result))
|
|
button=dialog.restore_button
|
|
else:
|
|
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
|
dialog = ManagementDialog([management.ManagementRequest('remove',library,'A')],
|
|
runner=lambda *a,**kw: pytest.fail('blocked management dispatched'))
|
|
dialog.completed.connect(results.extend)
|
|
button=dialog.execute_button
|
|
qtbot.addWidget(dialog)
|
|
dialog.native_queue=NativeQueue()
|
|
button.click()
|
|
qtbot.waitUntil(lambda: not dialog.busy, timeout=2500)
|
|
assert len(results)==1 and results[0].status=='SKIPPED' and results[0].safe_to_continue
|
|
|
|
|
|
def test_pre_admission_error_does_not_retain_a_path_lease(qtbot, tmp_path, monkeypatch):
|
|
from test_ui_filters import load_window
|
|
from cbb_refresh_verify.operation_queue import QueueUnavailable
|
|
window=load_window(qtbot,tmp_path,monkeypatch)
|
|
from types import SimpleNamespace
|
|
window.library_lease=SimpleNamespace()
|
|
retained=[]
|
|
monkeypatch.setattr(window,'_retain_path_lease',lambda *a,**kw: retained.append(a))
|
|
window._failed(QueueUnavailable('lib.lmc'))
|
|
window.library_lease=None
|
|
assert not retained
|