okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b82d1d456
main
1"""#4051: an engine an earlier run left behind is waited for until it has given its memory back. 2 3The new engine's arena is as big as the old one's; started beside it, a 60 GiB PC went into memory reclaim for good. The 4server ends the old engine before it starts the next (strata_control.procs) and now waits for it to be gone for as long as 5a big engine needs; one that is still there after that is said in plain words, and the start goes on (nothing is refused). 6 7python -m unittest serve.test_leftover_engines -v 8""" 9import contextlib 10import io 11import unittest 12from unittest import mock 13 14from serve import server 15from strata_control import procs 16 17 18class LeftoverEngines (unittest .TestCase ): 19def run_it (self ,leftovers = None ,orphans = None ,env = None ): 20out = io .StringIO () 21with mock .patch .object (procs ,"end_leftovers" ,return_value = leftovers or {"stuck" : []})as ends , \ 22mock .patch .object (procs ,"end_orphans" ,return_value = orphans or [])as orph , \ 23mock .patch .dict ("os.environ" ,env or {},clear = False ),contextlib .redirect_stdout (out ): 24server .end_leftover_engines ("/x/strata" ) 25return out .getvalue (),ends ,orph 26 27def test_the_wait_is_long_enough_for_a_big_engine (self ): 28_ ,ends ,orph = self .run_it () 29self .assertGreaterEqual (server .LEFTOVER_SETTLE_S ,60 ) 30self .assertEqual (ends .call_args .kwargs ["settle" ],server .LEFTOVER_SETTLE_S ) 31self .assertEqual (orph .call_args .kwargs ["settle" ],server .LEFTOVER_SETTLE_S ) 32 33def test_nothing_left_says_nothing (self ): 34text ,_ ,_ = self .run_it (orphans = [{"pid" :7 ,"outcome" :"ended" }]) 35self .assertEqual (text ,"" ) 36 37def test_an_engine_that_is_still_there_is_said_and_the_start_goes_on (self ): 38text ,_ ,_ = self .run_it (leftovers = {"stuck" : [{"pid" :4242 ,"role" :"engine" }]}, 39orphans = [{"pid" :4343 ,"outcome" :"stuck" }]) 40self .assertIn ("4242, 4343" ,text ) 41self .assertIn ("WARNING" ,text ) 42self .assertIn ("wait until that process is gone" ,text ) 43 44def test_keep_leftovers_leaves_them_alone (self ): 45_ ,ends ,orph = self .run_it (env = {"STRATA_KEEP_LEFTOVERS" :"1" }) 46ends .assert_not_called () 47orph .assert_not_called () 48 49def test_a_failed_check_never_stops_a_start (self ): 50out = io .StringIO () 51with mock .patch .object (procs ,"end_leftovers" ,side_effect = RuntimeError ("x" )),contextlib .redirect_stdout (out ): 52server .end_leftover_engines ("/x/strata" ) 53self .assertIn ("could not check" ,out .getvalue ()) 54 55 56if __name__ == "__main__" : 57unittest .main ()