241 lines
9.1 KiB
Python
241 lines
9.1 KiB
Python
"""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)
|