import pytest from cbb_refresh_verify.library_lock import LibraryBusy from cbb_refresh_verify.operation_queue import BatchTaskContext, QueueUnavailable from cbb_refresh_verify.verify_runner import VerifyRequest def test_batch_admission_rechecks_once_before_dispatch(tmp_path, monkeypatch): from cbb_refresh_verify import verify_runner library = tmp_path/'library.lmc' library.touch() request = VerifyRequest(library, 'Next', tmp_path/'evidence') calls = [] monkeypatch.setattr('cbb_refresh_verify.library_lock.recheck_previous_release', lambda lib, *args, **kwargs: calls.append((lib, kwargs['infer_scope']))) verify_runner.wait_for_batch_release(request, BatchTaskContext(str(library))) assert calls == [(library, True)] def test_batch_admission_reports_resource_at_entry(tmp_path, monkeypatch): from cbb_refresh_verify import verify_runner library = tmp_path/'library.lmc' library.touch() request = VerifyRequest(library, 'Next', tmp_path/'evidence') def busy(*args, **kwargs): raise LibraryBusy('共享符号库 icdb.dat 仍在使用') monkeypatch.setattr('cbb_refresh_verify.library_lock.recheck_previous_release', busy) with pytest.raises(QueueUnavailable, match='共享符号库 icdb.dat 仍在使用'): verify_runner.wait_for_batch_release(request, BatchTaskContext(str(library)), timeout_seconds=0) def test_batch_waits_for_recorded_save_past_ordinary_release_deadline(tmp_path, monkeypatch): from cbb_refresh_verify import verify_runner from cbb_refresh_verify import session_record library = tmp_path/'library.lmc' library.touch() request = VerifyRequest(library, 'Next', tmp_path/'evidence') context = BatchTaskContext(str(library)) waiting = [] monkeypatch.setattr(session_record, 'library_available', lambda *a, **k: bool(waiting)) monkeypatch.setattr(session_record, 'retained_session_states', lambda *_: [ {'status': 'ACTIVE', 'phase': 'NATIVE_CALL', 'block': 'Previous', 'active_save': True}]) monkeypatch.setattr('cbb_refresh_verify.library_lock.recheck_previous_release', lambda *a, **k: None) monkeypatch.setattr(context.stop, 'wait', lambda interval: waiting.append(interval)) verify_runner.wait_for_batch_release(request, context, timeout_seconds=0) assert len(waiting) == 1