feat: keep task browsing and paged logs responsive during execution
This commit is contained in:
1 parent
3e45040def
commit
78f3b0c32a
10 files changed
+646
-46
No files matched your search
@@ -0,0 +1,63 @@
|
||||
"""Catch lost/duplicated events across pagination, partial writes and rotation."""
|
||||
from pathlib import Path
|
||||
from threading import RLock
|
||||
|
||||
from dx_productivity.trial_task_center.task_logs import TaskLogReader
|
||||
|
||||
|
||||
def test_pages_cover_rotated_history_without_duplicates(tmp_path):
|
||||
path = tmp_path / 'events.jsonl'
|
||||
path.with_name(path.name + '.1').write_text('旧一\n旧二\n', encoding='utf-8')
|
||||
path.write_text('新一\n新二\n新三\n', encoding='utf-8')
|
||||
reader = TaskLogReader(path, RLock(), backup_count=5)
|
||||
latest = reader.latest(limit=2)
|
||||
assert latest.lines == ('新二', '新三') and latest.has_older
|
||||
previous = reader.older(latest.older_cursor, limit=2)
|
||||
assert previous.lines == ('旧二', '新一') and previous.has_older
|
||||
first = reader.older(previous.older_cursor, limit=2)
|
||||
assert first.lines == ('旧一',) and not first.has_older
|
||||
assert reader.poll(latest.cursor).lines == ()
|
||||
|
||||
|
||||
def test_incremental_poll_handles_partial_utf8_and_rotation(tmp_path):
|
||||
path = tmp_path / 'events.jsonl'
|
||||
path.write_bytes('first\n'.encode())
|
||||
reader = TaskLogReader(path, RLock(), backup_count=5)
|
||||
cursor = reader.latest().cursor
|
||||
payload = '位号 R1\n'.encode('utf-8')
|
||||
with path.open('ab') as stream:
|
||||
stream.write(payload[:2])
|
||||
incomplete = reader.poll(cursor)
|
||||
assert incomplete.lines == () and incomplete.cursor == cursor
|
||||
with path.open('ab') as stream:
|
||||
stream.write(payload[2:])
|
||||
path.rename(path.with_name(path.name + '.1'))
|
||||
path.write_text('next\n', encoding='utf-8')
|
||||
page = reader.poll(cursor)
|
||||
assert page.lines == ('位号 R1', 'next') and not page.reset
|
||||
assert reader.poll(page.cursor).lines == ()
|
||||
|
||||
|
||||
def test_truncated_log_resets_and_empty_log_can_later_be_created(tmp_path):
|
||||
path = tmp_path / 'events.jsonl'
|
||||
reader = TaskLogReader(path, RLock(), backup_count=5)
|
||||
empty = reader.latest()
|
||||
assert not empty.lines
|
||||
path.write_text('abcdef\n', encoding='utf-8')
|
||||
first = reader.poll(empty.cursor)
|
||||
assert first.lines == ('abcdef',)
|
||||
path.write_text('xy\n', encoding='utf-8')
|
||||
reset = reader.poll(first.cursor)
|
||||
assert reset.reset and reset.lines == ('xy',)
|
||||
|
||||
|
||||
def test_poll_is_bounded_and_retains_unread_events(tmp_path):
|
||||
path = tmp_path / 'events.jsonl'
|
||||
path.touch()
|
||||
reader = TaskLogReader(path, RLock(), backup_count=5)
|
||||
cursor = reader.latest().cursor
|
||||
path.write_text('1\n2\n3\n4\n5\n', encoding='utf-8')
|
||||
a = reader.poll(cursor, limit=2)
|
||||
b = reader.poll(a.cursor, limit=2)
|
||||
c = reader.poll(b.cursor, limit=2)
|
||||
assert (a.lines, b.lines, c.lines) == (('1', '2'), ('3', '4'), ('5',))
|
||||
@@ -12,7 +12,7 @@ from dx_productivity.trial_task_center.ui.project_plan import DirectoryTaskDialo
|
||||
|
||||
def test_directory_dialog_selects_individual_projects_and_invalidates_old_scan(qtbot, tmp_path):
|
||||
projects = (tmp_path / 'left' / 'same.prj', tmp_path / 'right' / 'same.prj')
|
||||
service = SimpleNamespace(scan_project_directory=lambda root: projects)
|
||||
service = SimpleNamespace(scan_project_directory=lambda root, **kwargs: projects)
|
||||
dialog = DirectoryTaskDialog(service)
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.directory.setText(str(tmp_path))
|
||||
@@ -51,6 +51,7 @@ def test_original_window_edits_target_plan_and_nc_without_export(qtbot, tmp_path
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
assert '1.1.0' in window.windowTitle()
|
||||
table = window.plan_panel.table
|
||||
table.item(0, 0).setCheckState(Qt.CheckState.Checked)
|
||||
@@ -58,6 +59,7 @@ def test_original_window_edits_target_plan_and_nc_without_export(qtbot, tmp_path
|
||||
assert not window.run_panel.quick_run_button.isEnabled()
|
||||
window.plan_panel.save()
|
||||
qtbot.waitUntil(lambda: not window._workers, timeout=5000)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
rows = service.target_plan(task.task_id)
|
||||
assert rows[0].enabled and not rows[0].sync_nc and not rows[1].enabled
|
||||
assert not table.dirty
|
||||
@@ -90,6 +92,7 @@ def test_single_project_summary_and_local_gates_do_not_query_service_again(qtbot
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
assert window.findChild(QTableWidget, 'projectTable') is None
|
||||
for name in ('scanProjectsButton', 'selectAllProjectsButton', 'clearProjectsButton',
|
||||
'retryProjectButton', 'skipProjectButton', 'startTaskButton'):
|
||||
@@ -117,6 +120,7 @@ def test_project_plan_exposes_bom_import_and_template_actions(qtbot, tmp_path):
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_project_tasks(root)[0]
|
||||
window = TrialTaskCenterWindow(service); qtbot.addWidget(window); window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
assert window.findChild(QPushButton, 'importBomPlan').text() == '导入 BOM'
|
||||
assert window.findChild(QPushButton, 'exportBomTemplate').text() == 'BOM 模板'
|
||||
assert not window.task_list.filter.isVisible()
|
||||
@@ -135,6 +139,7 @@ def test_batch_rules_and_target_plan_are_above_project_summary(qtbot, tmp_path):
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_project_tasks(root)[0]
|
||||
window = TrialTaskCenterWindow(service); qtbot.addWidget(window); window.open_task(task.task_id); window.show()
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitExposed(window)
|
||||
assert window.mapping_tabs.objectName() == 'batchPlanTabs'
|
||||
assert window.mapping_tabs.geometry().top() < window.project_summary.geometry().top()
|
||||
|
||||
@@ -65,12 +65,16 @@ def test_all_steps_survive_reopening_completed_task(qtbot, tmp_path):
|
||||
service.add_rule(task.task_id, 'OLD2', 'NEW2')
|
||||
window = TrialTaskCenterWindow(service); qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(lambda: not window._log_presenter.query.busy)
|
||||
assert not window.run_panel.toggle_log_button.isChecked()
|
||||
progress = service.quick_run_progress(task.task_id)
|
||||
result = service.execute_quick_run_queue(task.task_id,
|
||||
expected_item=(str(progress.next_project_path), 'OLD', 'NEW', 'preserve'), stop_event=Event())
|
||||
assert result.progress.queue_complete and len(calls) == 2
|
||||
window._show_batch_result(result)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(lambda: not window._log_presenter.query.busy)
|
||||
assert window.run_panel.toggle_log_button.isChecked()
|
||||
assert window.run_panel.log_view.toPlainText().count('位号 R1:OLD → NEW') == 1
|
||||
window.close(); store.close()
|
||||
@@ -78,6 +82,8 @@ def test_all_steps_survive_reopening_completed_task(qtbot, tmp_path):
|
||||
service = TaskCenterService(store, configure_logging(data))
|
||||
window = TrialTaskCenterWindow(service); qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(lambda: not window._log_presenter.query.busy)
|
||||
assert window.run_panel.toggle_log_button.isChecked()
|
||||
text = window.run_panel.log_view.toPlainText()
|
||||
assert '位号 R1:OLD → NEW' in text
|
||||
@@ -85,6 +91,8 @@ def test_all_steps_survive_reopening_completed_task(qtbot, tmp_path):
|
||||
# Switching to a draft clears the previous task's modification details.
|
||||
other = service.create_task('another')
|
||||
window.open_task(other.task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(lambda: not window._log_presenter.query.busy)
|
||||
assert '位号 R1' not in window.run_panel.log_view.toPlainText()
|
||||
window.close(); store.close()
|
||||
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
"""Exercise the event loop and task ownership while external I/O is delayed."""
|
||||
import os
|
||||
os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen')
|
||||
|
||||
from threading import Event, get_ident
|
||||
from time import perf_counter
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import QTimer
|
||||
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def browser(qtbot, tmp_path):
|
||||
root = tmp_path / 'projects'
|
||||
root.mkdir()
|
||||
for name in ('a', 'b'):
|
||||
path = root / name / 'board.prj'
|
||||
path.parent.mkdir()
|
||||
path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
tasks = service.create_project_tasks(root)
|
||||
for task in tasks:
|
||||
service.add_rule(task.task_id, 'OLD', 'NEW')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
yield window, service, tasks
|
||||
qtbot.waitUntil(lambda: not window._workers, timeout=5000)
|
||||
qtbot.waitUntil(lambda: not window._context_query.busy and not window._task_query.busy
|
||||
and not window._log_presenter.query.busy, timeout=5000)
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
def loaded(qtbot, window, task):
|
||||
qtbot.waitUntil(lambda: window._context is not None and not window._loading
|
||||
and window._context.task.task_id == task.task_id, timeout=3000)
|
||||
|
||||
|
||||
def test_slow_task_read_is_background_and_stale_result_cannot_change_selection(browser, qtbot, monkeypatch):
|
||||
window, service, (a, b) = browser
|
||||
real = service.task_context
|
||||
entered, release = Event(), Event()
|
||||
threads = []
|
||||
def delayed(task_id):
|
||||
threads.append(get_ident())
|
||||
if task_id == a.task_id:
|
||||
entered.set()
|
||||
release.wait(0.4)
|
||||
return real(task_id)
|
||||
monkeypatch.setattr(service, 'task_context', delayed)
|
||||
try:
|
||||
start = perf_counter()
|
||||
window.open_task(a.task_id)
|
||||
assert perf_counter() - start < 0.15
|
||||
qtbot.waitUntil(entered.is_set)
|
||||
tick = []
|
||||
QTimer.singleShot(0, lambda: tick.append(True))
|
||||
window.open_task(b.task_id)
|
||||
qtbot.waitUntil(lambda: bool(tick))
|
||||
assert window._task_id == b.task_id
|
||||
finally:
|
||||
release.set()
|
||||
loaded(qtbot, window, b)
|
||||
assert all(thread != get_ident() for thread in threads)
|
||||
assert window.project_summary.path.text().endswith('b\\board.prj')
|
||||
|
||||
|
||||
def test_execution_allows_read_only_browsing_and_completion_survives_task_switch(browser, qtbot):
|
||||
window, _, (a, b) = browser
|
||||
window.open_task(a.task_id)
|
||||
loaded(qtbot, window, a)
|
||||
release = Event()
|
||||
finished = []
|
||||
window._begin_run(a.task_id)
|
||||
stop = window._batch_stop
|
||||
def done(result):
|
||||
finished.append(result)
|
||||
window._finish_run()
|
||||
try:
|
||||
window._run_async(lambda: (release.wait(2), 'completed')[1], done, expected_task_id=a.task_id)
|
||||
assert window.task_list.list.isEnabled()
|
||||
window.open_task(b.task_id)
|
||||
loaded(qtbot, window, b)
|
||||
assert not window.add_mapping_button.isEnabled()
|
||||
assert not window.run_panel.quick_run_button.isEnabled()
|
||||
assert window.plan_panel.find_button.isEnabled()
|
||||
window._pause_queue()
|
||||
assert stop.is_set()
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
assert finished == ['completed']
|
||||
assert window._task_id == b.task_id and window._batch_stop is None
|
||||
|
||||
|
||||
def test_slow_log_read_does_not_block_or_leak_previous_task(browser, qtbot, monkeypatch, tmp_path):
|
||||
window, service, (a, b) = browser
|
||||
for task, text in ((a, 'OLD_TASK_ONLY'), (b, 'CURRENT_TASK')):
|
||||
path = tmp_path / 'data' / 'tasks' / task.task_id / 'events.jsonl'
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text + '\n')
|
||||
real = service.read_task_log
|
||||
entered, release = Event(), Event()
|
||||
def delayed(task_id, **kwargs):
|
||||
if task_id == a.task_id:
|
||||
entered.set()
|
||||
release.wait(0.4)
|
||||
return real(task_id, **kwargs)
|
||||
monkeypatch.setattr(service, 'read_task_log', delayed)
|
||||
window.open_task(a.task_id)
|
||||
loaded(qtbot, window, a)
|
||||
try:
|
||||
start = perf_counter()
|
||||
window._show_task_log()
|
||||
assert perf_counter() - start < 0.15
|
||||
qtbot.waitUntil(entered.is_set)
|
||||
window.open_task(b.task_id)
|
||||
loaded(qtbot, window, b)
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: 'CURRENT_TASK' in window.run_panel.log_view.toPlainText())
|
||||
assert 'OLD_TASK_ONLY' not in window.run_panel.log_view.toPlainText()
|
||||
|
||||
|
||||
def test_refresh_ticks_do_not_starve_initial_log_read(browser, qtbot, monkeypatch, tmp_path):
|
||||
window, service, (a, _) = browser
|
||||
path = tmp_path / 'data' / 'tasks' / a.task_id / 'events.jsonl'
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text('FIRST_PAGE\n')
|
||||
real = service.read_task_log
|
||||
entered, release = Event(), Event()
|
||||
reads = []
|
||||
def delayed(task_id, **kwargs):
|
||||
reads.append(task_id)
|
||||
entered.set()
|
||||
release.wait(0.4)
|
||||
return real(task_id, **kwargs)
|
||||
monkeypatch.setattr(service, 'read_task_log', delayed)
|
||||
window.open_task(a.task_id)
|
||||
loaded(qtbot, window, a)
|
||||
try:
|
||||
window._show_task_log()
|
||||
qtbot.waitUntil(entered.is_set)
|
||||
for _ in range(5):
|
||||
window._refresh_live_task_log()
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: not window._log_presenter.query.busy)
|
||||
assert 'FIRST_PAGE' in window.run_panel.log_view.toPlainText()
|
||||
assert reads == [a.task_id]
|
||||
|
||||
|
||||
def test_completion_names_execution_owner_when_browsing_another_task(browser, qtbot):
|
||||
from types import SimpleNamespace
|
||||
window, _, (a, b) = browser
|
||||
window.open_task(a.task_id)
|
||||
loaded(qtbot, window, a)
|
||||
window._begin_run(a.task_id)
|
||||
window.open_task(b.task_id)
|
||||
loaded(qtbot, window, b)
|
||||
result = SimpleNamespace(paused=False, progress=SimpleNamespace(saved_items=3, failed_items=0, no_match_items=0))
|
||||
window._show_batch_result(result)
|
||||
assert a.name in window.run_panel.operator_band.text()
|
||||
assert not window.run_panel.toggle_log_button.isChecked()
|
||||
Reference in new issue
Block a user