From 1e198b1c7557842f4cbb9f519c2a9f6807d56487 Mon Sep 17 00:00:00 2001 From: Mike Nitchie Date: Tue, 28 Mar 2023 21:51:39 -0400 Subject: [PATCH 1/6] Stash --- health_check/conf.py | 1 + health_check/mixins.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/health_check/conf.py b/health_check/conf.py index fdd2b1ec..6288c511 100644 --- a/health_check/conf.py +++ b/health_check/conf.py @@ -4,3 +4,4 @@ HEALTH_CHECK.setdefault("DISK_USAGE_MAX", 90) HEALTH_CHECK.setdefault("MEMORY_MIN", 100) HEALTH_CHECK.setdefault("WARNINGS_AS_ERRORS", True) +HEALTH_CHECK.setdefault("DISABLE_THREADING", False) diff --git a/health_check/mixins.py b/health_check/mixins.py index fefa29e9..62244fe2 100644 --- a/health_check/mixins.py +++ b/health_check/mixins.py @@ -40,16 +40,22 @@ def _run(plugin): connections.close_all() + def _collect_errors(plugin): + if plugin.critical_service: + if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]: + errors.extend( + e for e in plugin.errors if not isinstance(e, ServiceWarning) + ) + else: + errors.extend(plugin.errors) + + if HEALTH_CHECK["DISABLE_THREADING"]: + for plugin in self.plugins: + _run(plugin) + _collect_errors(plugin) + with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor: for plugin in executor.map(_run, self.plugins): - if plugin.critical_service: - if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]: - errors.extend( - e - for e in plugin.errors - if not isinstance(e, ServiceWarning) - ) - else: - errors.extend(plugin.errors) + _collect_errors(plugin) return errors From 49b081af4d417e9046d4a4dbdecaac7b8f24c6cd Mon Sep 17 00:00:00 2001 From: Michael Thompson Date: Wed, 29 Mar 2023 16:51:17 -0400 Subject: [PATCH 2/6] untested tests --- health_check/mixins.py | 8 ++++---- tests/test_mixins.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/health_check/mixins.py b/health_check/mixins.py index 62244fe2..8b3e5313 100644 --- a/health_check/mixins.py +++ b/health_check/mixins.py @@ -53,9 +53,9 @@ def _collect_errors(plugin): for plugin in self.plugins: _run(plugin) _collect_errors(plugin) - - with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor: - for plugin in executor.map(_run, self.plugins): - _collect_errors(plugin) + else: + with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor: + for plugin in executor.map(_run, self.plugins): + _collect_errors(plugin) return errors diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 1a35e936..40bd97a8 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -1,8 +1,10 @@ import pytest +from unittest.mock import patch from health_check.backends import BaseHealthCheckBackend from health_check.mixins import CheckMixin from health_check.plugins import plugin_dir +from health_check.conf import HEALTH_CHECK class FailPlugin(BaseHealthCheckBackend): @@ -36,3 +38,17 @@ def test_errors(self): def test_run_check(self): assert len(Checker().run_check()) == 1 + + def test_run_check_threading_enabled(self, monkeypatch): + monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", False) + + with patch("health_check.mixins.ThreadPoolExecutor") as tpe: + Checker().run_check() + tpe.assert_called() + + def test_run_check_threading_disabled(self, monkeypatch): + monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", True) + + with patch("health_check.mixins.ThreadPoolExecutor") as tpe: + Checker().run_check() + tpe.assert_not_called() From 181d2ad1965cf3189f5c6204fcb608415c3a9863 Mon Sep 17 00:00:00 2001 From: Michael Thompson Date: Wed, 29 Mar 2023 16:57:12 -0400 Subject: [PATCH 3/6] adding comments --- tests/test_mixins.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 40bd97a8..76f1727f 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -40,15 +40,23 @@ def test_run_check(self): assert len(Checker().run_check()) == 1 def test_run_check_threading_enabled(self, monkeypatch): + """Ensure threading used when not disabled.""" + + # Ensure threading is enabled. monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", False) + # Ensure ThreadPoolExecutor is used. with patch("health_check.mixins.ThreadPoolExecutor") as tpe: Checker().run_check() tpe.assert_called() def test_run_check_threading_disabled(self, monkeypatch): + """Ensure threading not used when disabled.""" + + # Ensure threading is disabled. monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", True) + # Ensure ThreadPoolExecutor is not used. with patch("health_check.mixins.ThreadPoolExecutor") as tpe: Checker().run_check() tpe.assert_not_called() From 684e76114083b4ce9b63c1086d301577cb69d94e Mon Sep 17 00:00:00 2001 From: Michael Thompson Date: Wed, 29 Mar 2023 16:59:52 -0400 Subject: [PATCH 4/6] kicking things for CI --- tests/test_mixins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 76f1727f..2a5b3fd8 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -42,10 +42,10 @@ def test_run_check(self): def test_run_check_threading_enabled(self, monkeypatch): """Ensure threading used when not disabled.""" - # Ensure threading is enabled. + # Ensure threading is enabled monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", False) - # Ensure ThreadPoolExecutor is used. + # Ensure ThreadPoolExecutor is used with patch("health_check.mixins.ThreadPoolExecutor") as tpe: Checker().run_check() tpe.assert_called() @@ -53,10 +53,10 @@ def test_run_check_threading_enabled(self, monkeypatch): def test_run_check_threading_disabled(self, monkeypatch): """Ensure threading not used when disabled.""" - # Ensure threading is disabled. + # Ensure threading is disabled monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", True) - # Ensure ThreadPoolExecutor is not used. + # Ensure ThreadPoolExecutor is not used with patch("health_check.mixins.ThreadPoolExecutor") as tpe: Checker().run_check() tpe.assert_not_called() From c76b83b6ecc57ad85d1a02b144f1ec5318b9c45c Mon Sep 17 00:00:00 2001 From: Mike Nitchie Date: Wed, 29 Mar 2023 19:58:49 -0400 Subject: [PATCH 5/6] Finish tests --- tests/test_mixins.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 2a5b3fd8..10c4c40e 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -30,20 +30,29 @@ def setup(self): yield plugin_dir.reset() - def test_plugins(self): + @pytest.mark.parametrize("disable_threading", [(True,), (False,)]) + def test_plugins(self, monkeypatch, disable_threading): + monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading) + assert len(Checker().plugins) == 2 - def test_errors(self): + @pytest.mark.parametrize("disable_threading", [(True,), (False,)]) + def test_errors(self, monkeypatch, disable_threading): + monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading) + assert len(Checker().errors) == 1 - def test_run_check(self): + @pytest.mark.parametrize("disable_threading", [(True,), (False,)]) + def test_run_check(self, monkeypatch, disable_threading): + monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading) + assert len(Checker().run_check()) == 1 def test_run_check_threading_enabled(self, monkeypatch): """Ensure threading used when not disabled.""" - # Ensure threading is enabled - monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", False) + # Ensure threading is enabled. + monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", False) # Ensure ThreadPoolExecutor is used with patch("health_check.mixins.ThreadPoolExecutor") as tpe: @@ -53,8 +62,8 @@ def test_run_check_threading_enabled(self, monkeypatch): def test_run_check_threading_disabled(self, monkeypatch): """Ensure threading not used when disabled.""" - # Ensure threading is disabled - monkeypatch.setattr(HEALTH_CHECK, "DISABLE_THREADING", True) + # Ensure threading is disabled. + monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", True) # Ensure ThreadPoolExecutor is not used with patch("health_check.mixins.ThreadPoolExecutor") as tpe: From ab099c402b58efa5f9bdff4771c0573d84fc4e54 Mon Sep 17 00:00:00 2001 From: Mike Nitchie Date: Wed, 29 Mar 2023 20:01:38 -0400 Subject: [PATCH 6/6] Add ci tests --- tests/test_mixins.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 10c4c40e..ca06b5e1 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -1,10 +1,11 @@ -import pytest from unittest.mock import patch +import pytest + from health_check.backends import BaseHealthCheckBackend +from health_check.conf import HEALTH_CHECK from health_check.mixins import CheckMixin from health_check.plugins import plugin_dir -from health_check.conf import HEALTH_CHECK class FailPlugin(BaseHealthCheckBackend):