Spaces:
Sleeping
Sleeping
Download engine/tests/test_runtime.py from lorcapoul/smol-neuron-studio: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/tests/test_runtime.py
- Command line
-
hf download hf://spaces/lorcapoul/smol-neuron-studio/engine/tests/test_runtime.py
-
curl -L -o test_runtime.py https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/tests/test_runtime.py
3.84 kB
| 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 | |