Spaces:
Running
Running
File size: 3,838 Bytes
0624276 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import json
import threading
import time
import pytest
import app.main as main
from app.runtime import Busy, Turnstile, Warmup
# --- Turnos -------------------------------------------------------------------------
def test_turnos_en_orden_de_llegada():
t = Turnstile(max_waiting=5)
a, b, c = t.take(), t.take(), t.take()
assert [t.ahead(x) for x in (a, b, c)] == [0, 1, 2]
assert t.wait(a, 0) and not t.wait(b, 0)
t.release(a)
assert t.wait(b, 0) and t.ahead(c) == 1
t.release(b)
t.release(c)
assert t.waiting == 0
def test_quien_se_va_antes_de_su_turno_no_bloquea():
t = Turnstile(max_waiting=5)
a, b, c = t.take(), t.take(), t.take()
t.release(b) # se fue esperando
assert t.ahead(c) == 1
t.release(a)
assert t.wait(c, 0), "c pasa directo: b ya no está"
t.release(c)
d = t.take()
assert t.wait(d, 0)
def test_cola_llena():
t = Turnstile(max_waiting=1)
t.take() # atendido
t.take() # esperando
with pytest.raises(Busy):
t.take()
def test_espera_hasta_su_turno_entre_hilos():
t = Turnstile(max_waiting=5)
a, b = t.take(), t.take()
threading.Timer(0.2, t.release, args=[a]).start()
start = time.monotonic()
assert t.wait(b, 5)
assert time.monotonic() - start >= 0.15
# --- Arranque -------------------------------------------------------------------------
def test_warmup_carga_una_sola_vez_y_reintenta_si_falla():
calls = []
def load():
calls.append(1)
if len(calls) == 1:
raise RuntimeError("boom")
w = Warmup(load)
w.start().join()
assert not w.ready and "boom" in w.error
w.run()
assert w.ready and w.error is None
w.run()
assert len(calls) == 2
def test_info_responde_503_mientras_carga(client, monkeypatch):
gate = threading.Event()
slow = Warmup(lambda: gate.wait(5))
monkeypatch.setattr(main, "warmup", slow)
slow.start()
r = client.get("/info")
assert r.status_code == 503 and r.json()["detail"] == "Warming up" and r.headers["retry-after"] == "2"
gate.set()
slow.run()
r = client.get("/info")
assert r.status_code == 200
assert r.json()["limits"]["max_queue"] == main.MAX_QUEUE
# --- Cola y límites en /generate/stream ----------------------------------------------
BODY = {"prompt": "Name a color.", "max_new_tokens": 4,
"interventions": [{"layer": 22, "unit": 333, "mode": "add", "value": 20}]}
def read_events(r):
return [json.loads(line[6:]) for line in r.iter_lines() if line.startswith("data: ")]
def test_stream_avisa_su_lugar_en_la_cola(client, monkeypatch):
t = Turnstile(max_waiting=4)
monkeypatch.setattr(main, "turnstile", t)
busy = t.take() # alguien está generando
threading.Timer(1.5, t.release, args=[busy]).start()
with client.stream("POST", "/generate/stream", json=BODY) as r:
events = read_events(r)
assert events[0] == {"type": "queued", "ahead": 1}
assert events[-1]["type"] == "done"
assert t.waiting == 0, "el turno se libera al terminar"
def test_stream_con_cola_llena_responde_503(client, monkeypatch):
t = Turnstile(max_waiting=0)
monkeypatch.setattr(main, "turnstile", t)
t.take()
r = client.post("/generate/stream", json=BODY)
assert r.status_code == 503 and "waiting" in r.json()["detail"]
def test_stream_corta_al_pasar_el_tiempo_maximo(client, monkeypatch):
monkeypatch.setattr(main, "turnstile", Turnstile(max_waiting=4))
monkeypatch.setattr(main, "MAX_REQUEST_SECONDS", 0)
with client.stream("POST", "/generate/stream", json=BODY) as r:
events = read_events(r)
assert events[-1]["type"] == "error" and "Time limit" in events[-1]["detail"]
assert not any(e["type"] == "done" for e in events)
assert main.turnstile.waiting == 0
|