"""Real Qt/management/admission boundaries, with only native work simulated.""" from dataclasses import asdict, replace import json from pathlib import Path from threading import Event import pytest from PySide6.QtWidgets import QMessageBox from test_management import setup, project @pytest.mark.parametrize('boundary,action,readback', [ (boundary, action, 'success') for boundary in ('remove', 'add', 'after-add') for action in ('button', 'close') ] + [('after-add', 'button', 'failed'), ('after-add', 'button', 'unknown_release')]) def test_soft_stop_finishes_current_transaction_and_skips_next(setup, qtbot, monkeypatch, boundary, action, readback): from cbb_refresh_verify import backup_recovery from cbb_refresh_verify.management import CatalogEntry from cbb_refresh_verify.native_management import NativeManagementAdapter from cbb_refresh_verify.operation_queue import NativeQueue from cbb_refresh_verify.ui import timed_prompt from cbb_refresh_verify.ui.management_dialog import ManagementDialog import win32con, win32event req, adapter, source = setup other = project(source.parent.parent/'other-source', 'Other') project(req.library.parent/'ReusableBlocksLP/Other/Work', 'Other') adapter.records.append(CatalogEntry('Other', 'Logical-Physical')) entered, release = Event(), Event() admitted, results = [], [] monkeypatch.setattr(backup_recovery, '_idle_check', lambda *a, **kw: None) # Native work is simulated here; machine WMI latency is not the stop/readback boundary. monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: []) monkeypatch.setattr(timed_prompt, 'question', lambda *a, **kw: QMessageBox.Yes) class Worker: def __init__(self, command, desktop): self.job = json.loads(Path(command[-1]).read_text()) self.folder = Path(command[-1]).parent self._start_event = win32event.CreateEvent(None, True, False, None) self.closed = False def bind_cancellation(self, path): pass def bind_session(self, job): self._start_binding = (job['session_record_path'], job['operation_id'], job['start_gate']) def poll(self): if win32event.WaitForSingleObject(self._start_event, 0) != win32con.WAIT_OBJECT_0: return None if self.folder.name == boundary: entered.set() assert release.wait(8) assert not Path(self.job['cancel_signal']).exists() if not (self.folder/'result.json').exists(): admitted.append(self.folder.name) failed = self.folder.name == 'after-add' and readback != 'success' (self.folder/'result.json').write_text(json.dumps(dict(success=not failed, safe_to_continue=not (failed and readback == 'unknown_release'), detail='readback failure' if failed else '', library=str(req.library), flow=1, entries=[asdict(r) for r in adapter.records]))) return 0 def close(self): if not self.closed: self._start_event.Close(); self.closed = True catalog = NativeManagementAdapter(mentor_home=req.library.parent, worker_factory=Worker) adapter.bind_stop = catalog.bind_stop adapter.catalog = catalog.catalog for name in ('remove', 'add'): original = getattr(adapter, name) def call(*args, original=original, name=name): result = original(*args) if name == boundary: entered.set() assert release.wait(8) return result setattr(adapter, name, call) requests = [replace(req, operation='update', source=source, auto_verify=False), replace(req, operation='update', name='Other', source=other, auto_verify=False)] dialog = ManagementDialog(requests, adapter=adapter) dialog.native_queue = NativeQueue() dialog.completed.connect(results.extend) qtbot.addWidget(dialog); dialog.show(); dialog.execute_button.click() try: qtbot.waitUntil(entered.is_set, timeout=10000) if action == 'button': dialog.stop_button.click() else: dialog.close() assert not dialog.context.stop.is_set(), 'Stop-after-current must not hard-cancel the admitted transaction' finally: release.set() qtbot.waitUntil(lambda: not dialog.busy, timeout=10000) assert [r.status for r in results] == ['UPLOADED' if readback == 'success' else 'RECOVERY_REQUIRED', 'SKIPPED'] record = json.loads(results[0].evidence_path.read_text(encoding='utf-8')) assert record['safe_to_continue'] is (readback != 'unknown_release') assert admitted == ['before', 'after-remove', 'after-add'] assert [r.name for r in adapter.records] == ['Other', 'Block'] assert len(adapter.calls) == 2 assert dialog.isVisible() def test_soft_stop_before_first_item_never_opens_catalog(setup): from cbb_refresh_verify.management import run_management_batch from cbb_refresh_verify.operation_queue import BatchTaskContext req, adapter, _ = setup context = BatchTaskContext(str(req.library)) context.stop_after_current.set() adapter.catalog = lambda *args: pytest.fail('Stopped batch must not open native catalog') results = context.run(lambda: run_management_batch([req], adapter=adapter, idle_check=lambda *a: None)) assert [r.status for r in results] == ['SKIPPED'] assert not context.stop.is_set() def test_soft_stop_at_terminal_result_skips_next_item(setup): from cbb_refresh_verify.management import run_management_batch from cbb_refresh_verify.operation_queue import BatchTaskContext req, adapter, _ = setup context = BatchTaskContext(str(req.library)) def progress(event): if event['stage'] == 'REMOVED': context.stop_after_current.set() results = context.run(lambda: run_management_batch([req, replace(req, name='Other')], adapter=adapter, idle_check=lambda *a: None, progress=progress)) assert [r.status for r in results] == ['REMOVED', 'SKIPPED'] assert adapter.calls == ['remove'] and not context.stop.is_set() def test_soft_stop_cancels_only_its_queued_batch(tmp_path): from threading import Thread from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue, QueueCancelled queue = NativeQueue(); waiting = Event(); outcomes = [] stopped = BatchTaskContext(str(tmp_path/'stopped.lmc')) independent = BatchTaskContext(str(tmp_path/'independent.lmc')) def queued(): try: queue.run(lambda: outcomes.append('unexpected'), context=stopped, ready=lambda: False, waiting=waiting.set) except QueueCancelled: outcomes.append('cancelled') thread = Thread(target=queued); thread.start() try: assert waiting.wait(3) stopped.stop_after_current.set(); queue.wake(); thread.join(3) assert not thread.is_alive() queue.run(lambda: outcomes.append('independent'), context=independent) assert outcomes == ['cancelled', 'independent'] assert not stopped.stop.is_set() and not independent.stop_after_current.is_set() finally: stopped.cancel(); queue.wake(); thread.join(3) @pytest.mark.parametrize('shutdown', [False, True]) def test_soft_stop_never_weakens_hard_worker_admission(tmp_path, shutdown): from types import SimpleNamespace from cbb_refresh_verify.operation_queue import BatchTaskContext, QueueCancelled, TaskHandoff, commit_worker_start import win32con, win32event context = BatchTaskContext(str(tmp_path/'lib.lmc'), native_policy='external') event = win32event.CreateEvent(None, True, False, None) job = dict(session_record_path=str(tmp_path/'session.json'), operation_id='exact', start_gate=str(tmp_path/'start.signal'), cancel_signal=str(tmp_path/'cancel.signal')) worker = SimpleNamespace(_start_event=event, _start_binding=(job['session_record_path'], job['operation_id'], job['start_gate'])) checked = Event() def action(): context.stop_after_current.set() if shutdown: context.shutdown() else: context.cancel() with pytest.raises(QueueCancelled): commit_worker_start(worker, job) checked.set() try: try: context.run(action) except TaskHandoff: pass assert checked.wait(3) assert win32event.WaitForSingleObject(event, 0) == win32con.WAIT_TIMEOUT assert not Path(job['start_gate']).exists() and Path(job['cancel_signal']).exists() finally: event.Close()