okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5aa356f1
main
1"""#1619: a request waiting for the single request turn sends keep-alive comments (a client with a stream idle 2timeout must not abort a healthy queued request), and one cancelled while queued leaves without taking the turn. 3 4python -m unittest serve.test_queue_keepalive -v 5""" 6import http .client 7import json 8import threading 9import time 10import unittest 11from pathlib import Path 12from unittest import mock 13 14import serve .server as server 15from serve .frontend import ChatTemplate 16from serve .server import ByteTokenizer ,MockEngine ,Service ,serve 17 18ROOT = Path (__file__ ).resolve ().parents [1 ] 19 20 21class QueueKeepAlive (unittest .TestCase ): 22def setUp (self ): 23tok = ByteTokenizer () 24self .svc = Service (MockEngine (tok ,"ok" ),tok ,ChatTemplate (ROOT / "serve/chat_template.jinja" )) 25self .httpd = serve (self .svc ,port = 0 ) 26self .addCleanup (self .httpd .server_close ) 27self .addCleanup (self .httpd .shutdown ) 28p = mock .patch .object (server ,"QUEUE_BEAT_S" ,0.1 ) 29p .start () 30self .addCleanup (p .stop ) 31 32def test_a_queued_stream_gets_keep_alives_then_its_answer (self ): 33self .svc .fifo .acquire ()# another request holds the turn 34c = http .client .HTTPConnection ("127.0.0.1" ,self .httpd .server_address [1 ],timeout = 10 ) 35self .addCleanup (c .close ) 36body = {"stream" :True ,"messages" : [{"role" :"user" ,"content" :"hi" }]} 37c .request ("POST" ,"/v1/chat/completions" ,json .dumps (body ), {"Content-Type" :"application/json" }) 38r = c .getresponse () 39seen = b"" 40t0 = time .monotonic () 41while seen .count (b": keep-alive" )< 2 and time .monotonic ()- t0 < 5 : 42seen += r .fp .readline () 43self .assertGreaterEqual (seen .count (b": keep-alive" ),2 ,seen ) 44self .assertEqual (self .svc .status ["queued" ],1 ) 45self .svc .fifo .release () 46rest = r .read () 47self .assertIn (b"[DONE]" ,rest ) 48self .assertIn (b'"finish_reason": "stop"' ,rest )# the answer ran once the turn was free 49self .assertEqual (self .svc .status ["queued" ],0 ) 50 51def test_a_request_cancelled_while_queued_never_takes_the_turn (self ): 52self .svc .fifo .acquire () 53self .addCleanup (self .svc .fifo .release ) 54cancel = threading .Event () 55ids = self .svc .tok .encode ("hi" ) 56run = self .svc .run (ids ,False ,None ,8 , {},cancel ) 57first = next (run ) 58self .assertEqual (first , ("ping" ,None )) 59self .assertEqual (self .svc .status ["queued" ],1 ) 60cancel .set () 61rest = list (run ) 62self .assertEqual ([k for k ,_ in rest ], ["done" ]) 63self .assertEqual (rest [0 ][1 ]["finish" ],"cancel" ) 64self .assertEqual (self .svc .status ["queued" ],0 ) 65self .assertTrue (self .svc .fifo .locked ())# still the other request's: we never held it 66 67def test_closing_a_queued_request_is_no_longer_queued (self ): 68self .svc .fifo .acquire () 69self .addCleanup (self .svc .fifo .release ) 70run = self .svc .run (self .svc .tok .encode ("hi" ),False ,None ,8 , {},threading .Event ()) 71next (run ) 72run .close () 73self .assertEqual (self .svc .status ["queued" ],0 ) 74 75 76if __name__ == "__main__" : 77unittest .main ()