fix: reconcile cancelled sessions and resume queued native work
This commit is contained in:
1 parent
845ae58690
commit
7cc65f78da
10 files changed
+637
-18
No files matched your search
@@ -0,0 +1,129 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import desktop_worker, session_record, verify_runner as runner
|
||||
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
|
||||
|
||||
def worker_fixture(tmp_path, monkeypatch, *, exited=False):
|
||||
fixture(tmp_path)
|
||||
state = dict(waits=[], closed=False, observers=[], polling=False, polls=0)
|
||||
stop = Event()
|
||||
class Worker:
|
||||
desktop=None; pid=700; companion_pids=[]; returncode=1 if exited else None
|
||||
def __init__(self, command, name):
|
||||
self.name=name
|
||||
path=Path(command[-1]); state.update(path=path,job=json.loads(path.read_text()),worker=self)
|
||||
self.session_record_path=path.with_name('session.json')
|
||||
session_record.write_session_record(self.session_record_path, dict(
|
||||
operation_id=state['job']['operation_id'],library=state['job']['library'],
|
||||
block=state['job']['block']))
|
||||
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='CANCELLED',safe_to_continue=False)))
|
||||
def poll(self):return self.returncode
|
||||
def request_stop(self):
|
||||
assert not state['polling']
|
||||
Path(state['job']['cancel_signal']).touch()
|
||||
def native_write_pending(self):return True
|
||||
def wait(self,timeout=5):
|
||||
state['waits'].append(timeout)
|
||||
if timeout <= 5:raise TimeoutError('native startup still closing')
|
||||
self.returncode=1;return 1
|
||||
def automation_release_state(self):return {'safe_to_continue':self.poll() is not None,'remaining_pids':[]}
|
||||
def close(self):state['closed']=True
|
||||
class Supervisor:
|
||||
def poll(self,*args):
|
||||
assert not state['observers']
|
||||
state['polling']=True;state['polls']+=1;stop.set();state['polling']=False
|
||||
return DialogPollResult()
|
||||
monkeypatch.setattr(desktop_worker,'DesktopWorker',Worker)
|
||||
monkeypatch.setattr(runner,'_cleanup_session_processes',lambda *a,**kw:
|
||||
{'safe_to_continue':False,'remaining_pids':[901]})
|
||||
monkeypatch.setattr(session_record,'handoff_session',lambda path,**kw:
|
||||
state['observers'].append((path,kw,state['closed'])))
|
||||
# Keep the task runner deterministic while exercising real supervisor ownership decisions.
|
||||
monkeypatch.setattr('cbb_refresh_verify.operation_queue.hold_native_handoff',lambda *a:None)
|
||||
return state,stop,lambda diagnostic:runner.run_verify(request(tmp_path,diagnostic_only=diagnostic),
|
||||
port_check=PORTS_OK,stop=stop,dialog_supervisor_factory=lambda **kw:Supervisor())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('diagnostic',[True,False])
|
||||
def test_diagnostic_cancel_yields_after_five_seconds_but_write_wait_is_unchanged(tmp_path,monkeypatch,diagnostic):
|
||||
state,stop,run=worker_fixture(tmp_path,monkeypatch)
|
||||
assert run(diagnostic).status=='CANCELLED'
|
||||
assert state['waits']==([5] if diagnostic else [5,115])
|
||||
assert state['observers'] and state['observers'][0][1]['process'] is state['worker']
|
||||
assert not state['observers'][0][2] and not state['closed']
|
||||
assert state['polls']==1
|
||||
|
||||
|
||||
def test_exited_worker_with_live_native_clients_keeps_ownership_for_observer(tmp_path,monkeypatch):
|
||||
state,stop,run=worker_fixture(tmp_path,monkeypatch,exited=True)
|
||||
result=run(True)
|
||||
assert result.status=='CANCELLED' and not result.safe_to_continue
|
||||
assert len(state['observers'])==1, 'worker exit must not orphan the still-live registered native client'
|
||||
path,options,already_closed=state['observers'][0]
|
||||
assert path==state['worker'].session_record_path
|
||||
assert options['process'] is state['worker'] and not already_closed and not state['closed']
|
||||
original=json.loads(state['path'].with_name('result.json').read_text())
|
||||
assert original['status']=='CANCELLED' and not original['safe_to_continue']
|
||||
|
||||
|
||||
def test_failed_observer_registration_preserves_original_cancelled_result_and_handle(tmp_path,monkeypatch):
|
||||
state,stop,run=worker_fixture(tmp_path,monkeypatch,exited=True)
|
||||
def fail(*args,**kwargs):raise OSError('observation journal unavailable')
|
||||
monkeypatch.setattr(session_record,'handoff_session',fail)
|
||||
result=run(True)
|
||||
assert result.status=='CANCELLED' and not result.safe_to_continue
|
||||
assert not state['closed']
|
||||
|
||||
|
||||
def test_natural_native_exit_gets_fresh_release_and_admits_waiting_scope_without_click(tmp_path,monkeypatch):
|
||||
import subprocess,sys,time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from types import SimpleNamespace
|
||||
from cbb_refresh_verify import library_lock
|
||||
from cbb_refresh_verify.operation_queue import NativeQueue,BatchTaskContext
|
||||
from test_issue15_resource_release import library_file,cleanup
|
||||
library=library_file(tmp_path)
|
||||
gate=tmp_path/'native-exit'
|
||||
native=subprocess.Popen([sys.executable,'-c',
|
||||
'from pathlib import Path; import time,sys\nwhile not Path(sys.argv[1]).exists(): time.sleep(.02)',str(gate)])
|
||||
evidence=tmp_path/'evidence';evidence.mkdir();path=evidence/'session.json'
|
||||
session_record.write_session_record(path,dict(operation_id='cancelled-owned',operation='verify',
|
||||
library=str(library),block='A',processes=[dict(role='native',**session_record.process_identity(native.pid))]))
|
||||
original=evidence/'result.json';original.write_text('{"status":"CANCELLED","safe_to_continue":false}')
|
||||
before=original.read_bytes()
|
||||
with library_lock.LibraryLock(library,block_name='A',safe_to_release=lambda:False) as lease:pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *a,**kw:[])
|
||||
active=Event();stop=Event();closed=[]
|
||||
owner=SimpleNamespace(pid=700,poll=lambda:0,close=lambda:closed.append(native.poll()))
|
||||
thread=None
|
||||
try:
|
||||
session_record.handoff_session(path,evidence_root=evidence,process=owner,stop=stop,
|
||||
progress=lambda state:active.set() if state['status']=='ACTIVE' else None)
|
||||
assert active.wait(3) and not closed
|
||||
key=str(path.absolute()).casefold()
|
||||
thread=session_record._observers[key][0]
|
||||
assert not session_record.library_available(library)
|
||||
queue=NativeQueue()
|
||||
def admitted():
|
||||
with library_lock.LibraryLock(library,checkpoint=False):return 'fresh admission'
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
future=pool.submit(queue.run,admitted,context=BatchTaskContext(str(library)),stop=stop)
|
||||
time.sleep(.15);assert not future.done()
|
||||
gate.touch();native.wait(5)
|
||||
assert future.result(8)=='fresh admission'
|
||||
thread.join(3)
|
||||
assert not thread.is_alive() and closed==[0]
|
||||
observations=[json.loads(p.read_text()) for p in evidence.glob('observation-*.json')]
|
||||
assert any(o.get('status')=='RELEASED' and o['release_proof']['proof_block_name']=='A' for o in observations)
|
||||
assert original.read_bytes()==before
|
||||
finally:
|
||||
gate.touch();native.wait(5);stop.set()
|
||||
if thread:thread.join(3)
|
||||
cleanup(lease)
|
||||
Reference in new issue
Block a user