local_protocol_smoke.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """Run the public Qt-clean validator against an in-process simulated server."""
  3. import asyncio
  4. import os
  5. from pathlib import Path
  6. import sys
  7. import tempfile
  8. ROOT = Path(__file__).resolve().parents[1]
  9. sys.path.insert(0, str(ROOT / "src"))
  10. from stl_backend.config import load_settings
  11. from stl_backend.server import start_server
  12. from stl_backend.web_gateway import WebGateway
  13. async def main():
  14. with tempfile.TemporaryDirectory(prefix="qt-clean-smoke-") as directory:
  15. settings = load_settings(ROOT / "config/simulated.json")
  16. settings.update(listen="127.0.0.1:0", data_dir=directory, telemetry_interval_s=.1)
  17. settings["stations"] = settings["stations"][:1]
  18. server, bench, grpc_port = await start_server(settings)
  19. gateway = WebGateway(bench.grpc_service, "127.0.0.1", 0,
  20. ROOT / "src/stl_backend/web")
  21. try:
  22. await gateway.start()
  23. port = gateway.server.sockets[0].getsockname()[1]
  24. output = Path(directory) / "protocol.json"
  25. child = await asyncio.create_subprocess_exec(sys.executable, "-B",
  26. str(ROOT / "validation/validate_qt_clean.py"), "--url",
  27. f"ws://127.0.0.1:{port}/ws?view=qt-clean", "--station", "S1",
  28. "--output", str(output), "--grpc-address", f"127.0.0.1:{grpc_port}",
  29. "--backend-source", str(ROOT),
  30. stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
  31. cwd=ROOT, env=os.environ.copy())
  32. stdout, _ = await asyncio.wait_for(child.communicate(), timeout=45)
  33. print(stdout.decode("utf-8", errors="replace"), end="")
  34. raise SystemExit(child.returncode)
  35. finally:
  36. await gateway.stop()
  37. await server.stop(0)
  38. await bench.grpc_service.drain()
  39. await bench.close()
  40. if __name__ == "__main__":
  41. asyncio.run(main())