Files
cbb-refresh-verify-795/tests/test_issue21_batch_entry.py
T

122 lines
5.3 KiB
Python

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_rechecks_recorded_session_after_client_exit(tmp_path, monkeypatch):
from cbb_refresh_verify import session_record, verify_runner
library = tmp_path / 'library.lmc'
library.touch()
request = VerifyRequest(library, 'Next', tmp_path / 'evidence')
monkeypatch.setattr(session_record, 'library_available', lambda _, live_only=False: live_only)
checks = []
def recheck(*args, **kwargs):
checks.append(kwargs.get('required', False))
if kwargs.get('required'):
raise LibraryBusy('记录中的客户端已退出,共享 iCDB 数据库仍在使用')
monkeypatch.setattr('cbb_refresh_verify.library_lock.recheck_previous_release', recheck)
with pytest.raises(QueueUnavailable, match='共享 iCDB 数据库仍在使用'):
verify_runner.wait_for_batch_release(request, BatchTaskContext(str(library)), timeout_seconds=0)
assert checks == [True]
def test_acknowledged_release_stops_only_matching_observer(tmp_path, monkeypatch):
from threading import Event
from cbb_refresh_verify import session_record as sessions
library = tmp_path / 'library.lmc'
key = str(tmp_path / 'session.json')
aliases = {sessions._library_key(library)}
stop = Event()
monkeypatch.setattr(sessions, '_retained_libraries', {key: aliases})
monkeypatch.setattr(sessions, '_retained_results', {key: {'operation_id': 'old', 'block': 'A'}})
monkeypatch.setattr(sessions, '_observers', {key: (None, None, stop)})
monkeypatch.setattr(sessions, 'record_session_release', lambda *_: [])
proof = {'library': str(library), 'status': 'RELEASE_CONFIRMED'}
snapshot = sessions.retained_session_snapshot(library)
sessions.acknowledge_library_release(proof, snapshot, block_name='B')
assert not stop.is_set()
sessions.acknowledge_library_release(proof, snapshot, block_name='A')
assert stop.is_set()
assert sessions.library_available(library)
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
def test_completed_save_keeps_bounded_release_probe_alive(tmp_path, monkeypatch):
from contextlib import contextmanager
from threading import Event
from cbb_refresh_verify.icdb_ownership import wait_for_saved_database_release
entered, finish = Event(), Event()
@contextmanager
def probe(*args, **kwargs):
entered.set()
assert finish.wait(1)
yield {'exclusive_files': [str(tmp_path/'icdb.dat')]}
monkeypatch.setattr('cbb_refresh_verify.file_probe.hold_release_files', probe)
context = BatchTaskContext(str(tmp_path/'library.lmc'))
from threading import Thread
def finish_probe():
assert entered.wait(1)
Event().wait(.15)
finish.set()
thread = Thread(target=finish_probe)
thread.start()
try:
result = context.run(lambda: wait_for_saved_database_release(context.library,
tmp_path/'project.prj', tmp_path/'project.pcb'), timeout_seconds=.05)
assert result['status'] == 'RELEASE_CONFIRMED'
finally:
finish.set()
thread.join()