"""Real loopback sockets: SSE must reach the caller before the upstream ends.""" import asyncio from contextlib import contextmanager import socket import threading import time from fastapi import FastAPI from fastapi.responses import StreamingResponse import httpx import uvicorn from app.core.config import settings from app.main import create_app @contextmanager def serve(app): sock = socket.socket() sock.bind(("127.0.0.1", 0)) port = sock.getsockname()[1] server = uvicorn.Server(uvicorn.Config(app, log_level="error", loop="asyncio")) thread = threading.Thread(target=server.run, kwargs={"sockets": [sock]}, daemon=True) thread.start() try: deadline = time.monotonic() + 10 while not server.started: if not thread.is_alive() or time.monotonic() > deadline: raise RuntimeError("test server did not start") time.sleep(0.02) yield f"http://127.0.0.1:{port}" finally: server.should_exit = True thread.join(timeout=5) sock.close() def test_sse_first_chunk_and_disconnect_cleanup(monkeypatch): upstream = FastAPI() release = threading.Event() closed = threading.Event() @upstream.post("/api/v1/ai/routes/generate/stream") async def events(): async def chunks(): try: yield b"data: first\n\n" # The caller releases this only AFTER receiving the first chunk. while not release.is_set(): await asyncio.sleep(0.02) yield b"data: done\n\n" finally: closed.set() return StreamingResponse(chunks(), media_type="text/event-stream") with serve(upstream) as target: monkeypatch.setattr(settings, "ai_service_url", target) monkeypatch.setattr(settings, "generated_routes_dir", None) with serve(create_app()) as public: with httpx.Client(timeout=3, trust_env=False) as client: with client.stream("POST", public + "/api/v1/ai/routes/generate/stream", json={}) as response: assert response.status_code == 200 chunks = response.iter_bytes() assert next(chunks) == b"data: first\n\n" release.set() assert b"data: done\n\n" in b"".join(chunks) assert closed.wait(2) release.clear() closed.clear() with client.stream("POST", public + "/api/v1/ai/routes/generate/stream", json={}) as response: assert next(response.iter_bytes()) == b"data: first\n\n" assert closed.wait(2), "browser disconnect must release upstream streaming resources"