fix: fence final worker admission against UI shutdown
This commit is contained in:
1 parent
dbc685b9a5
commit
eabf61c73b
13 files changed
+356
-46
No files matched your search
@@ -0,0 +1,205 @@
|
||||
"""Real producer/binding/worker admission, using only harmless Python children."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from threading import Event
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from test_issue15_ui_integration import window_for, load
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def transport(tmp_path):
|
||||
from cbb_refresh_verify.desktop_worker import DesktopWorker
|
||||
script = tmp_path/'harmless_worker.py'
|
||||
script.write_text('''import json,sys,time
|
||||
from pathlib import Path
|
||||
from cbb_refresh_verify.desktop_worker import run_native_worker
|
||||
from cbb_refresh_verify import native_management as management
|
||||
path=Path(sys.argv[1]); job=json.loads(path.read_text('utf-8')); root=path.parent
|
||||
def harmless(path):
|
||||
(root/'handler-entered').touch()
|
||||
while (root/'hold-handler').exists() and not (root/'finish').exists(): time.sleep(.01)
|
||||
(root/'result.json').write_text(json.dumps(dict(success=True,safe_to_continue=True,
|
||||
library=job['library'],flow=1,entries=[dict(name='A',partition='Logical-Physical',engineering_identity={})])))
|
||||
return 0
|
||||
if job['operation']=='catalog':
|
||||
management._current_desktop=lambda:(None,job['desktop'])
|
||||
def catalog(job,root):
|
||||
harmless(path)
|
||||
return json.loads((root/'result.json').read_text())
|
||||
management._catalog_native=catalog
|
||||
from cbb_refresh_verify.application import main
|
||||
execute=lambda:main(['--management-worker',str(path)])
|
||||
else: execute=lambda:run_native_worker(harmless,path)
|
||||
try: execute()
|
||||
except Exception as error: (root/'rejected').write_text(str(error),encoding='utf-8')
|
||||
''', encoding='utf-8')
|
||||
workers = []
|
||||
spawn = subprocess.Popen
|
||||
class Transport:
|
||||
bind_cancellation = DesktopWorker.bind_cancellation
|
||||
bind_session = DesktopWorker.bind_session
|
||||
request_stop = DesktopWorker.request_stop
|
||||
desktop = None
|
||||
def __init__(self, command, *args, **kwargs):
|
||||
self.path = Path(command[-1])
|
||||
self.process = spawn([sys.executable, str(script), str(self.path)])
|
||||
self.pid, self.handle = self.process.pid, self.process._handle
|
||||
workers.append(self)
|
||||
def poll(self): return self.process.poll()
|
||||
def wait(self, timeout=5): return self.process.wait(timeout)
|
||||
def close(self): self.process.wait(timeout=5)
|
||||
yield Transport, workers
|
||||
for worker in workers:
|
||||
worker.path.with_name('finish').touch()
|
||||
if worker.poll() is None:
|
||||
Path(json.loads(worker.path.read_text('utf-8'))['cancel_signal']).touch()
|
||||
worker.wait(6)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('entry', ['initial', 'verify', 'icdb'])
|
||||
def test_real_catalog_adapter_enters_initial_and_execution_reads(qtbot, tmp_path, monkeypatch, transport, entry):
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext
|
||||
Transport, workers = transport
|
||||
adapter = NativeManagementAdapter(worker_factory=Transport)
|
||||
window = window_for(qtbot, tmp_path/'ui', monkeypatch,
|
||||
catalog_reader=adapter.catalog, status_refresher=lambda names, _: {name:'PENDING' for name in names})
|
||||
library = tmp_path/'library.lmc'; library.write_bytes(b'isolated')
|
||||
if entry == 'initial':
|
||||
load(window, qtbot, str(library))
|
||||
qtbot.waitUntil(lambda: not any(j.kind=='catalog' and j.active for j in window._jobs.values()), timeout=6000)
|
||||
assert window._records_by_name['A'].catalog_source == 'both', window.status_label.text()
|
||||
else:
|
||||
context = BatchTaskContext(str(library), native_policy='external')
|
||||
reader = window._execution_status_reader(entry, None)
|
||||
assert context.run(lambda: reader(('A', 'Missing'), library)) == {'A':'PENDING','Missing':'NOT_REGISTERED'}
|
||||
assert len(workers) == 1
|
||||
record = json.loads(workers[0].path.with_name('session.json').read_text('utf-8'))
|
||||
assert record['worker_protection'] == 'catalog_readonly'
|
||||
assert record['worker_lease_handle'] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pause', ['after_bind', 'final_preparation'])
|
||||
def test_shutdown_wins_after_real_binding_and_returns_ui_waiter(qtbot, tmp_path, monkeypatch, transport, pause):
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
from cbb_refresh_verify.library_lock import LibraryLock
|
||||
Transport, workers = transport
|
||||
entered, resume = Event(), Event()
|
||||
original_bind, original_touch = Transport.bind_session, Path.touch
|
||||
if pause == 'after_bind':
|
||||
def bind(self, job):
|
||||
original_bind(self, job); entered.set(); assert resume.wait(6)
|
||||
Transport.bind_session = bind
|
||||
else:
|
||||
def touch(path, *args, **kwargs):
|
||||
if path.name == 'start-management.signal':
|
||||
entered.set(); assert resume.wait(6)
|
||||
return original_touch(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'touch', touch)
|
||||
library = tmp_path/'lib.lmc'; library.write_bytes(b'isolated')
|
||||
adapter = NativeManagementAdapter(worker_factory=Transport)
|
||||
window = window_for(qtbot, tmp_path/'ui', monkeypatch)
|
||||
job = window._new_job('verify', library=library)
|
||||
def action():
|
||||
with LibraryLock(library, checkpoint=False):
|
||||
return adapter._worker(dict(operation='remove',library=str(library)),tmp_path/'operation')
|
||||
window._start(action, lambda _:None, job=job)
|
||||
qtbot.waitUntil(entered.is_set, timeout=4000)
|
||||
try:
|
||||
state = job.context.shutdown()
|
||||
assert state['shutdown_handoff']
|
||||
qtbot.waitUntil(lambda: not window.busy, timeout=1000)
|
||||
resume.set()
|
||||
qtbot.waitUntil(lambda: not job.context.snapshot()['pending'], timeout=4000)
|
||||
qtbot.waitUntil(lambda: workers[0].poll() is not None, timeout=4000)
|
||||
assert not (tmp_path/'operation'/'handler-entered').exists()
|
||||
finally:
|
||||
resume.set()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('route', ['verify', 'backup', 'repair'])
|
||||
@pytest.mark.parametrize('pause', ['after_bind', 'final_preparation'])
|
||||
def test_other_real_producers_share_final_admission_fence(qtbot,tmp_path,monkeypatch,transport,route,pause):
|
||||
from cbb_refresh_verify import desktop_worker, native_backup, native_repair, session_record
|
||||
from cbb_refresh_verify.library_lock import LibraryLock
|
||||
from cbb_refresh_verify.verify_runner import run_verify
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
from test_native_repair import target_fixture
|
||||
Transport, workers = transport
|
||||
entered, resume = Event(), Event()
|
||||
original_bind, original_register, original_touch = Transport.bind_session, session_record.register_worker_session, Path.touch
|
||||
if pause == 'after_bind':
|
||||
def bind(self, job):
|
||||
original_bind(self, job); entered.set(); assert resume.wait(8)
|
||||
Transport.bind_session = bind
|
||||
if route == 'backup':
|
||||
def register(worker,job):
|
||||
original_register(worker,job); entered.set(); assert resume.wait(8)
|
||||
monkeypatch.setattr(session_record,'register_worker_session',register)
|
||||
else:
|
||||
def touch(path,*args,**kwargs):
|
||||
if path.name in ('start-worker.signal','start-repair.signal'):
|
||||
entered.set(); assert resume.wait(8)
|
||||
return original_touch(path,*args,**kwargs)
|
||||
monkeypatch.setattr(Path,'touch',touch)
|
||||
monkeypatch.setattr(desktop_worker,'DesktopWorker',Transport)
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *_:[])
|
||||
if route == 'repair':
|
||||
req,_,target = target_fixture(tmp_path)
|
||||
def action():
|
||||
with LibraryLock(req.lmc_file,checkpoint=False):
|
||||
return native_repair.run_native_repair(target,tmp_path/'repair',maintenance_context={'library':str(req.lmc_file)})
|
||||
elif route == 'backup':
|
||||
from test_native_backup import project_fixture
|
||||
req,_ = project_fixture(tmp_path)
|
||||
monkeypatch.setattr(native_backup,'native_backup_executable',lambda *_:tmp_path/'unused.exe')
|
||||
monkeypatch.setattr(native_backup.subprocess,'Popen',Transport)
|
||||
action=lambda:native_backup.launch_native_backup(req)
|
||||
else:
|
||||
fixture(tmp_path); req=request(tmp_path)
|
||||
action=lambda:run_verify(req,port_check=PORTS_OK)
|
||||
window=window_for(qtbot,tmp_path/'ui',monkeypatch)
|
||||
job=window._new_job('verify',library=req.lmc_file)
|
||||
window._start(action,lambda _:None,job=job)
|
||||
try:
|
||||
qtbot.waitUntil(entered.is_set,timeout=6000)
|
||||
assert job.context.shutdown()['shutdown_handoff']
|
||||
qtbot.waitUntil(lambda:not window.busy,timeout=1500)
|
||||
resume.set()
|
||||
qtbot.waitUntil(lambda:not job.context.snapshot()['pending'],timeout=6000)
|
||||
qtbot.waitUntil(lambda:workers[0].poll() is not None,timeout=4000)
|
||||
assert not workers[0].path.with_name('handler-entered').exists()
|
||||
finally:
|
||||
resume.set()
|
||||
|
||||
|
||||
def test_catalog_read_role_requires_the_actual_readonly_worker_action(tmp_path):
|
||||
from cbb_refresh_verify.desktop_worker import run_native_worker
|
||||
from cbb_refresh_verify.native_management import catalog_worker_main,management_worker_main,_catalog_com,catalog_dialog_action
|
||||
from cbb_refresh_verify.session_record import register_worker_session
|
||||
from types import SimpleNamespace
|
||||
path=tmp_path/'request.json'
|
||||
job=dict(operation='catalog',worker_role='catalog_readonly',operation_id='read',
|
||||
library=str(tmp_path/'lib.lmc'),session_record_path=str(tmp_path/'session.json'))
|
||||
path.write_text(json.dumps(job))
|
||||
register_worker_session(SimpleNamespace(pid=os.getpid()),job)
|
||||
effects=[]
|
||||
def impostor(path): effects.append('write')
|
||||
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
|
||||
run_native_worker(impostor,path)
|
||||
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
|
||||
management_worker_main(path)
|
||||
job['operation']='add';path.write_text(json.dumps(job))
|
||||
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
|
||||
catalog_worker_main(path)
|
||||
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
|
||||
_catalog_com(job,tmp_path)
|
||||
assert effects==[]
|
||||
assert catalog_dialog_action('Library Manager','Would you like to save?',read_only=True)=='cancel'
|
||||
assert catalog_dialog_action('Upgrade library','Continue?',read_only=True)=='cancel'
|
||||
Reference in new issue
Block a user