From 59e2d54cbc1633fb4a4a2237a4df39588884af58 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Tue, 19 Nov 2024 23:39:54 +0100 Subject: [PATCH] fix: treat temporary failures as activity When an activity check fails with a TemporaryCheckError, we now treat that situation as activity to prevent unintentional shutdowns of the system due to having a check configured wrongly or simply because of a temporary name resolution error or alike. Fixes: #589 --- src/autosuspend/__init__.py | 2 +- tests/test_autosuspend.py | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/autosuspend/__init__.py b/src/autosuspend/__init__.py index 5e997bf1..2f3fd635 100755 --- a/src/autosuspend/__init__.py +++ b/src/autosuspend/__init__.py @@ -119,7 +119,7 @@ def _safe_execute_activity(check: Activity, logger: logging.Logger) -> str | Non return check.check() except TemporaryCheckError: logger.warning("Check %s failed. Ignoring...", check, exc_info=True) - return None + return f"Check {check.name} failed temporarily" def execute_checks( diff --git a/tests/test_autosuspend.py b/tests/test_autosuspend.py index 00cd98fb..d7a01c86 100644 --- a/tests/test_autosuspend.py +++ b/tests/test_autosuspend.py @@ -294,22 +294,16 @@ def test_all_called(self, mocker: MockerFixture) -> None: matching_check.check.assert_called_once_with() second_check.check.assert_called_once_with() - def test_ignore_temporary_errors(self, mocker: MockerFixture) -> None: + def test_treat_temporary_errors_as_activity(self, mocker: MockerFixture) -> None: matching_check = mocker.MagicMock(spec=autosuspend.Activity) matching_check.name = "foo" matching_check.check.side_effect = autosuspend.TemporaryCheckError() - second_check = mocker.MagicMock() - second_check.name = "bar" - second_check.check.return_value = "matches" assert ( - autosuspend.execute_checks( - [matching_check, second_check], False, mocker.MagicMock() - ) + autosuspend.execute_checks([matching_check], False, mocker.MagicMock()) is True ) matching_check.check.assert_called_once_with() - second_check.check.assert_called_once_with() class TestExecuteWakeups: