okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
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 ): 25def app (self ,run ): 26app = LaunchApp .__new__ (LaunchApp ) 27loop = asyncio .new_event_loop () 28loop .close ()# the app has quit: its loop is closed 29app ._loop = loop 30app ._thread_id = - 1 # (the worker is another thread than the app's) 31app .run_screen = mock .Mock () 32app .runner = SimpleNamespace (run = run ) 33app .ctx = SimpleNamespace (settings = SimpleNamespace (name = None )) 34app ._save_pending = lambda :None 35app .running = True 36return app 37 38def check (self ,run ): 39app = self .app (run ) 40with warnings .catch_warnings (): 41warnings .simplefilter ("ignore" ) 42with self .assertRaises (RuntimeError ):# the precondition: this app's loop refuses a call 43app .call_from_thread (lambda :None ) 44gc .collect ()# (that call's own unawaited coroutine, not _work's) 45with warnings .catch_warnings (record = True )as seen : 46warnings .simplefilter ("always" ) 47app ._work ()# must not raise in the worker thread 48gc .collect () 49self .assertFalse (app .running ) 50self .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 53def test_finished (self ): 54self .check (lambda :"result" ) 55 56def test_cancelled (self ): 57def run (): 58raise Cancelled () 59self .check (run ) 60 61def test_launch_error (self ): 62def run (): 63raise LaunchError ("could not start" ,"try again" ) 64self .check (run ) 65 66def test_unexpected_error (self ): 67def run (): 68raise KeyError ("x" ) 69self .check (run ) 70 71 72if __name__ == "__main__" : 73unittest .main ()