okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1"""A request whose consumer stops early sends the engine STOP from Engine.generate's `finally`. When the engine is being 2ended or restarted on another thread at that moment, its stdin can already be closed (Engine.close() closes it before it 3clears `proc`) or `proc` can already be None. The stop path must not raise in any of these states: the exception would 4replace the request's own outcome and leave the generator's close() with an error (serve/replicas.py's gen.close(); seen 5as the "'NoneType' object has no attribute 'stdin'" warning in serve/test_stream_ownership.py).""" 6 7from __future__import annotations 8 9import io 10import queue 11import sys 12import threading 13import unittest 14from pathlib import Path 15from unittest import mock 16 17sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 18 19from serve import server # noqa: E402 20from serve .server import StrataEngine # noqa: E402 21 22 23class FakeProc : 24def __init__ (self ): 25self .stdin = io .StringIO () 26 27def poll (self ): 28return None 29 30 31def engine ()-> StrataEngine : 32e = StrataEngine .__new__ (StrataEngine ) 33e .alive = lambda :True 34e .batch ,e .multi ,e .silence_s ,e .can_stop = 0 ,0 ,0 ,True 35e .lines = queue .Queue () 36e .proc = FakeProc () 37e .sampling_keys = lambda sampling :"" 38e .exit_code = lambda :1 39return e 40 41 42class StopOnAClosedPipe (unittest .TestCase ): 43def run_case (self ,break_pipe ): 44e = engine () 45e .lines .put ("T 7" ) 46with mock .patch .object (server .bias_api ,"validate_request" ,lambda * a ,** k :None ), \ 47mock .patch .object (server ,"refuse_for_split" ,lambda * a ,** k :None ), \ 48mock .patch .object (server ,"ENGINE_STALL_S" ,0 ): 49gen = e .generate ([1 ,2 ,3 ],8 , {},threading .Event ()) 50self .assertEqual (next (gen ),7 ) 51break_pipe (e ) 52e .lines .put (None )# the engine's output ended (its reader thread's end mark) 53gen .close ()# must not raise, whatever state the pipe is in 54 55def test_stdin_closed (self ): 56self .run_case (lambda e :e .proc .stdin .close ())# ValueError: I/O operation on closed file 57 58def test_proc_gone (self ): 59def gone (e ): 60e .proc = None # AttributeError: 'NoneType' ... 'stdin' 61self .run_case (gone ) 62 63def test_broken_pipe_still_quiet (self ): 64def broken (e ): 65def write (_ ): 66raise BrokenPipeError (32 ,"Broken pipe" ) 67e .proc .stdin .write = write 68self .run_case (broken ) 69 70def test_open_pipe_gets_the_stop (self ): 71e = engine () 72e .lines .put ("T 7" ) 73with mock .patch .object (server .bias_api ,"validate_request" ,lambda * a ,** k :None ), \ 74mock .patch .object (server ,"refuse_for_split" ,lambda * a ,** k :None ), \ 75mock .patch .object (server ,"ENGINE_STALL_S" ,0 ): 76gen = e .generate ([1 ,2 ,3 ],8 , {},threading .Event ()) 77self .assertEqual (next (gen ),7 ) 78e .lines .put ("DONE 1 3 1.0 1.0 stop" ) 79gen .close () 80self .assertTrue (e .proc .stdin .getvalue ().endswith ("STOP\n" )) 81 82 83if __name__ == "__main__" : 84unittest .main ()