Files
CenaForge/voice/tests/test_queue.py
T
MateuusandClaude Opus 5.5 84f30bbc2d CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz,
Clipador de lives e Publicador para YouTube, TikTok e Instagram.

Inclui a leitura das visualizações dos posts para o Dashboard e o botão
"Publicar no <rede>" nos cartões das publicações.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:35:09 -03:00

99 lines
4.0 KiB
Python

import asyncio
import threading
import pytest
from app.errors import QueueFull, QueueTimeout, SynthesisTimeout
from app.work_queue import WorkQueue
from tests.conftest import HEADERS, body
pytestmark = pytest.mark.anyio
async def _wait_until(predicate, timeout=3.0):
loop = asyncio.get_running_loop()
end = loop.time() + timeout
while not predicate():
if loop.time() > end:
raise AssertionError("condição não atingida")
await asyncio.sleep(0.01)
async def test_concurrency_limit_and_queue_full():
gate = threading.Event()
queue = WorkQueue(max_concurrency=1, max_queue_per_key=1, queue_timeout_s=5, run_timeout_s=5)
running = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="a"))
await _wait_until(lambda: queue.running == 1)
waiting = asyncio.create_task(queue.run(lambda: "segundo", owner="a"))
await _wait_until(lambda: queue.waiting == 1)
with pytest.raises(QueueFull):
await queue.run(lambda: "terceiro", owner="a")
gate.set()
assert await running is True
assert await waiting == "segundo"
queue.shutdown()
async def test_queue_timeout():
gate = threading.Event()
queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=0.2, run_timeout_s=5)
running = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="a"))
await _wait_until(lambda: queue.running == 1)
with pytest.raises(QueueTimeout):
await queue.run(lambda: "nunca", owner="a")
gate.set()
await running
queue.shutdown()
async def test_synthesis_timeout_keeps_slot_until_thread_ends():
gate = threading.Event()
queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=5, run_timeout_s=0.2)
with pytest.raises(SynthesisTimeout):
await queue.run(lambda: gate.wait(5), owner="a")
# O cliente já recebeu timeout, mas a thread segue rodando: a vaga continua ocupada.
assert queue.running == 1
gate.set()
await _wait_until(lambda: queue.running == 0)
assert await queue.run(lambda: "livre", owner="a") == "livre"
queue.shutdown()
async def test_http_queue_full_returns_503_with_retry_after(make_client, engine):
engine.gate = threading.Event()
async with make_client(max_concurrency=1, max_queue_per_key=1) as client:
first = asyncio.create_task(client.post("/v1/synthesize", json=body("req-aaaaaaaa", text="um"), headers=HEADERS))
await _wait_until(lambda: engine.calls == 1)
second = asyncio.create_task(client.post("/v1/synthesize", json=body("req-bbbbbbbb", text="dois"), headers=HEADERS))
await asyncio.sleep(0.1)
third = await client.post("/v1/synthesize", json=body("req-cccccccc", text="três"), headers=HEADERS)
engine.gate.set()
assert (await first).status_code == 200
assert (await second).status_code == 200
assert third.status_code == 503
assert third.json()["error"]["code"] == "queue_full"
assert third.headers["retry-after"] == "5"
async def test_http_synthesis_timeout_is_504(make_client, engine):
engine.gate = threading.Event()
async with make_client(synth_timeout_s=0.2) as client:
response = await client.post("/v1/synthesize", json=body(), headers=HEADERS)
engine.gate.set()
assert response.status_code == 504
assert response.json()["error"]["code"] == "synthesis_timeout"
async def test_inflight_same_content_is_rendered_once(make_client, engine):
engine.gate = threading.Event()
async with make_client(max_concurrency=2) as client:
a = asyncio.create_task(client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS))
await _wait_until(lambda: engine.calls == 1)
b = asyncio.create_task(client.post("/v1/synthesize", json=body("req-bbbbbbbb"), headers=HEADERS))
await asyncio.sleep(0.1)
engine.gate.set()
ra, rb = await a, await b
assert ra.status_code == rb.status_code == 200
assert ra.content == rb.content
assert engine.calls == 1