fix: acknowledge diagnostic prompt ownership before native close
This commit is contained in:
1 parent
de81bbf293
commit
4402486baa
5 files changed
+232
-16
No files matched your search
@@ -21,7 +21,7 @@ def test_only_current_diagnostic_close_leaves_prompt_actions_to_checker(tmp_path
|
||||
def __init__(self, command, name):
|
||||
self.name = name
|
||||
path = Path(command[-1]); job = json.loads(path.read_text())
|
||||
event = dict(stage='diagnostic_wm_close', pcb_pid=901, hwnd=900,
|
||||
event = dict(stage='diagnostic_close_requested', close_id='b'*32, pcb_pid=901, hwnd=900,
|
||||
operation_id=job['operation_id'], desktop=job['desktop'], project=job['project'], pcb=job['pcb'])
|
||||
for condition, key, value in [('wrong_pid','pcb_pid',999),('wrong_operation','operation_id','foreign'),
|
||||
('wrong_project','project','foreign.prj'),('wrong_desktop','desktop','Default')]:
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
from test_diagnostic_open import job, write_logs, frame, Session
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fault', ['', 'foreign_operation', 'ordinary', 'missing_nonce'])
|
||||
def test_close_handoff_arriving_during_poll_prevents_competing_abort(tmp_path, monkeypatch, fault):
|
||||
from cbb_refresh_verify import desktop_worker
|
||||
from cbb_refresh_verify.verify_runner import run_verify
|
||||
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
|
||||
fixture(tmp_path); state = {}
|
||||
class Worker:
|
||||
desktop = None; pid = 700; companion_pids = []; returncode = None; count = 0
|
||||
def __init__(self, command, name):
|
||||
self.name = name
|
||||
path = Path(command[-1]); state.update(path=path, job=json.loads(path.read_text()))
|
||||
path.with_name('events.jsonl').write_text(json.dumps(dict(stage='pcb_worker_started', pcb_pid=901))+'\n')
|
||||
path.with_name('outcome.json').write_text(json.dumps(dict(status='FAIL', safe_to_continue=False,
|
||||
stderr='DIAGNOSTIC_CLOSE_UNCONFIRMED: checker retained unknown prompt')))
|
||||
def poll(self):
|
||||
self.count += 1
|
||||
if self.count > 2: self.returncode = 1
|
||||
return self.returncode
|
||||
def close(self): pass
|
||||
def wait(self, timeout=5): self.returncode = 1; return 1
|
||||
class Supervisor:
|
||||
def poll(self, *args):
|
||||
j = state['job']
|
||||
event = dict(stage='diagnostic_close_requested', close_id='a'*32, pcb_pid=901, hwnd=900,
|
||||
operation_id=j['operation_id'], desktop=j['desktop'], project=j['project'], pcb=j['pcb'])
|
||||
if fault == 'foreign_operation': event['operation_id'] = 'foreign'
|
||||
if fault == 'missing_nonce': event.pop('close_id')
|
||||
with state['path'].with_name('events.jsonl').open('a') as stream:
|
||||
stream.write(json.dumps(event)+'\n')
|
||||
return DialogPollResult(error_code='UNKNOWN_DIALOG', detail='in-flight competing save prompt')
|
||||
monkeypatch.setattr(desktop_worker, 'DesktopWorker', Worker)
|
||||
result = run_verify(request(tmp_path, diagnostic_only=fault!='ordinary'), port_check=PORTS_OK,
|
||||
dialog_supervisor_factory=lambda **kw: Supervisor())
|
||||
assert result.status == 'FAIL'
|
||||
ack = state['path'].with_name('diagnostic-close-ack.json')
|
||||
if fault:
|
||||
assert 'UNKNOWN_DIALOG' in result.stderr and not ack.exists()
|
||||
else:
|
||||
assert 'DIAGNOSTIC_CLOSE_UNCONFIRMED' in result.stderr
|
||||
assert 'UNKNOWN_DIALOG' not in result.stderr
|
||||
assert json.loads(ack.read_text())['close_id'] == 'a'*32
|
||||
|
||||
|
||||
def test_checker_never_posts_close_before_bound_supervisor_ack(job, monkeypatch):
|
||||
from cbb_refresh_verify import diagnostic_open as diagnostic
|
||||
job['operation_id'] = 'verify-protocol'
|
||||
Path(job['root']).mkdir(exist_ok=True)
|
||||
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
||||
write_logs(job); events = []
|
||||
ack = Path(job['root'])/'diagnostic-close-ack.json'
|
||||
class AcknowledgedSession(Session):
|
||||
def post(self, hwnd, message):
|
||||
assert ack.exists(), 'WM_CLOSE raced ahead of supervisor ownership acknowledgement'
|
||||
assert events[-1]['stage'] == 'diagnostic_close_requested'
|
||||
super().post(hwnd, message)
|
||||
def emit(**event):
|
||||
events.append(event)
|
||||
if event['stage'] == 'diagnostic_close_requested':
|
||||
ack.write_text(json.dumps({k:v for k,v in event.items() if k != 'stage'}))
|
||||
monkeypatch.setattr(diagnostic, '_NativeSession', AcknowledgedSession)
|
||||
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
|
||||
assert diagnostic.check_target_window(job, 42, emit=emit, error_reader=lambda: None)['status'] == 'PASS'
|
||||
assert Session.instances[-1].posted == [(10, 0x10)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fault', ['missing', 'stale_nonce', 'foreign_pid', 'foreign_project',
|
||||
'foreign_desktop', 'cancel_wait', 'cancel_ack', 'late_ack'])
|
||||
def test_missing_stale_cancelled_or_late_ack_preserves_native(job, monkeypatch, fault):
|
||||
from cbb_refresh_verify import diagnostic_open as diagnostic
|
||||
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
||||
write_logs(job)
|
||||
offset = [0]; clock = time.monotonic
|
||||
cancel = Path(job['root'])/'cancel.signal'; job['cancel_signal'] = str(cancel)
|
||||
def wait(_):
|
||||
if fault == 'cancel_wait': cancel.touch()
|
||||
else: offset[0] += 31
|
||||
monkeypatch.setattr(diagnostic, 'time', SimpleNamespace(monotonic=lambda:clock()+offset[0], sleep=wait))
|
||||
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
||||
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)])
|
||||
def emit(**event):
|
||||
if event['stage'] != 'diagnostic_close_requested': return
|
||||
if fault in ('missing', 'cancel_wait'): return
|
||||
ack = {k:v for k,v in event.items() if k != 'stage'}
|
||||
for condition,key,value in [('stale_nonce','close_id','0'*32),('foreign_pid','pcb_pid',99),
|
||||
('foreign_project','project','foreign'),('foreign_desktop','desktop','Default')]:
|
||||
if fault == condition: ack[key] = value
|
||||
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps(ack))
|
||||
if fault == 'cancel_ack': cancel.touch()
|
||||
if fault == 'late_ack': offset[0] += 31
|
||||
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED' if fault.startswith('cancel') else 'DIAGNOSTIC_CLOSE_UNCONFIRMED'):
|
||||
diagnostic.check_target_window(job,42,emit=emit,error_reader=lambda:None)
|
||||
assert Session.instances[-1].alive and Session.instances[-1].posted == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['progress', 'disabled', 'hwnd', 'pid', 'unknown', 'forward', 'native_error', 'cancel_recheck'])
|
||||
def test_ready_frame_and_prompt_are_revalidated_after_ack(job, monkeypatch, change):
|
||||
from cbb_refresh_verify import diagnostic_open as diagnostic
|
||||
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
||||
write_logs(job); acknowledged = []
|
||||
job['cancel_signal'] = str(Path(job['root'])/'cancel.signal')
|
||||
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
||||
def windows(pid):
|
||||
target = frame(job); rows = [target]
|
||||
if not acknowledged: return rows
|
||||
if change == 'progress': target['children'].append({'class':'msctls_progress32','visible':True})
|
||||
if change == 'disabled': target['enabled'] = False
|
||||
if change == 'hwnd': target['hwnd'] = 99
|
||||
if change == 'pid': target['pid'] = 99
|
||||
if change == 'cancel_recheck': Path(job['cancel_signal']).touch()
|
||||
if change == 'unknown': rows.append({'hwnd':20,'pid':42,'class':'#32770','text':'Unknown','children':[]})
|
||||
if change == 'forward': rows.append({'hwnd':20,'pid':42,'class':'#32770','text':'Expedition PCB','owner':10,
|
||||
'children':[{'class':'Static','id':6172,'text':'New changes are ready for Forward Annotation. Do you want to Forward Annotate now?'},
|
||||
{'hwnd':21,'class':'Button','id':7,'text':'No'}, {'hwnd':22,'class':'Button','id':6,'text':'Yes'}]})
|
||||
return rows
|
||||
monkeypatch.setattr(diagnostic, '_windows', windows)
|
||||
def emit(**event):
|
||||
if event['stage'] == 'diagnostic_close_requested':
|
||||
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps({k:v for k,v in event.items() if k!='stage'}))
|
||||
acknowledged.append(True)
|
||||
def error():
|
||||
return 'ICDB_INCONSISTENT: fresh error after handoff' if change == 'native_error' and acknowledged else None
|
||||
expected = {'native_error':'ICDB_INCONSISTENT', 'cancel_recheck':'VERIFY_CANCELLED'}.get(change, 'DIAGNOSTIC_CLOSE_UNCONFIRMED')
|
||||
with pytest.raises(RuntimeError, match=expected):
|
||||
diagnostic.check_target_window(job,42,emit=emit,error_reader=error)
|
||||
assert acknowledged and Session.instances[-1].posted == []
|
||||
@@ -1,14 +1,25 @@
|
||||
import importlib
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def diagnostic():
|
||||
def diagnostic(monkeypatch):
|
||||
assert importlib.util.find_spec('cbb_refresh_verify.diagnostic_open') is not None, 'diagnostic open module is missing'
|
||||
return importlib.import_module('cbb_refresh_verify.diagnostic_open')
|
||||
module = importlib.import_module('cbb_refresh_verify.diagnostic_open')
|
||||
check = module.check_target_window
|
||||
def acknowledged(job, pid, *, emit, **kwargs):
|
||||
def parent(**event):
|
||||
emit(**event)
|
||||
if event['stage'] == 'diagnostic_close_requested':
|
||||
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps(
|
||||
{key:value for key,value in event.items() if key != 'stage'}))
|
||||
return check(job, pid, emit=parent, **kwargs)
|
||||
monkeypatch.setattr(module, 'check_target_window', acknowledged)
|
||||
return module
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -17,8 +28,9 @@ def job(tmp_path):
|
||||
pcb = project / 'PCB/IP101GR.pcb'
|
||||
pcb.parent.mkdir(parents=True)
|
||||
pcb.write_bytes(b'pcb')
|
||||
(tmp_path/'evidence').mkdir()
|
||||
return {'project': str(project / 'IP101GR.prj'), 'pcb': str(pcb), 'root': str(tmp_path / 'evidence'),
|
||||
'desktop': 'CbbDiagnosticTest', 'timeout': .03}
|
||||
'desktop': 'CbbDiagnosticTest', 'operation_id': 'verify-fixture', 'timeout': .03}
|
||||
|
||||
|
||||
def write_logs(job, *, pid=42, foreign=False, success=True):
|
||||
|
||||
Reference in new issue
Block a user