Files
leleka/engine/strata/serve/test_queue_keepalive.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

78 lines
3.1 KiB
Python

"""#1619: a request waiting for the single request turn sends keep-alive comments (a client with a stream idle
timeout must not abort a healthy queued request), and one cancelled while queued leaves without taking the turn.
python -m unittest serve.test_queue_keepalive -v
"""
import http.client
import json
import threading
import time
import unittest
from pathlib import Path
from unittest import mock
import serve.server as server
from serve.frontend import ChatTemplate
from serve.server import ByteTokenizer, MockEngine, Service, serve
ROOT = Path(__file__).resolve().parents[1]
class QueueKeepAlive(unittest.TestCase):
def setUp(self):
tok = ByteTokenizer()
self.svc = Service(MockEngine(tok, "ok"), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))
self.httpd = serve(self.svc, port=0)
self.addCleanup(self.httpd.server_close)
self.addCleanup(self.httpd.shutdown)
p = mock.patch.object(server, "QUEUE_BEAT_S", 0.1)
p.start()
self.addCleanup(p.stop)
def test_a_queued_stream_gets_keep_alives_then_its_answer(self):
self.svc.fifo.acquire() # another request holds the turn
c = http.client.HTTPConnection("127.0.0.1", self.httpd.server_address[1], timeout=10)
self.addCleanup(c.close)
body = {"stream": True, "messages": [{"role": "user", "content": "hi"}]}
c.request("POST", "/v1/chat/completions", json.dumps(body), {"Content-Type": "application/json"})
r = c.getresponse()
seen = b""
t0 = time.monotonic()
while seen.count(b": keep-alive") < 2 and time.monotonic() - t0 < 5:
seen += r.fp.readline()
self.assertGreaterEqual(seen.count(b": keep-alive"), 2, seen)
self.assertEqual(self.svc.status["queued"], 1)
self.svc.fifo.release()
rest = r.read()
self.assertIn(b"[DONE]", rest)
self.assertIn(b'"finish_reason": "stop"', rest) # the answer ran once the turn was free
self.assertEqual(self.svc.status["queued"], 0)
def test_a_request_cancelled_while_queued_never_takes_the_turn(self):
self.svc.fifo.acquire()
self.addCleanup(self.svc.fifo.release)
cancel = threading.Event()
ids = self.svc.tok.encode("hi")
run = self.svc.run(ids, False, None, 8, {}, cancel)
first = next(run)
self.assertEqual(first, ("ping", None))
self.assertEqual(self.svc.status["queued"], 1)
cancel.set()
rest = list(run)
self.assertEqual([k for k, _ in rest], ["done"])
self.assertEqual(rest[0][1]["finish"], "cancel")
self.assertEqual(self.svc.status["queued"], 0)
self.assertTrue(self.svc.fifo.locked()) # still the other request's: we never held it
def test_closing_a_queued_request_is_no_longer_queued(self):
self.svc.fifo.acquire()
self.addCleanup(self.svc.fifo.release)
run = self.svc.run(self.svc.tok.encode("hi"), False, None, 8, {}, threading.Event())
next(run)
run.close()
self.assertEqual(self.svc.status["queued"], 0)
if __name__ == "__main__":
unittest.main()