diff --git a/src/_ert/forward_model_runner/cli.py b/src/_ert/forward_model_runner/cli.py index 1d6ab0beef3..dba4d78c93a 100644 --- a/src/_ert/forward_model_runner/cli.py +++ b/src/_ert/forward_model_runner/cli.py @@ -17,6 +17,10 @@ logger = logging.getLogger(__name__) +class ForwardModelRunnerException(Exception): + pass + + def _setup_reporters( is_interactive_run, ens_id, @@ -172,11 +176,11 @@ async def _main( f"job_dispatch failed due to {oserror}. Stopping and cleaning up." ) await let_reporters_finish(reporters) - raise SystemExit(1) + raise ForwardModelRunnerException from oserror if isinstance(job_status, Finish) and not job_status.success(): await let_reporters_finish(reporters) - raise SystemExit(1) + raise ForwardModelRunnerException except asyncio.CancelledError: await let_reporters_finish(reporters) - raise SystemExit(1) + raise ForwardModelRunnerException from None diff --git a/tests/ert/unit_tests/forward_model_runner/test_job_dispatch.py b/tests/ert/unit_tests/forward_model_runner/test_job_dispatch.py index 9c473b38760..905c100733b 100644 --- a/tests/ert/unit_tests/forward_model_runner/test_job_dispatch.py +++ b/tests/ert/unit_tests/forward_model_runner/test_job_dispatch.py @@ -12,15 +12,20 @@ from textwrap import dedent from unittest.mock import mock_open, patch -from _ert.forward_model_runner.reporting.message import Finish, Init import pandas as pd import psutil import pytest import _ert.forward_model_runner.cli -from _ert.forward_model_runner.cli import JOBS_FILE, _setup_reporters, main +from _ert.forward_model_runner.cli import ( + JOBS_FILE, + ForwardModelRunnerException, + _setup_reporters, + main, +) from _ert.forward_model_runner.forward_model_step import killed_by_oom from _ert.forward_model_runner.reporting import Event, Interactive +from _ert.forward_model_runner.reporting.message import Finish, Init from tests.ert.utils import _mock_ws_task, async_wait_until, wait_until @@ -351,6 +356,7 @@ async def test_job_dispatch_kills_itself_after_unsuccessful_job(unused_tcp_port) patch("_ert.forward_model_runner.cli.open", new=mock_open(read_data=jobs_json)), patch("_ert.forward_model_runner.cli.ForwardModelRunner") as mock_runner, ): + async def mock_run_method(*args, **kwargs): events = [ Init([], 0, 0), @@ -359,13 +365,12 @@ async def mock_run_method(*args, **kwargs): for event in events: await asyncio.sleep(0) yield event + mock_runner.return_value.run = mock_run_method async with _mock_ws_task(host, port, []): - tsk = asyncio.create_task(main(["script.py"])) - await tsk - assert isinstance(tsk.exception, SystemExit) - await asyncio.sleep(0) + with pytest.raises(ForwardModelRunnerException): + await main(["script.py"]) @pytest.mark.skipif(sys.platform.startswith("darwin"), reason="No oom_score on MacOS")