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
+4 -5
View File
@@ -72,7 +72,7 @@ def run_scenario(output: Path, *, projects=3, sheets=2, rules=2, behavior='succe
assert item['target_scope'] == 'targets'
selected = [entry.split('\x1f') for entry in _decode_vbs_field(item['target_selectors']).split('\x1e')]
matched = [t for t in inventory if t.sheet_number == item['sheet_number'] and t.uid in {s[0] for s in selected}]
assert len(matched) == 2
assert len(selected) == len(matched) == 1
assert {(t.uid, t.refdes, t.old_value) for t in matched} == {tuple(s) for s in selected}
assert all(model[t.key] == item['old_part'] for t in matched)
state = 'saved_ok'
@@ -131,9 +131,8 @@ def run_scenario(output: Path, *, projects=3, sheets=2, rules=2, behavior='succe
assert result.paused and len(calls) == 2
stop.clear()
result = measured('queue_resume_wall', lambda: execute_task_queue(service, ids, stop_event=stop, queue_id=result.queue_id))
expected_steps = projects * sheets * rules
expected_calls = (1 if behavior == 'uncertain_stop' else
1 + (projects-1)*sheets*rules if behavior == 'failure_continue' else expected_steps)
expected_steps = projects * sheets * rules * 2
expected_calls = 1 if behavior == 'uncertain_stop' else expected_steps
assert len(calls) == expected_calls
if behavior == 'uncertain_stop':
assert result.paused and result.entries[0].state == 'blocked'
@@ -145,7 +144,7 @@ def run_scenario(output: Path, *, projects=3, sheets=2, rules=2, behavior='succe
order = [call['task_id'] for call in calls]
assert order == sorted(order, key=ids.index)
changed = sum(model[t.key] != t.old_part_number for inventory in targets.values() for t in inventory)
assert changed == sum(call['state'] == 'saved_ok' for call in calls) * 2
assert changed == sum(call['state'] == 'saved_ok' for call in calls)
assert all(model[t.key] == t.old_part_number for inventory in targets.values() for t in inventory if int(t.uid.split('-')[-1]) == 3)
for task in tasks:
events = [json.loads(line) for line in service.tail_log(task.task_id, limit=None)]