"""Full real scheduler/journal acceptance with a controlled native executor.""" import csv import subprocess from collections import Counter from pathlib import Path from threading import Event from dx_productivity.trial_task_center.logging_setup import configure_logging from dx_productivity.trial_task_center.quick_run import QuickRunPreparer, _decode_vbs_field from dx_productivity.trial_task_center.service import TaskCenterService from dx_productivity.trial_task_center.store import TaskStore from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow from dx_productivity.trial_task_center.task_queue import execute_task_queue def test_hundred_uid_batch_keeps_93_verified_items_and_isolates_unknown_writes(tmp_path): from test_project_isolation_queue import ReleasedRuntime calls = [] outcomes = {} def worker(command, timeout): root = Path(command[3]) with (root / 'current-command.tsv').open(encoding='utf-8-sig') as stream: row = {key: _decode_vbs_field(value) for key, value in next(csv.DictReader(stream, delimiter='\t')).items()} uid, refdes, _ = row['target_selectors'].split('\x1f') assert '\x1e' not in row['target_selectors'] key = row['task_id'], uid calls.append(key) state = outcomes.get(key, 'saved_ok') with (root / 'status.tsv').open('a', encoding='utf-8') as stream: if state == 'saved_ok': stream.write(f'{command[-1]}\tawaiting_native_replace\t\tcaptured\n') stream.write(f'{command[-1]}\t{state}\tbatch_closed\tControlled {refdes}\n') return subprocess.CompletedProcess(command, 0, '', '') for name in ('A', 'B', 'C', 'D'): project = tmp_path / 'projects' / name / 'board.prj' project.parent.mkdir(parents=True) project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n') data = tmp_path / 'data' database = tmp_path / 'tasks.db' store = TaskStore(database) preparer = QuickRunPreparer(data, process_runner=worker, cscript_resolver=lambda: Path('controlled-native'), sheet_discoverer=lambda _: (('MAIN', '1'),)) engine = TaskCenterService(store, configure_logging(data), quick_run_preparer=preparer) tasks = engine.create_project_tasks(tmp_path / 'projects') counts = (48, 50, 1, 1) for index, (task, count) in enumerate(zip(tasks, counts)): project = engine.projects(task.task_id)[0].path targets = tuple(ComponentTarget(project, 'MAIN', '1', f'u{i}', f'R{i}', 'OLD', '10K') for i in range(1, count + 1)) store.record_target_inventory(task.task_id, targets) store.replace_target_plan(task.task_id, tuple(TargetPlanRow(target, True, 'NEW') for target in targets)) if index == 0: outcomes.update({(task.task_id, f'u{i}'): 'batch_failed' for i in range(1, 6)}) elif index in (2, 3): outcomes[(task.task_id, 'u1')] = 'batch_saved_unverified' gui = TaskCenterService(store, configure_logging(data), supervised=True, quick_run_preparer=preparer) # Only the process transport/native CAD are substituted. Each child executes # the production queue, receipts, backups, issue pass and runtime cleanup. def supervise(operation, arguments, *, stop_event): assert operation == 'execute_task_queue' runtime = ReleasedRuntime(data) runtime.desktop = None return execute_task_queue(engine, stop_event=stop_event, runtime=runtime, **arguments) gui._supervise = supervise ids = tuple(task.task_id for task in tasks) try: result = execute_task_queue(gui, ids, stop_event=Event()) assert not result.paused assert [entry.state for entry in result.entries] == ['failed', 'completed', 'blocked', 'blocked'] assert len(calls) == 100 and set(Counter(calls).values()) == {1} assert (tasks[3].task_id, 'u1') in calls records = dict(store._connection.execute('SELECT state,count(*) FROM execution_operations GROUP BY state')) assert records == {'verified': 93, 'failed': 5, 'uncertain': 2} resumed = execute_task_queue(gui, ids, stop_event=Event(), queue_id=result.queue_id) assert not resumed.paused and resumed.entries[2].state == 'blocked' assert len(calls) == 100 finally: store.close() reopened = TaskStore(database) try: assert reopened._connection.execute("SELECT count(*) FROM execution_operations WHERE state='verified'").fetchone()[0] == 93 assert reopened._connection.execute('SELECT sum(auto_retries) FROM execution_problems').fetchone()[0] == 0 finally: reopened.close()