"""Production RTSP source construction in a worker, without SDK Start or network."""
import glob
import tempfile
import threading
from pyservicemaker import Pipeline, Flow
from app.source_adapters.rtsp import RtspSourceAdapter


def verify_source_composition():
    assert not glob.glob('/dev/nvidia*')
    errors = []
    completed = []
    def worker():
        try:
            with tempfile.TemporaryDirectory() as root:
                adapter = RtspSourceAdapter('rtsp://127.0.0.1:9/synthetic', config_root=root)
                with adapter.prepare('det', 1) as source:
                    pipeline = Pipeline('source-compose-only')
                    source.build_capture(Flow(pipeline), pipeline, rtp_time=True)
                    completed.append(True)
        except Exception as error:
            errors.append(type(error).__name__)
    thread = threading.Thread(target=worker, daemon=True)
    thread.start()
    thread.join(8)
    assert not thread.is_alive(), 'Source composition did not return'
    assert not errors, errors
    assert completed == [True]


if __name__ == '__main__':
    verify_source_composition()
    print('SDK_SOURCE_COMPOSE_PASS: production adapter/worker, no SDK Start/network/GPU')
