"""실제 SDK Stop 호출 연결 확인용 CPU 경계. Live infer 종료 검증은 별도다."""
import glob
import json
import os
import threading

assert not glob.glob('/dev/nvidia*')
os.environ['CONTEXTRA_START_DIAGNOSTICS_DIR'] = '/unused'
from pyservicemaker import Pipeline
from app.stream_lifecycle import RoleRunHandle

h = RoleRunHandle('det', 1)
p = Pipeline('stop-observation-probe')
p.add('fakesrc', 'source', {'num-buffers': 1})
p.add('fakesink', 'sink', {'sync': False})
p.link('source', 'sink')
h.bind_pipeline(p)
def worker():
    try:
        waiter = h.start_pipeline()
        h.diagnostic.mark('wait_enter')
        waiter.wait()
        h.diagnostic.mark('wait_returned')
    finally:
        h.diagnostic.mark('worker_finally')
        h.diagnostic.mark('worker_cleanup_returned')
t = threading.Thread(target=worker)
h.bind_thread(t)
t.start()
t.join(10)
assert not t.is_alive()
assert h.stop() is None
s = h.diagnostic_snapshot()
assert s['stop_matches_started'] is True
assert {'stop_enter', 'stop_returned', 'wait_returned', 'worker_cleanup_returned'} <= s['events'].keys()
assert s['worker_alive'] is False
print(json.dumps({'status': 'PASS', 'sdk_stop_call_boundary': True, 'live_stop_test': False, 'gpu_nodes': 0}))
