97 lines
4.6 KiB
Python
97 lines
4.6 KiB
Python
from pathlib import Path
|
|
import subprocess
|
|
from threading import Event
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.trial_task_center.errors import TaskCenterError
|
|
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
|
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
|
from dx_productivity.trial_task_center.service import TaskCenterService
|
|
from dx_productivity.trial_task_center.store import TaskStore
|
|
from dx_productivity.trial_task_center.task_queue import execute_task_queue
|
|
|
|
|
|
@pytest.fixture
|
|
def resume_case(tmp_path):
|
|
project=tmp_path/'projects'/'Board'/'board.prj'
|
|
project.parent.mkdir(parents=True)
|
|
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
|
calls=[];stop=Event()
|
|
def worker(command,timeout):
|
|
job=command[-1];calls.append(job)
|
|
with (Path(command[3])/'status.tsv').open('a',encoding='utf8') as stream:
|
|
stream.write(f'{job}\tawaiting_native_replace\t\tSimulated capture\n')
|
|
stream.write(f'{job}\tsaved_ok\tbatch_closed\tSimulated verified result\n')
|
|
if len(calls)==2:stop.set()
|
|
return subprocess.CompletedProcess(command,0,'','')
|
|
stores=[]
|
|
def reopen():
|
|
store=TaskStore(tmp_path/'tasks.db');stores.append(store)
|
|
return TaskCenterService(store,configure_logging(tmp_path/'data'),
|
|
quick_run_preparer=QuickRunPreparer(tmp_path/'data',process_runner=worker,
|
|
cscript_resolver=lambda:Path('simulated-cscript'),
|
|
sheet_discoverer=lambda _: (('MAIN','1'),('MAIN','2'))))
|
|
service=reopen()
|
|
task=service.create_project_tasks(tmp_path/'projects')[0]
|
|
service.add_rule(task.task_id,'OLD1','NEW1');service.add_rule(task.task_id,'OLD2','NEW2')
|
|
result=execute_task_queue(service,(task.task_id,),stop_event=stop)
|
|
assert result.paused and len(calls)==2
|
|
root=service.for_project_task(task.task_id)._quick_run_preparer.data_root/'quick-run'
|
|
yield service,task,result,root,calls,reopen
|
|
for store in stores:store.close()
|
|
|
|
|
|
@pytest.mark.parametrize('damage',['missing_status','missing_command','empty_status','truncated_status','truncated_command'])
|
|
def test_restart_with_incomplete_handoff_never_replays_worker(resume_case,damage):
|
|
service,task,result,root,calls,reopen=resume_case
|
|
path=root/('command.tsv' if 'command' in damage else 'status.tsv')
|
|
if damage.startswith('missing'):path.unlink()
|
|
elif damage=='empty_status':path.write_text('job_id\tstate\tcode\tmessage\n',encoding='utf8')
|
|
else:path.write_text(path.read_text(encoding='utf8')[:-12],encoding='utf8')
|
|
restarted=reopen()
|
|
context=restarted.task_context(task.task_id)
|
|
assert context.interrupted
|
|
assert context.has_evidence
|
|
outcome=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
|
assert not outcome.paused and outcome.entries[0].state=='blocked'
|
|
assert len(calls)==2
|
|
|
|
|
|
def test_ordinary_restart_only_dispatches_remaining_items(resume_case):
|
|
service,task,result,root,calls,reopen=resume_case
|
|
restarted=reopen()
|
|
context=restarted.task_context(task.task_id)
|
|
assert not context.interrupted and context.progress.completed_items==2
|
|
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
|
assert not result.paused and result.entries[0].state=='completed'
|
|
assert len(calls)==4 and len(set(calls))==4
|
|
|
|
|
|
def test_direct_prepare_cannot_overwrite_incomplete_handoff(resume_case):
|
|
service,task,result,root,calls,reopen=resume_case
|
|
(root/'status.tsv').unlink()
|
|
command=(root/'command.tsv').read_bytes()
|
|
with pytest.raises(TaskCenterError):service.prepare_quick_run(task.task_id)
|
|
assert (root/'command.tsv').read_bytes()==command
|
|
assert len(calls)==2
|
|
|
|
|
|
def test_receipts_block_rollback_to_an_empty_status_even_without_sidecars(resume_case):
|
|
service,task,result,root,calls,reopen=resume_case
|
|
(root/'status.tsv').write_text('job_id\tstate\tcode\tmessage\n',encoding='utf8')
|
|
(root/'current-command.tsv').unlink(missing_ok=True)
|
|
restarted=reopen()
|
|
assert restarted.task_context(task.task_id).interrupted
|
|
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
|
assert not result.paused and result.entries[0].state=='blocked' and len(calls)==2
|
|
|
|
|
|
def test_missing_entire_handoff_cannot_erase_database_receipts(resume_case):
|
|
service,task,result,root,calls,reopen=resume_case
|
|
root.rename(root.with_name('preserved-probe-handoff'))
|
|
restarted=reopen()
|
|
assert restarted.task_context(task.task_id).interrupted
|
|
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
|
assert not result.paused and result.entries[0].state=='blocked' and len(calls)==2
|