feat: integrate native diagnostics and coordinated CBB transactions
This commit is contained in:
1 parent
9e230e1a46
commit
da67de026f
91 files changed
+14370
-435
No files matched your search
@@ -0,0 +1,258 @@
|
||||
"""Only live clients stop a batch; native writes are replaced by fixtures."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status,detail', [('FAIL', 'UNKNOWN_DIALOG: native prompt'),
|
||||
('TIMEOUT', 'native operation timed out')])
|
||||
@pytest.mark.parametrize('released', [True, False])
|
||||
def test_batch_continuation_follows_actual_release(tmp_path, monkeypatch, status, detail, released):
|
||||
fixture(tmp_path)
|
||||
launches = []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 1
|
||||
def __init__(self, command, *_a, **_k):
|
||||
launches.append(command)
|
||||
Path(command[-1]).with_name('outcome.json').write_text(json.dumps({
|
||||
'status': status, 'stderr': detail, 'safe_to_continue': False}))
|
||||
def poll(self): return 1
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k:
|
||||
{'safe_to_continue': released, 'remaining_pids': [] if released else [123]})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
req = request(tmp_path)
|
||||
try:
|
||||
results = runner.run_batch([req, req], verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
|
||||
assert results[0].status == status and results[0].safe_to_continue is released
|
||||
assert len(launches) == (2 if released else 1)
|
||||
assert results[1].status == (status if released else 'SKIPPED')
|
||||
finally:
|
||||
from cbb_refresh_verify import library_lock as locks
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_item_failure_with_confirmed_release_does_not_stop_unrelated_items(tmp_path):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
library = tmp_path/'library.lmc'; library.touch()
|
||||
requests = [VerifyRequest(library, name, tmp_path/'evidence') for name in ('A', 'B')]
|
||||
calls = []
|
||||
def verify(request):
|
||||
calls.append(request.block_name)
|
||||
return VerifyResult('FAIL' if request.block_name == 'A' else 'PASS',
|
||||
stderr='单项失败但会话已释放' if request.block_name == 'A' else '',
|
||||
safe_to_continue=True)
|
||||
results = runner.run_batch(requests, verifier=verify)
|
||||
assert calls == ['A', 'B']
|
||||
assert [result.status for result in results] == ['FAIL', 'PASS']
|
||||
|
||||
|
||||
def test_stop_worker_escalates_only_after_cooperative_deadline():
|
||||
calls = []
|
||||
class Worker:
|
||||
stopped = False
|
||||
def request_stop(self): calls.append('cancel')
|
||||
def wait(self, timeout=5):
|
||||
calls.append('wait')
|
||||
if not self.stopped: raise TimeoutError()
|
||||
def kill(self): calls.append('client_stop'); self.stopped = True
|
||||
def poll(self): return 1 if self.stopped else None
|
||||
assert runner._stop_worker(Worker(), timeout=.1) is True
|
||||
assert calls == ['cancel', 'wait', 'client_stop', 'wait']
|
||||
|
||||
|
||||
def test_cancel_reaches_current_supervised_worker(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
stop = Event()
|
||||
cancelled = []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = None
|
||||
def __init__(self, command, *_a, **_k):
|
||||
self.job = json.loads(Path(command[-1]).read_text('utf-8'))
|
||||
stop.set()
|
||||
def bind_cancellation(self, path): self.cancel = Path(path)
|
||||
def request_stop(self):
|
||||
self.cancel.touch(); cancelled.append(self.cancel)
|
||||
self.returncode = 1
|
||||
def wait(self, **_k): return 1
|
||||
def poll(self): return self.returncode
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
result = runner.run_verify(request(tmp_path), stop=stop, port_check=PORTS_OK)
|
||||
assert result.status == 'CANCELLED' and result.safe_to_continue
|
||||
assert len(cancelled) == 1 and cancelled[0].is_file()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('text,expected', [
|
||||
('content checking completed successfully! 0 errors, 0 warnings', 'PASS'),
|
||||
('content checking completed successfully! 10 errors', 'FAIL'),
|
||||
('content checking completed successfully! error: missing symbol', 'FAIL'),
|
||||
('content checking completed successfully! 0 errors\nERROR: cannot connect', 'FAIL'),
|
||||
])
|
||||
def test_zero_error_count_does_not_block_content_check(text, expected):
|
||||
assert runner.classify_output(0, text) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('codec', ['utf-16', 'utf-8-sig', 'mbcs'])
|
||||
def test_preflight_reads_native_project_encodings(tmp_path, codec):
|
||||
work = fixture(tmp_path)
|
||||
(work/'CBB1.prj').write_bytes(('# 中文工程\nKEY PCBDesignPath "PCB\\actual.pcb"\n').encode(codec))
|
||||
project, pcb = runner.preflight(request(tmp_path))
|
||||
assert pcb == work/'PCB/actual.pcb'
|
||||
|
||||
|
||||
def test_unrelated_pcb_does_not_block_target_before_launch(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(adapter, 'pcb_processes', lambda: ((999, 'ExpeditionPCB.exe', '"ExpeditionPCB.exe" "C:\\other\\board.pcb"'),))
|
||||
assert execute()['status'] == 'PASS'
|
||||
assert any(e['stage'] == 'pcb_launch' for e in events)
|
||||
|
||||
|
||||
def test_sampling_ports_only_warn_by_default(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
launches, warnings = [], []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 0
|
||||
def __init__(self, command, *_a, **_k):
|
||||
launches.append(command)
|
||||
Path(command[-1]).with_name('outcome.json').write_text('{"status":"PASS"}')
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
result = runner.run_verify(request(tmp_path), port_check=lambda: (False, 'sample unavailable'),
|
||||
flag_reader=lambda *_: __import__('cbb_refresh_verify.status_bridge', fromlist=['BridgeResult']).BridgeResult('READY', 'IsVerifiedReuseBlock', 1),
|
||||
progress=warnings.append)
|
||||
assert launches and result.status == 'PASS'
|
||||
assert any(e['stage'] == 'port_warning' for e in warnings)
|
||||
|
||||
|
||||
def test_cleanup_includes_late_native_descendant_without_pcb_argument(monkeypatch):
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
rows = [(10, 'ExpeditionPCB.exe', '', 1), (11, 'ExpeditionPCB.exe', '', 10),
|
||||
(99, 'ExpeditionPCB.exe', '', 77)]
|
||||
monkeypatch.setattr(adapter, '_process_details', lambda name: rows if name == 'ExpeditionPCB.exe' else [])
|
||||
result = adapter.cleanup_verify_processes(None, None, 10, baseline_pids={99})
|
||||
assert not result['safe_to_continue'] and result['remaining_pids'] == [10, 11]
|
||||
|
||||
|
||||
def test_save_client_without_worker_still_prevents_continuation(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 1
|
||||
def __init__(self, command, *_a, **_k):
|
||||
Path(command[-1]).with_name('outcome.json').write_text('{"status":"FAIL"}')
|
||||
def poll(self): return 1
|
||||
def close(self): pass
|
||||
def automation_release_state(self): return {'safe_to_continue': False, 'remaining_pids': [333]}
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
req = request(tmp_path)
|
||||
try:
|
||||
result = runner.run_verify(req, port_check=PORTS_OK)
|
||||
assert not result.safe_to_continue
|
||||
proof = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8'))
|
||||
assert proof['worker_exited'] is True
|
||||
assert proof['automation_release']['remaining_pids'] == [333]
|
||||
assert proof['remaining_pids'] == [333]
|
||||
finally:
|
||||
from cbb_refresh_verify import library_lock as locks
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_closed_desktop_worker_keeps_exit_status_without_using_closed_handle(monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from cbb_refresh_verify import desktop_worker as desktop
|
||||
worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker)
|
||||
handle = SimpleNamespace(closed=False)
|
||||
def close(): handle.closed = True
|
||||
handle.Close = close
|
||||
worker.handle = handle
|
||||
worker.job = SimpleNamespace(Close=lambda: None)
|
||||
worker.desktop = None; worker.companions = []; worker.companion_pids = []
|
||||
def code(h):
|
||||
assert not h.closed, 'Read from a closed Win32 process handle'
|
||||
return 1
|
||||
monkeypatch.setattr(desktop.win32process, 'GetExitCodeProcess', code)
|
||||
worker.close()
|
||||
assert worker.poll() == 1
|
||||
worker.close() # Idempotent cleanup.
|
||||
|
||||
|
||||
def test_native_process_queries_use_bounded_shared_helper(monkeypatch):
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
calls = []
|
||||
def query(names):
|
||||
calls.append(names)
|
||||
return [{'pid': 12, 'executable': 'ExpeditionPCB.exe', 'command': 'native', 'parent': 11}]
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', query)
|
||||
assert adapter._process_details('ExpeditionPCB.exe') == [(12, 'ExpeditionPCB.exe', 'native', 11)]
|
||||
assert calls == [('ExpeditionPCB.exe',)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script', ['save_verified_block.vbs', 'close_failed_block.vbs'])
|
||||
def test_save_and_failure_close_clients_are_both_tracked(script, monkeypatch):
|
||||
from cbb_refresh_verify import desktop_worker as desktop
|
||||
worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker)
|
||||
worker.pid = 11; worker._closed = True; worker._exit_code = 1
|
||||
command = f'cscript.exe //nologo "{Path(desktop.__file__).with_name(script)}" "C:\\fixture\\design.prj"'
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: [
|
||||
{'pid': 22, 'parent': 11, 'command': command},
|
||||
{'pid': 33, 'parent': 99, 'command': command},
|
||||
{'pid': 44, 'parent': 11, 'command': 'cscript.exe unrelated.vbs'}])
|
||||
proof = worker.automation_release_state()
|
||||
assert proof['safe_to_continue'] is False and proof['remaining_pids'] == [22]
|
||||
|
||||
|
||||
def test_watchdog_with_cancel_signal_stops_clients_after_supervisor_exit(tmp_path):
|
||||
import subprocess, sys, time
|
||||
import win32api, win32con, win32process
|
||||
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
|
||||
script = tmp_path/'watchdog.py'; ready = tmp_path/'ready.json'; cancel = tmp_path/'cancel.signal'
|
||||
script.write_text(
|
||||
'import sys, subprocess, time, json\nfrom pathlib import Path\n'
|
||||
'from cbb_refresh_verify.desktop_worker import start_supervisor_watchdog, launch_save_helper\n'
|
||||
f'start_supervisor_watchdog({supervisor.pid}, cancel_path={str(cancel)!r}, stop_clients=True)\n'
|
||||
'native = subprocess.Popen([sys.executable,"-c","import time; time.sleep(30)"])\n'
|
||||
'client = launch_save_helper([sys.executable,"-c","import time; time.sleep(30)"])\n'
|
||||
f'Path({str(ready)!r}).write_text(json.dumps([native.pid,client.pid]))\n'
|
||||
'time.sleep(30)\n', encoding='utf-8')
|
||||
worker = subprocess.Popen([sys.executable, str(script)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
handles = []
|
||||
try:
|
||||
deadline = time.monotonic()+10
|
||||
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
|
||||
time.sleep(.05)
|
||||
assert ready.exists()
|
||||
for pid in json.loads(ready.read_text()):
|
||||
handles.append(win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE, False, pid))
|
||||
supervisor.terminate(); supervisor.wait(5)
|
||||
assert worker.wait(10) == 75 and cancel.is_file()
|
||||
assert win32process.GetExitCodeProcess(handles[0]) == 259
|
||||
assert win32process.GetExitCodeProcess(handles[1]) != 259
|
||||
finally:
|
||||
for p in (supervisor, worker):
|
||||
if p.poll() is None: p.terminate()
|
||||
p.wait(5)
|
||||
for h in handles:
|
||||
if win32process.GetExitCodeProcess(h) == 259: win32process.TerminateProcess(h, 0)
|
||||
h.Close()
|
||||
Reference in new issue
Block a user