okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
203df526f
main
1"""serve/test_admit_order.py - STRATA_ADMIT_SHORT_FIRST: new requests take the control lines short prompts first 2(StrataEngine._take_control / _admit_next; no process, no GPU). 3 4python -m unittest serve.test_admit_order -v 5""" 6from __future__import annotations 7 8import sys 9import collections 10import threading 11import time 12import unittest 13from pathlib import Path 14from unittest import mock 15 16sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 17import serve .server as server # noqa: E402 18from serve .server import StrataEngine # noqa: E402 19 20 21def engine_without_process (n = 2 ): 22"""A StrataEngine with the admission state of a started one and no process: alive() is `e.up`.""" 23e = StrataEngine ("missing-executable" , [],lazy = True ) 24e .batch = n 25e .slot_busy = [False ]* n 26e .slot_claims ,e .slot_releasing ,e ._orphan_seen = set (),set (), {} 27e .slot_cv = threading .Condition () 28e .waiting ,e .admitting ,e .wait_lens ,e .ctl_epoch = 0 ,0 , [],0 29e .arrivals = collections .deque (maxlen = 64 ) 30e .ctl = threading .Lock () 31e .gen = 1 32e .up = True 33e .alive = lambda :e .up 34return e 35 36 37class AdmitOrderTest (unittest .TestCase ): 38def setUp (self ): 39self .e = engine_without_process (16 ) 40self .order ,self .cancels = [], [] 41self .order_lock = threading .Lock () 42 43def tearDown (self ): 44for c in self .cancels : 45c .set () 46 47def take (self ,name ,plen ,** kw ): 48"""A request waiting for the control lines; once it has them it notes its name and gives them back.""" 49c = threading .Event () 50self .cancels .append (c ) 51 52def go (): 53gen = self .e ._take_control (c ,plen ,** kw ) 54try : 55while True : 56next (gen ) 57except StopIteration as s : 58if s .value : 59with self .order_lock : 60self .order .append (name ) 61time .sleep (0.05 )# its read 62self .e .ctl .release () 63threading .Thread (target = go ,daemon = True ).start () 64 65def until (self ,cond ,s = 5.0 ): 66end = time .time ()+ s 67while time .time ()< end and not cond (): 68time .sleep (0.01 ) 69self .assertTrue (cond ()) 70 71def queue (self ,* reqs ): 72"""While a long read holds the control lines, the requests arrive in this order; then the read ends.""" 73self .assertTrue (self .e .ctl .acquire (timeout = 1 )) 74for name ,plen in reqs : 75self .take (name ,plen ,new = True ) 76self .until (lambda n = len (self .order )+ sum (1 for _ in self .e .wait_lens ):self .e .waiting >= 1 ) 77time .sleep (0.02 )# (distinct arrival times) 78self .until (lambda :self .e .waiting == len (reqs )) 79self .e .ctl .release () 80self .until (lambda :len (self .order )== len (reqs ),s = 15.0 ) 81return self .order 82 83def test_short_first_puts_the_chat_before_the_agent_reads (self ): 84with mock .patch .object (server ,"ADMIT_SHORT_FIRST" ,8192 ),mock .patch .object (server ,"ADMIT_MAX_WAIT_S" ,60.0 ): 85order = self .queue (("agent1" ,120000 ), ("agent2" ,190000 ), ("chat" ,500 ), ("agent3" ,150000 )) 86self .assertEqual (order [0 ],"chat" ) 87self .assertEqual (order [1 :], ["agent1" ,"agent2" ,"agent3" ])# arrival order among the long ones 88 89def test_a_long_request_that_waited_too_long_goes_first (self ): 90with mock .patch .object (server ,"ADMIT_SHORT_FIRST" ,8192 ),mock .patch .object (server ,"ADMIT_MAX_WAIT_S" ,0.2 ): 91self .assertTrue (self .e .ctl .acquire (timeout = 1 )) 92self .take ("agent" ,120000 ,new = True ) 93self .until (lambda :self .e .waiting == 1 ) 94time .sleep (0.4 )# past STRATA_ADMIT_MAX_WAIT_S 95self .take ("chat" ,500 ,new = True ) 96self .until (lambda :self .e .waiting == 2 ) 97self .e .ctl .release () 98self .until (lambda :len (self .order )== 2 ,s = 15.0 ) 99self .assertEqual (self .order , ["agent" ,"chat" ]) 100 101def test_off_by_default (self ): 102self .assertEqual (server .ADMIT_SHORT_FIRST if "STRATA_ADMIT_SHORT_FIRST" not in __import__ ("os" ).environ 103else 0 ,0 ) 104with mock .patch .object (server ,"ADMIT_SHORT_FIRST" ,0 ): 105order = self .queue (("agent1" ,120000 ), ("chat" ,500 )) 106self .assertEqual (sorted (order ), ["agent1" ,"chat" ])# both get their turn, in any order 107 108def test_a_continuation_is_not_held_back (self ): 109"""A request already admitted once (new=False: promoted from the solo path) is not part of the order.""" 110with mock .patch .object (server ,"ADMIT_SHORT_FIRST" ,8192 ),mock .patch .object (server ,"ADMIT_MAX_WAIT_S" ,60.0 ): 111self .assertTrue (self .e .ctl .acquire (timeout = 1 )) 112self .take ("chat" ,500 ,new = True ) 113self .take ("continuation" ,190000 ) 114self .until (lambda :self .e .waiting == 2 ) 115self .e .ctl .release () 116self .until (lambda :len (self .order )== 2 ,s = 15.0 ) 117self .assertEqual (sorted (self .order ), ["chat" ,"continuation" ]) 118 119 120if __name__ == "__main__" : 121unittest .main ()