fix: measure and verify queues at single UID granularity

This commit is contained in:
阿白叔 committed 2026-11-08 19:37:54 +08:00
1 parent e78cdd3c97
commit 4ee7b768bd
2 files changed
+58 -9

No files matched your search

+54 -4
View File
@@ -1,17 +1,67 @@
import csv
import importlib.util
import json
from pathlib import Path
import sqlite3
import pytest
@pytest.mark.parametrize('behavior', ['success', 'failure_continue', 'uncertain_stop', 'pause_resume'])
def test_multiple_projects_preserve_task_order_scope_results_and_measurements(tmp_path, behavior):
@pytest.mark.parametrize('behavior, steps, changed, states', [
('success', 24, 24, ['completed', 'completed', 'completed']),
('failure_continue', 24, 23, ['failed', 'completed', 'completed']),
('uncertain_stop', 1, 0, ['blocked', 'pending', 'pending']),
('pause_resume', 24, 24, ['completed', 'completed', 'completed']),
])
def test_multiple_projects_preserve_task_order_scope_results_and_measurements(tmp_path, behavior, steps, changed, states):
path = Path(__file__).resolve().parents[2] / 'tools/measure_task_queue.py'
spec = importlib.util.spec_from_file_location('measure_task_queue', path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
report = module.run_scenario(tmp_path / behavior, projects=3, sheets=2, rules=2, behavior=behavior)
output = tmp_path / behavior
report = module.run_scenario(output, projects=3, sheets=2, rules=2, behavior=behavior)
assert report['checks'] == 'passed'
assert report['executed_steps'] == steps
assert report['changed_components'] == changed
assert report['unselected_components_preserved'] == 12
assert report['result']['paused'] == (behavior == 'uncertain_stop')
assert [entry['state'] for entry in report['result']['entries']] == states
calls = report['calls']
assert len(calls) == steps
assert all(len(call['refdes']) == 1 for call in calls)
assert len({(call['project'], call['sheet'], call['refdes'][0]) for call in calls}) == steps
assert sum(call['state'] == 'saved_ok' for call in calls) == changed
assert [call['project'] for call in calls] == (['board-000'] if behavior == 'uncertain_stop' else
['board-000'] * 8 + ['board-001'] * 8 + ['board-002'] * 8)
assert calls[0]['state'] == {'failure_continue': 'batch_failed', 'uncertain_stop': 'batch_saved_unverified'}.get(behavior, 'saved_ok')
with sqlite3.connect(output / 'tasks.db') as connection:
rows = connection.execute('SELECT state,command_json,receipt_json FROM execution_operations').fetchall()
receipts = [json.loads(receipt) for _, _, receipt in rows if receipt]
assert len(receipts) == steps
assert {receipt['job_id']: receipt['native_state'] for receipt in receipts} == {
call['job_id']: call['state'] for call in calls}
assert sum(state == 'verified' for state, _, _ in rows) == changed
assert sum(state == 'failed' for state, _, _ in rows) == (behavior == 'failure_continue')
assert sum(state == 'uncertain' for state, _, _ in rows) == (behavior == 'uncertain_stop')
assert sum(state == 'pending' for state, _, _ in rows) == (7 if behavior == 'uncertain_stop' else 0)
for _, command, receipt in rows:
selector = json.loads(command)['target_selectors'].split('\x1e')
assert len(selector) == 1
uid, refdes, value = selector[0].split('\x1f')
assert uid.endswith(('-1', '-2'))
assert value in {'10K/NC', '10K'}
if receipt:
call = next(call for call in calls if call['job_id'] == json.loads(receipt)['job_id'])
assert call['refdes'] == [refdes]
events = [json.loads(line) for path in (output / 'data' / 'tasks').glob('*/events.jsonl')
for line in path.read_text(encoding='utf-8').splitlines()]
executed = [event for event in events if event['operation'] == 'quick_run_executed']
assert len(executed) == steps
assert all(event['details']['timing']['phase'] == 'execute_step' for event in executed)
assert {event['details']['timing']['job_id'] for event in executed} == {call['job_id'] for call in calls}
timings = report['timings']
assert len([row for row in timings if row['phase'] == 'execute_step']) == report['executed_steps']
assert len([row for row in timings if row['phase'] == 'execute_step']) == steps
assert all(row['elapsed_seconds'] >= 0 for row in timings)
with (output / 'timings.csv').open(encoding='utf-8-sig', newline='') as stream:
assert len([row for row in csv.DictReader(stream) if row['phase'] == 'execute_step']) == steps
assert json.loads((output / 'report.json').read_text(encoding='utf-8'))['executed_steps'] == steps