import asyncio import sys import tempfile from pathlib import Path import pytest from fastapi import HTTPException sys.path.insert(0, str(Path(__file__).resolve().parents[2])) from tts.app import SpeechRequest, TTSSettings, create_app def test_queue_deadline_never_starts_waiting_model_and_cleans_request(): async def scenario(root): model = root / "models" (model / "voices").mkdir(parents=True) for name in ("config.json", "kokoro-v1_0.pth", "voices/zf_xiaobei.pt", "voices/zm_yunxi.pt"): (model / name).write_bytes(b"fixture") settings = TTSSettings("fixture-key", model, root / "work", queue_timeout_seconds=.01) started, calls = asyncio.Event(), [] async def synthesize(body, destination, settings): calls.append(body.input) started.set() await asyncio.Event().wait() class Connection: async def is_disconnected(self): return False endpoint = next(r.endpoint for r in create_app(settings, synthesize=synthesize).routes if r.path == "/v1/audio/speech") first = asyncio.create_task(endpoint(SpeechRequest(input="first"), Connection())) try: await started.wait() with pytest.raises(HTTPException) as error: await endpoint(SpeechRequest(input="queued"), Connection()) assert error.value.status_code == 503 assert calls == ["first"] assert len(list(settings.work_dir.iterdir())) == 1 finally: first.cancel() await asyncio.gather(first, return_exceptions=True) assert list(settings.work_dir.iterdir()) == [] with tempfile.TemporaryDirectory(prefix="tq-") as directory: asyncio.run(scenario(Path(directory)))