54 lines
2.6 KiB
Python
54 lines
2.6 KiB
Python
"""Original queue recovery across a fatal receipt-write boundary."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
from threading import Event
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.dsh.execution_runs import execute_confirmed_run
|
|
from dx_productivity.dsh.run_journal import RunJournal
|
|
from dx_productivity.dsh.task_lifecycle import preflight
|
|
from test_run_journal import project_service
|
|
|
|
|
|
def test_second_resume_after_fatal_exit_preserves_verified_progress_and_runs_each_task_once(project_service, monkeypatch):
|
|
service, _, data, ids, _ = project_service
|
|
calls = []
|
|
active = {'stop': Event(), 'pause': True}
|
|
def worker(command, timeout):
|
|
queue, job = Path(command[3]), command[-1]
|
|
calls.append(next(key for key in ids if key in queue.parts))
|
|
with (queue / 'status.tsv').open('a', encoding='utf8') as stream:
|
|
stream.write(f'{job}\tno_match\tbatch_closed\tControlled verified no match\n')
|
|
if active['pause']:
|
|
active['stop'].set()
|
|
return subprocess.CompletedProcess(command, 0, '', '')
|
|
for task_id in ids:
|
|
preparer = service.for_project_task(task_id)._quick_run_preparer
|
|
preparer._process_runner = worker
|
|
preparer._cscript_resolver = lambda: Path('cscript.exe')
|
|
run_id = 'd' * 32
|
|
first = execute_confirmed_run(service, data, ids, preflight(service, ids), active['stop'], run_id)
|
|
assert first['status'] == 'paused' and calls == ids[:1]
|
|
original = RunJournal(data).inspect(service, run_id)
|
|
active['stop'] = Event()
|
|
class FatalExit(BaseException):
|
|
pass
|
|
with monkeypatch.context() as patch:
|
|
def fatal_finish(*args, **kwargs):
|
|
raise FatalExit('exit after checkpoint, before final receipt')
|
|
patch.setattr(RunJournal, 'finish', fatal_finish)
|
|
with pytest.raises(FatalExit):
|
|
execute_confirmed_run(service, data, ids, preflight(service, ids, allow_completed=True),
|
|
active['stop'], run_id, resume=original)
|
|
assert calls == ids[:2]
|
|
restarted = RunJournal(data).inspect(service, run_id)
|
|
assert restarted['status'] == 'running' and restarted['resume_ready'], restarted['blockers']
|
|
assert restarted['remaining_task_ids'] == ids[2:]
|
|
active.update(stop=Event(), pause=False)
|
|
last = execute_confirmed_run(service, data, ids, preflight(service, ids, allow_completed=True),
|
|
active['stop'], run_id, resume=restarted)
|
|
assert last['status'] == 'completed' and last['queue_id'] == run_id
|
|
assert calls == ids
|
|
assert all(task['saved_items'] == 0 and task['no_match_items'] == 1 for task in last['tasks'])
|