fix: finish current management item before stopping its batch
This commit is contained in:
1 parent
fd50805380
commit
e0bb2438e8
5 files changed
+178
-7
No files matched your search
@@ -465,7 +465,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
def run_management_batch(requests, *, stop=None, progress=None, item_executor=None, context=None, **kwargs):
|
||||
from threading import Event
|
||||
from .write_admission import WriteAdmission
|
||||
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys, remember_result
|
||||
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys, remember_result, current_context
|
||||
context = context or current_context()
|
||||
if context is not None:
|
||||
stop = stop if stop is not None else context.stop
|
||||
progress = context.guard_callback(progress) if progress else None
|
||||
@@ -477,7 +478,8 @@ def run_management_batch(requests, *, stop=None, progress=None, item_executor=No
|
||||
cancelled = False
|
||||
for request in requests:
|
||||
keys = request_library_keys(request, context)
|
||||
stopped = cancelled or stop.is_set() or context is not None and context.stop.is_set()
|
||||
stopped = (cancelled or stop.is_set() or context is not None
|
||||
and (context.stop.is_set() or context.stop_after_current.is_set()))
|
||||
if stopped or keys.intersection(retained_libraries):
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', 'SKIPPED', True,
|
||||
|
||||
@@ -237,6 +237,8 @@ class BatchTaskContext:
|
||||
stop: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False)
|
||||
_closed: Event = field(default_factory=Event, repr=False, compare=False)
|
||||
native_policy: str = field(default='legacy', kw_only=True)
|
||||
# Only queue/batch boundaries consume this; the current item retains hard-stop semantics.
|
||||
stop_after_current: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False, kw_only=True)
|
||||
|
||||
def __post_init__(self):
|
||||
if self.native_policy not in ('legacy', 'restricted', 'external'):
|
||||
@@ -402,7 +404,7 @@ class NativeQueue:
|
||||
available = ready() # Call only this ticket's predicate, outside the queue lock.
|
||||
with self.condition:
|
||||
if (self.closed or stop is not None and stop.is_set()
|
||||
or context is not None and context.stop.is_set()):
|
||||
or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())):
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
ticket[1] = bool(available)
|
||||
first = next((entry for entry in self.pending if entry[1]), None)
|
||||
@@ -422,7 +424,8 @@ class NativeQueue:
|
||||
notified, previous_state = True, current_state
|
||||
except Exception:
|
||||
pass
|
||||
if stop is not None and stop.is_set():
|
||||
if (stop is not None and stop.is_set()
|
||||
or context is not None and context.stop_after_current.is_set()):
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
return context.run(action, stop=stop, timeout_seconds=timeout_seconds) if context else action()
|
||||
finally:
|
||||
|
||||
@@ -100,7 +100,7 @@ class ManagementDialog(QDialog):
|
||||
self.execute_button = QPushButton('预览并确认执行…')
|
||||
self.execute_button.clicked.connect(self._execute)
|
||||
self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False)
|
||||
self.stop_button.clicked.connect(lambda:self.stop.set())
|
||||
self.stop_button.clicked.connect(lambda:self.context.stop_after_current.set())
|
||||
self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject)
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(QLabel(f'目标库:{self.template.library}'))
|
||||
@@ -305,6 +305,6 @@ class ManagementDialog(QDialog):
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self.busy:
|
||||
self.stop.set(); event.ignore()
|
||||
self.context.stop_after_current.set(); event.ignore()
|
||||
else:
|
||||
super().closeEvent(event)
|
||||
@@ -280,7 +280,7 @@ def test_management_confirm_cancel_execute_stop_and_repeated_click(controls, qtb
|
||||
qtbot.mouseClick(dialog.close_button, Qt.LeftButton)
|
||||
assert len(calls) == 1 and dialog.isVisible()
|
||||
press(qtbot, dialog.stop_button)
|
||||
assert dialog.stop.is_set()
|
||||
assert dialog.context.stop_after_current.is_set() and not dialog.stop.is_set()
|
||||
finally:
|
||||
finish.set()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
"""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)
|
||||
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()
|
||||
Reference in new issue
Block a user