"""Isolamento entre chaves de serviço (homologação e produção no mesmo serviço): fila, idempotência e jobs.""" import asyncio import logging import sqlite3 import threading from dataclasses import replace from pathlib import Path import pytest from app.errors import QueueFull, QueueTimeout, SynthesisTimeout from app.keys import issue_key, keys_path from app.store import Store from app.work_queue import WorkQueue from tests.conftest import HEADERS, body from tests.test_queue import _wait_until from tests.test_transcribe import ONE_SECOND, stt_headers pytestmark = pytest.mark.anyio def _issue(settings, scopes=("tts", "transcribe", "status"), name="homolog"): record, plaintext = issue_key(keys_path(settings.data_dir), name, tuple(scopes)) return record, {"X-Service-Key": plaintext} # ---------- fila ---------- async def test_full_queue_of_one_key_does_not_refuse_the_other(): 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="homolog")) await _wait_until(lambda: queue.running == 1) homolog_waiting = asyncio.create_task(queue.run(lambda: "homolog-2", owner="homolog")) await _wait_until(lambda: queue.waiting_for("homolog") == 1) with pytest.raises(QueueFull): await queue.run(lambda: "homolog-3", owner="homolog") assert not queue.is_full("prod") prod = asyncio.create_task(queue.run(lambda: "prod-1", owner="prod")) await _wait_until(lambda: queue.waiting_for("prod") == 1) gate.set() assert await running is True assert await homolog_waiting == "homolog-2" assert await prod == "prod-1" queue.shutdown() async def test_cpu_slot_rotates_between_keys(): """A vaga de CPU é global, mas entregue em rodízio: a produção não espera a fila inteira da homologação.""" gate = threading.Event() order: list[str] = [] queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=5, run_timeout_s=5) first = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="homolog")) await _wait_until(lambda: queue.running == 1) tasks = [asyncio.create_task(queue.run(lambda i=i: order.append(f"homolog-{i}"), owner="homolog")) for i in range(3)] await _wait_until(lambda: queue.waiting_for("homolog") == 3) tasks.append(asyncio.create_task(queue.run(lambda: order.append("prod"), owner="prod"))) await _wait_until(lambda: queue.waiting_for("prod") == 1) gate.set() await first await asyncio.gather(*tasks) assert order == ["prod", "homolog-0", "homolog-1", "homolog-2"] queue.shutdown() async def test_http_full_queue_of_one_key_does_not_block_the_other(make_client, settings, engine): engine.gate = threading.Event() async with make_client(max_concurrency=1, max_queue_per_key=1) as client: _, homolog = _issue(settings) first = asyncio.create_task(client.post("/v1/synthesize", json=body("req-h0000001", text="um"), headers=homolog)) await _wait_until(lambda: engine.calls == 1) second = asyncio.create_task(client.post("/v1/synthesize", json=body("req-h0000002", text="dois"), headers=homolog)) await asyncio.sleep(0.1) refused = await client.post("/v1/synthesize", json=body("req-h0000003", text="três"), headers=homolog) status_homolog = (await client.get("/v1/status", headers=homolog)).json()["queues"]["synthesis"] status_prod = (await client.get("/v1/status", headers=HEADERS)).json()["queues"]["synthesis"] prod = asyncio.create_task(client.post("/v1/synthesize", json=body("req-p0000001", text="pago"), headers=HEADERS)) await asyncio.sleep(0.1) engine.gate.set() results = [await first, await second, await prod] assert refused.status_code == 503 and refused.json()["error"]["code"] == "queue_full" assert [r.status_code for r in results] == [200, 200, 200] # O /v1/status mostra a fila da própria chave: a homologação cheia não aparece como cheia para a produção. assert status_homolog["waiting"] == 1 and status_homolog["maxQueue"] == 1 assert status_prod["waiting"] == 0 and status_prod["running"] == 1 # ---------- idempotência da síntese ---------- async def test_same_request_id_on_two_keys_are_distinct_requests(make_client, settings): async with make_client() as client: _, homolog = _issue(settings) prod = await client.post("/v1/synthesize", json=body("req-colide01", text="texto da produção"), headers=HEADERS) other = await client.post("/v1/synthesize", json=body("req-colide01", text="texto da homologação"), headers=homolog) assert prod.status_code == 200 and other.status_code == 200 # sem 409: são pedidos diferentes assert prod.headers["x-idempotent-replay"] == "false" assert other.headers["x-idempotent-replay"] == "false" assert prod.content != other.content async def test_same_key_stays_idempotent(make_client, settings): async with make_client() as client: _, homolog = _issue(settings) first = await client.post("/v1/synthesize", json=body("req-idem0001"), headers=homolog) again = await client.post("/v1/synthesize", json=body("req-idem0001"), headers=homolog) conflict = await client.post("/v1/synthesize", json=body("req-idem0001", text="outro texto"), headers=homolog) assert first.headers["x-idempotent-replay"] == "false" assert again.headers["x-idempotent-replay"] == "true" and again.content == first.content assert conflict.status_code == 409 # ---------- transcrição ---------- async def test_job_of_another_key_answers_like_an_unknown_job(make_client, settings): async with make_client(stt_sync_wait_s=0.0) as client: _, homolog = _issue(settings) created = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers()) ref = created.headers["x-job-ref"] await asyncio.sleep(0.05) own = await client.get(f"/v1/transcribe/jobs/{ref}", headers=HEADERS) foreign = await client.get(f"/v1/transcribe/jobs/{ref}", headers=homolog) unknown = await client.get("/v1/transcribe/jobs/abcdefghijkl", headers=homolog) assert own.status_code in (200, 202) # Mesmo status e mesmo corpo de um job inexistente: não revela que o job existe. assert foreign.status_code == unknown.status_code == 410 assert foreign.json() == unknown.json() assert "x-job-ref" not in foreign.headers async def test_same_idempotency_key_on_two_keys_does_not_collide(make_client, settings, stt_engine): async with make_client() as client: _, homolog = _issue(settings) prod = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:1:t")) other = await client.post( "/v1/transcribe", content=ONE_SECOND + b"z", headers=stt_headers(key="order:1:t", **homolog) ) same_audio = await client.post( "/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:2:t", **homolog) ) replay = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:1:t")) assert prod.status_code == other.status_code == 200 # sem 409 entre chaves assert other.headers["x-idempotent-replay"] == "false" # O mesmo áudio por outra chave vira outro job (nada de jobRef compartilhado entre chaves). assert same_audio.headers["x-job-ref"] != prod.headers["x-job-ref"] assert replay.headers["x-idempotent-replay"] == "true" assert replay.headers["x-job-ref"] == prod.headers["x-job-ref"] # ---------- banco no formato antigo ---------- LEGACY_SCHEMA = """ CREATE TABLE IF NOT EXISTS requests (request_id TEXT PRIMARY KEY, cache_key TEXT NOT NULL, created_at INTEGER NOT NULL); CREATE TABLE IF NOT EXISTS transcribe_requests ( idempotency_key TEXT PRIMARY KEY, content_key TEXT NOT NULL, created_at INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS cache ( cache_key TEXT PRIMARY KEY, size INTEGER NOT NULL, duration_ms INTEGER NOT NULL, sha256 TEXT NOT NULL, created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL ); """ def _legacy_bind(db_path, request_id, cache_key): """O que a imagem anterior faz no bind_request: INSERT sem key_id e, no conflito, lê a linha gravada.""" db = sqlite3.connect(db_path) try: db.execute("INSERT INTO requests(request_id, cache_key, created_at) VALUES (?, ?, strftime('%s','now'))", (request_id, cache_key)) db.commit() return True, cache_key except sqlite3.IntegrityError: return False, db.execute("SELECT cache_key FROM requests WHERE request_id = ?", (request_id,)).fetchone()[0] finally: db.close() def test_store_boots_over_legacy_database_and_keeps_rollback_working(tmp_path): db_path = tmp_path / "voice.sqlite3" db = sqlite3.connect(db_path) db.executescript(LEGACY_SCHEMA) db.execute("INSERT INTO requests VALUES ('req-antigo1', 'k-antiga', strftime('%s','now'))") db.execute("INSERT INTO transcribe_requests VALUES ('order:9:t', 'c-antiga', strftime('%s','now'))") db.commit() db.close() store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1) cached = store.put("a" * 64, b"ogg", 1000, "h") assert store.bind_request("env", "req-antigo1", "k-nova") == (True, "k-nova") assert store.bind_request("homolog", "req-antigo1", "k-outra") == (True, "k-outra") assert store.bind_request("env", "req-antigo1", "k-x") == (False, "k-nova") assert store.bind_transcription("env", "order:9:t", "c-nova") == (True, "c-nova") store.close() # As tabelas antigas continuam intactas: mesmo esquema e mesmas linhas. db = sqlite3.connect(db_path) assert [r[1] for r in db.execute("PRAGMA table_info(requests)")] == ["request_id", "cache_key", "created_at"] assert db.execute("SELECT request_id, cache_key FROM requests").fetchall() == [("req-antigo1", "k-antiga")] assert db.execute("SELECT idempotency_key, content_key FROM transcribe_requests").fetchall() == [ ("order:9:t", "c-antiga") ] db.close() # Rollback: a imagem anterior segue gravando e fazendo replay sobre o mesmo arquivo, e enxerga o cache. assert _legacy_bind(db_path, "req-rollback", "k-r") == (True, "k-r") assert _legacy_bind(db_path, "req-rollback", "k-r") == (False, "k-r") assert _legacy_bind(db_path, "req-antigo1", "k-antiga") == (False, "k-antiga") # Volta para a versão nova: as amarrações dela continuam lá. store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1) assert store.bind_request("env", "req-antigo1", "k-x") == (False, "k-nova") assert store.get(cached.cache_key) is not None store.close() # ---------- timeouts ---------- async def _hog_then_other(queue_timeout_s: float): """Chave A ocupa a CPU com um trabalho no teto (a thread passa do teto um pouco) e tem mais um esperando; a chave B chega depois. Devolve o resultado (ou a exceção) de B.""" import time queue = WorkQueue(max_concurrency=1, max_queue_per_key=4, queue_timeout_s=queue_timeout_s, run_timeout_s=0.3) hog = asyncio.create_task(queue.run(lambda: time.sleep(0.35), owner="homolog")) await _wait_until(lambda: queue.running == 1) second = asyncio.create_task(queue.run(lambda: time.sleep(0.35), owner="homolog")) await _wait_until(lambda: queue.waiting_for("homolog") == 1) other = asyncio.create_task(queue.run(lambda: "prod", owner="prod")) results = await asyncio.gather(hog, second, other, return_exceptions=True) queue.shutdown() return results async def test_other_key_is_served_when_a_job_hits_the_ceiling(): # Espera = 2x execução (a relação dos padrões): B passa na frente do 2º trabalho de A e é atendido, # mesmo com a thread de A passando um pouco do teto. hog, second, other = await _hog_then_other(queue_timeout_s=0.6) assert isinstance(hog, SynthesisTimeout) assert other == "prod" async def test_short_queue_timeout_lets_long_job_starve_the_other_key(): # Contraprova: espera menor que a execução, B leva queue_timeout atrás de um trabalho da outra chave. _, _, other = await _hog_then_other(queue_timeout_s=0.2) assert isinstance(other, QueueTimeout) def test_default_timeouts_hold_the_relations(): from app.config import BACKEND_CLIENT_TIMEOUT_S, Settings defaults = Settings(service_key="k" * 40, models_dir=Path("."), data_dir=Path("."), catalog_path=Path("."), lock_path=Path(".")) assert defaults.timeout_warnings() == [] assert defaults.queue_timeout_s >= 2 * defaults.synth_timeout_s assert defaults.stt_queue_timeout_s >= 2 * defaults.stt_timeout_s assert defaults.queue_timeout_s + defaults.synth_timeout_s < BACKEND_CLIENT_TIMEOUT_S bad = replace(defaults, queue_timeout_s=20.0, synth_timeout_s=30.0, stt_queue_timeout_s=90.0, stt_timeout_s=90.0) warnings = bad.timeout_warnings() assert any(w.startswith("VOICE_QUEUE_TIMEOUT_S=20 abaixo de 2x") for w in warnings) assert any(w.startswith("VOICE_STT_QUEUE_TIMEOUT_S=90 abaixo de 2x") for w in warnings) async def test_boot_logs_timeout_warning(make_client, caplog): caplog.set_level(logging.WARNING) async with make_client(queue_timeout_s=5, synth_timeout_s=5): pass assert "config: VOICE_QUEUE_TIMEOUT_S=5 abaixo de 2x VOICE_SYNTH_TIMEOUT_S=5" in caplog.text def test_retired_queue_variable_is_reported(monkeypatch): from app.config import Settings monkeypatch.setenv("VOICE_MAX_QUEUE", "8") monkeypatch.delenv("VOICE_STT_MAX_QUEUE", raising=False) assert Settings.retired_vars_in_env() == [("VOICE_MAX_QUEUE", "VOICE_MAX_QUEUE_PER_KEY")]