baseball-engine / tests /test_recovery_extended.py
github-actions[bot]
Automated Deploy: a19ede424a80b7b8d7f6978585141d1bb45de69c
326d3cc
Raw History Blame Contribute Delete
2.05 kB
import os
import signal
import subprocess
import sys
from pathlib import Path
import pytest
from baseball_batting.ingest import ingest
from baseball_batting.observations import run_observations
from baseball_batting.config import EngineConfig
from baseball_batting.storage import StageCache,atomic_json
@pytest.mark.skipif(os.name!="posix",reason="POSIX termination fixture")
def test_forced_termination_after_committed_chunk_resumes(video,tmp_path):
capture=ingest(video,{})
atomic_json(tmp_path/"capture.json",capture)
script=tmp_path/"interrupted.py"
script.write_text('''import os,signal,sys\nfrom baseball_batting.storage import StageCache,read_json\nfrom baseball_batting.config import EngineConfig\nfrom baseball_batting.observations import run_observations\ncache=StageCache(sys.argv[2],"observations","fixture")\noriginal=cache.commit\ndef stop(name,*args,**kwargs):\n result=original(name,*args,**kwargs)\n if name=="chunk_000000":\n print("COMMITTED",flush=True)\n os.kill(os.getpid(),signal.SIGSTOP)\n return result\ncache.commit=stop\nrun_observations(sys.argv[1],read_json(sys.argv[2]+"/capture.json"),EngineConfig(chunk_size=3,export_video=False),cache)\n''')
env=dict(os.environ);env["PYTHONPATH"]=str(Path(__file__).resolve().parents[1])
child=subprocess.Popen([sys.executable,str(script),str(video),str(tmp_path)],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,env=env)
try:
assert child.stdout.readline().strip()=="COMMITTED"
cache=StageCache(tmp_path,"observations","fixture");assert cache.valid("chunk_000000")
path=cache.root/"chunk_000000.json";before=path.stat().st_mtime_ns
child.kill();child.wait(timeout=10)
result=run_observations(video,capture,EngineConfig(chunk_size=3,export_video=False),cache)
assert len(result["frames"])==capture["frame_count"] and path.stat().st_mtime_ns==before
assert cache.valid("chunk_000001")
finally:
if child.poll() is None:child.kill();child.wait(timeout=10)