okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-028-tools-p1b82d1d456

main
2.6 KiB57 linesraw
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
7    python -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):
19    def run_it(self, leftovers=None, orphans=None, env=None):
20        out = io.StringIO()
21        with mock.patch.object(procs, "end_leftovers", return_value=leftovers or {"stuck": []}) as ends, \
22                mock.patch.object(procs, "end_orphans", return_value=orphans or []) as orph, \
23                mock.patch.dict("os.environ", env or {}, clear=False), contextlib.redirect_stdout(out):
24            server.end_leftover_engines("/x/strata")
25        return out.getvalue(), ends, orph
26
27    def test_the_wait_is_long_enough_for_a_big_engine(self):
28        _, ends, orph = self.run_it()
29        self.assertGreaterEqual(server.LEFTOVER_SETTLE_S, 60)
30        self.assertEqual(ends.call_args.kwargs["settle"], server.LEFTOVER_SETTLE_S)
31        self.assertEqual(orph.call_args.kwargs["settle"], server.LEFTOVER_SETTLE_S)
32
33    def test_nothing_left_says_nothing(self):
34        text, _, _ = self.run_it(orphans=[{"pid": 7, "outcome": "ended"}])
35        self.assertEqual(text, "")
36
37    def test_an_engine_that_is_still_there_is_said_and_the_start_goes_on(self):
38        text, _, _ = self.run_it(leftovers={"stuck": [{"pid": 4242, "role": "engine"}]},
39                                 orphans=[{"pid": 4343, "outcome": "stuck"}])
40        self.assertIn("4242, 4343", text)
41        self.assertIn("WARNING", text)
42        self.assertIn("wait until that process is gone", text)
43
44    def test_keep_leftovers_leaves_them_alone(self):
45        _, ends, orph = self.run_it(env={"STRATA_KEEP_LEFTOVERS": "1"})
46        ends.assert_not_called()
47        orph.assert_not_called()
48
49    def test_a_failed_check_never_stops_a_start(self):
50        out = io.StringIO()
51        with mock.patch.object(procs, "end_leftovers", side_effect=RuntimeError("x")), contextlib.redirect_stdout(out):
52            server.end_leftover_engines("/x/strata")
53        self.assertIn("could not check", out.getvalue())
54
55
56if __name__ == "__main__":
57    unittest.main()