fix: isolate component failures and recover owned CAD resources
This commit is contained in:
1 parent
a8da30aad2
commit
4db5f05531
87 files changed
+6688
-301
No files matched your search
@@ -0,0 +1,240 @@
|
||||
"""Model and supervision waits must not own execution admission or capacity."""
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import QThreadPool, Signal
|
||||
|
||||
from dx_productivity.trial_task_center.ui.dsh_automation import DshAutomation
|
||||
from dx_productivity.trial_task_center.ui.workers import ServiceWorker
|
||||
from .test_dsh_native_menu import HarnessView
|
||||
from .test_dsh_operations import setup_view
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def single_execution_thread(ui, qtbot):
|
||||
ui.settle()
|
||||
pool = QThreadPool.globalInstance()
|
||||
previous = pool.maxThreadCount()
|
||||
pool.setMaxThreadCount(1)
|
||||
yield pool
|
||||
qtbot.waitUntil(lambda: pool.activeThreadCount() == 0, timeout=5000)
|
||||
pool.setMaxThreadCount(previous)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('source', ['timer', 'history', 'refresh_ticket'])
|
||||
def test_supervision_wait_leaves_execution_lane_and_thread_available(
|
||||
ui, monkeypatch, qtbot, single_execution_thread, source):
|
||||
setup_view(ui, monkeypatch)
|
||||
operations = ui.window._dsh_operations
|
||||
started, release, executed = Event(), Event(), Event()
|
||||
def read(*_args, **_kwargs):
|
||||
started.set()
|
||||
assert release.wait(5)
|
||||
return {'schedules': []}
|
||||
monkeypatch.setattr(operations, '_read', read)
|
||||
monkeypatch.setattr(operations.tickets, 'refresh', read)
|
||||
if source == 'timer':
|
||||
operations.tick()
|
||||
else:
|
||||
spec = {'operation': source}
|
||||
if source == 'refresh_ticket':
|
||||
spec['incident_id'] = 'a' * 24
|
||||
operations.request({'request_id': 'a' * 32, 'spec': spec})
|
||||
try:
|
||||
qtbot.waitUntil(started.is_set)
|
||||
assert operations.worker not in ui.window._workers
|
||||
assert not operations.busy
|
||||
ui.window._run_async(executed.set, lambda _: None)
|
||||
qtbot.waitUntil(executed.is_set, timeout=1000)
|
||||
assert not release.is_set()
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: operations.worker is None and not ui.window._workers)
|
||||
|
||||
|
||||
class DiagnosisView(HarnessView):
|
||||
diagnosis_event = Signal(object)
|
||||
is_running = True
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.requests = []
|
||||
|
||||
def request_diagnosis(self, request):
|
||||
self.requests.append(request)
|
||||
|
||||
def shutdown(self):
|
||||
self.is_running = False
|
||||
self.stopped.emit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def diagnosis(ui, monkeypatch, qtbot):
|
||||
from dx_productivity.trial_task_center.ui import dsh_automation as module
|
||||
ui.settle()
|
||||
monkeypatch.setattr(type(ui.window), '_create_dsh_view', lambda self: DiagnosisView(self))
|
||||
published = []
|
||||
monkeypatch.setattr(module, 'GitIssuePublisher', lambda _path: lambda *_args: published.append(True) or {'status': 'unavailable'})
|
||||
automation = DshAutomation(ui.window)
|
||||
ui.window._dsh_automation = automation
|
||||
# Bypass only offscreen enqueue suppression; exercise the real incident runner.
|
||||
automation.pending.append((ui.tasks[0].task_id, 'worker_error', 'controlled failure', True, 'old-run', None))
|
||||
automation._drain()
|
||||
qtbot.waitUntil(lambda: bool(ui.window._dsh_view.requests))
|
||||
yield automation, ui.window._dsh_view, published
|
||||
automation._native_unavailable = True
|
||||
automation._runtime_stopped()
|
||||
if hasattr(automation, 'close'):
|
||||
automation.close()
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
|
||||
|
||||
def test_model_wait_does_not_own_controls_or_starve_execution(single_execution_thread, diagnosis, ui, qtbot):
|
||||
automation, _view, _ = diagnosis
|
||||
executed = Event()
|
||||
try:
|
||||
assert automation.worker not in ui.window._workers
|
||||
assert ui.window._batch_stop is None
|
||||
assert ui.window._active_quick_run_task_id is None
|
||||
worker = ServiceWorker(executed.set)
|
||||
single_execution_thread.start(worker)
|
||||
qtbot.waitUntil(executed.is_set, timeout=1000)
|
||||
finally:
|
||||
automation.close()
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
|
||||
|
||||
def test_old_diagnosis_finishing_preserves_new_manual_run(diagnosis, ui, qtbot):
|
||||
automation, _, _ = diagnosis
|
||||
ui.window._begin_run(ui.tasks[1].task_id)
|
||||
manual_stop, run_id = ui.window._batch_stop, ui.window._dsh_run_id
|
||||
try:
|
||||
automation._runtime_stopped()
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
assert ui.window._batch_stop is manual_stop
|
||||
assert ui.window._dsh_run_id == run_id
|
||||
assert ui.window._dsh_live_run['active']
|
||||
assert not manual_stop.is_set()
|
||||
finally:
|
||||
ui.window._finish_run()
|
||||
|
||||
|
||||
def test_close_wakes_model_wait_and_suppresses_ticket_submission(diagnosis, ui, qtbot):
|
||||
automation, _, published = diagnosis
|
||||
ui.window.close()
|
||||
qtbot.waitUntil(lambda: automation.worker is None, timeout=2000)
|
||||
assert not published
|
||||
|
||||
|
||||
def test_cancelled_close_keeps_idle_automation_available(ui, monkeypatch):
|
||||
ui.settle()
|
||||
ui.window.archive_on_close_check.setChecked(False)
|
||||
automation = DshAutomation(ui.window)
|
||||
ui.window._dsh_automation = automation
|
||||
monkeypatch.setattr(ui.window.plan_panel, 'has_unsaved_plans', lambda: True)
|
||||
monkeypatch.setattr(ui.window, '_close_plan_confirmer', lambda: 'cancel')
|
||||
ui.window.close()
|
||||
assert not ui.window._closed
|
||||
assert not automation._closing
|
||||
|
||||
|
||||
def test_cancelled_close_after_model_cancellation_keeps_automation_available(diagnosis, ui, monkeypatch, qtbot):
|
||||
automation, _, published = diagnosis
|
||||
ui.window.archive_on_close_check.setChecked(False)
|
||||
monkeypatch.setattr(ui.window.plan_panel, 'has_unsaved_plans', lambda: True)
|
||||
monkeypatch.setattr(ui.window, '_close_plan_confirmer', lambda: 'cancel')
|
||||
ui.window.close()
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
ui.settle()
|
||||
assert not ui.window._closed and not automation._closing
|
||||
assert not published
|
||||
|
||||
|
||||
def test_mutation_admission_owns_lane_only_until_actual_action_finishes(ui, qtbot):
|
||||
from dx_productivity.trial_task_center.stop_control import StopControl
|
||||
ui.settle()
|
||||
automation = DshAutomation(ui.window)
|
||||
ui.window._dsh_automation = automation
|
||||
automation._observed_run_id = ui.window._dsh_run_id
|
||||
automation._action_task_id = ui.tasks[0].task_id
|
||||
automation._action_stop = StopControl()
|
||||
entered, release, reflected = Event(), Event(), Event()
|
||||
def action():
|
||||
with automation._action_scope('retry_failed'):
|
||||
entered.set()
|
||||
assert release.wait(5)
|
||||
reflected.set()
|
||||
assert automation.worker not in ui.window._workers
|
||||
return {'status': 'pending_issue'}
|
||||
automation._start_worker(ServiceWorker(action))
|
||||
try:
|
||||
qtbot.waitUntil(entered.is_set)
|
||||
assert automation.worker in ui.window._workers
|
||||
assert ui.window._batch_stop is automation._action_stop
|
||||
assert ui.window._active_quick_run_task_id == ui.tasks[0].task_id
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
assert reflected.is_set()
|
||||
assert not ui.window._workers
|
||||
assert ui.window._batch_stop is None
|
||||
|
||||
|
||||
def test_new_manual_run_denies_old_admission_without_touching_controls(ui, qtbot):
|
||||
from dx_productivity.trial_task_center.stop_control import StopControl
|
||||
ui.settle()
|
||||
automation = DshAutomation(ui.window)
|
||||
ui.window._dsh_automation = automation
|
||||
automation._observed_run_id = ui.window._dsh_run_id
|
||||
automation._action_task_id = ui.tasks[0].task_id
|
||||
automation._action_stop = StopControl()
|
||||
ui.window._begin_run(ui.tasks[1].task_id)
|
||||
manual_stop = ui.window._batch_stop
|
||||
rejected, mutated = Event(), Event()
|
||||
def action():
|
||||
try:
|
||||
with automation._action_scope('retry_failed'):
|
||||
mutated.set()
|
||||
except ValueError:
|
||||
rejected.set()
|
||||
return {'status': 'pending_issue'}
|
||||
try:
|
||||
automation._start_worker(ServiceWorker(action))
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
assert rejected.is_set() and not mutated.is_set()
|
||||
assert ui.window._batch_stop is manual_stop
|
||||
assert ui.window._active_quick_run_task_id == ui.tasks[1].task_id
|
||||
assert not manual_stop.is_set()
|
||||
finally:
|
||||
ui.window._finish_run()
|
||||
|
||||
|
||||
def test_reflection_waits_for_gui_to_confirm_lane_release(ui, qtbot):
|
||||
from dx_productivity.trial_task_center.stop_control import StopControl
|
||||
ui.settle()
|
||||
automation = DshAutomation(ui.window)
|
||||
ui.window._dsh_automation = automation
|
||||
automation._observed_run_id = ui.window._dsh_run_id
|
||||
automation._action_task_id = ui.tasks[0].task_id
|
||||
automation._action_stop = StopControl()
|
||||
releases, reflected = [], Event()
|
||||
automation.action_requested.disconnect(automation._admit_action)
|
||||
def gui(request):
|
||||
if request['release']:
|
||||
releases.append(request)
|
||||
else:
|
||||
automation._admit_action(request)
|
||||
automation.action_requested.connect(gui)
|
||||
def action():
|
||||
with automation._action_scope('retry_failed'):
|
||||
pass
|
||||
reflected.set()
|
||||
return {'status': 'pending_issue'}
|
||||
automation._start_worker(ServiceWorker(action))
|
||||
try:
|
||||
qtbot.waitUntil(lambda: bool(releases))
|
||||
qtbot.wait(5100)
|
||||
assert not reflected.is_set(), 'Model work started before GUI acknowledged release'
|
||||
finally:
|
||||
automation._admit_action(releases[0])
|
||||
qtbot.waitUntil(lambda: automation.worker is None)
|
||||
@@ -139,7 +139,7 @@ def test_last_execution_receipt_survives_later_catalog_reads(ui, monkeypatch, qt
|
||||
assert view.operations['last_execution']['tasks'][0]['no_match_items'] == 2
|
||||
|
||||
|
||||
def test_history_read_busy_state_is_published_and_rejection_is_visible(ui, monkeypatch, qtbot):
|
||||
def test_history_read_busy_state_is_published_without_blocking_execution_confirmation(ui, monkeypatch, qtbot):
|
||||
from threading import Event
|
||||
view=setup_view(ui,monkeypatch); operations=ui.window._dsh_operations
|
||||
started,release=Event(),Event()
|
||||
@@ -152,8 +152,9 @@ def test_history_read_busy_state_is_published_and_rejection_is_visible(ui, monke
|
||||
assert view.operations['busy'] is True
|
||||
ui.window._dsh_lifecycle.request({'request_id':'b'*32,'capability':'execute',
|
||||
'spec':{'operation':'start','task_ids':[ui.tasks[0].task_id]}})
|
||||
assert view.lifecycle_results[-1]['status']=='busy'
|
||||
assert view.operations['activity']['message']
|
||||
qtbot.waitUntil(lambda: ui.window._dsh_lifecycle.dialog is not None)
|
||||
ui.window._dsh_lifecycle.dialog.reject()
|
||||
assert view.lifecycle_results[-1]['status']=='cancelled'
|
||||
assert not ui.calls
|
||||
finally:
|
||||
release.set()
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
def test_owned_modal_can_be_handled_while_execution_worker_is_busy(ui, monkeypatch):
|
||||
window = ui.window
|
||||
ui.settle()
|
||||
worker = object()
|
||||
invoked = []
|
||||
monkeypatch.setattr('dx_productivity.trial_task_center.ui.cad_dialog_operations.show_cad_dialogs',
|
||||
lambda current: invoked.append(current))
|
||||
window.service._supervisor_dialog_state = {'supervision': {'blocked': True}}
|
||||
window._workers.add(worker)
|
||||
try:
|
||||
window._update_gates()
|
||||
button = window.run_panel.cleanup_resources_button
|
||||
assert not button.isHidden() and button.isEnabled()
|
||||
button.click()
|
||||
assert invoked == [window]
|
||||
finally:
|
||||
window._workers.discard(worker)
|
||||
window.service._supervisor_dialog_state = None
|
||||
window._update_gates()
|
||||
|
||||
|
||||
def test_retained_resource_force_requires_explicit_operator_choice(ui, monkeypatch):
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
window = ui.window
|
||||
ui.settle()
|
||||
forced = []
|
||||
monkeypatch.setattr(window.service, 'cleanup_batch_resources', lambda: ({'state': 'blocked'},))
|
||||
monkeypatch.setattr(window.service, 'force_pending_owned_resources',
|
||||
lambda *, confirmed=False: forced.append(confirmed) or ())
|
||||
monkeypatch.setattr(window, '_run_async', lambda action, done, *_a, **_k: done(action()))
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *_a, **_k: QMessageBox.StandardButton.No)
|
||||
window.run_panel.cleanup_resources_button.setEnabled(True)
|
||||
window._cleanup_batch_resources()
|
||||
assert forced == []
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *_a, **_k: QMessageBox.StandardButton.Yes)
|
||||
window.run_panel.cleanup_resources_button.setEnabled(True)
|
||||
window._cleanup_batch_resources()
|
||||
assert forced == [True]
|
||||
Reference in new issue
Block a user