okami.mom/strata

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

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
2.8 KiB73 linesraw
1"""The launch screen's worker thread (LaunchApp._work) reports the run's end to the screen with call_from_thread.  When
2the person quits while the run is ending, the app's event loop is already closed and call_from_thread raises
3RuntimeError ("Event loop is closed") in that thread, which printed a traceback after the screen was gone.  Every one of
4those calls must be as guarded as _emit's and _ask_from_run's already are.  Here the app's loop is a closed asyncio loop
5(the state after a quit) and _work runs each way a run can end."""
6
7from __future__ import annotations
8
9import asyncio
10import gc
11import sys
12import unittest
13import warnings
14from pathlib import Path
15from types import SimpleNamespace
16from unittest import mock
17
18sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20from strata_cli.launch.app import LaunchApp  # noqa: E402
21from strata_cli.launch.runner import Cancelled, LaunchError  # noqa: E402
22
23
24class WorkerAfterQuit(unittest.TestCase):
25    def app(self, run):
26        app = LaunchApp.__new__(LaunchApp)
27        loop = asyncio.new_event_loop()
28        loop.close()                                       # the app has quit: its loop is closed
29        app._loop = loop
30        app._thread_id = -1                                # (the worker is another thread than the app's)
31        app.run_screen = mock.Mock()
32        app.runner = SimpleNamespace(run=run)
33        app.ctx = SimpleNamespace(settings=SimpleNamespace(name=None))
34        app._save_pending = lambda: None
35        app.running = True
36        return app
37
38    def check(self, run):
39        app = self.app(run)
40        with warnings.catch_warnings():
41            warnings.simplefilter("ignore")
42            with self.assertRaises(RuntimeError):          # the precondition: this app's loop refuses a call
43                app.call_from_thread(lambda: None)
44            gc.collect()                                   # (that call's own unawaited coroutine, not _work's)
45        with warnings.catch_warnings(record=True) as seen:
46            warnings.simplefilter("always")
47            app._work()                                    # must not raise in the worker thread
48            gc.collect()
49        self.assertFalse(app.running)
50        self.assertEqual([str(w.message) for w in seen if "never awaited" in str(w.message)], [],
51                         "a call to the closed app left a coroutine behind (a warning on the terminal)")
52
53    def test_finished(self):
54        self.check(lambda: "result")
55
56    def test_cancelled(self):
57        def run():
58            raise Cancelled()
59        self.check(run)
60
61    def test_launch_error(self):
62        def run():
63            raise LaunchError("could not start", "try again")
64        self.check(run)
65
66    def test_unexpected_error(self):
67        def run():
68            raise KeyError("x")
69        self.check(run)
70
71
72if __name__ == "__main__":
73    unittest.main()