Skip to content

Commit 7328eaa

Browse files
mnitchieMichael ThompsonActionScripted
authored
Add configuration for running health checks without threads (#362)
* Stash * untested tests * adding comments * kicking things for CI * Finish tests * Add ci tests --------- Co-authored-by: Michael Thompson <michael.thompson@strataoncology.com> Co-authored-by: Michael Thompson <actionscripted@gmail.com>
1 parent 593594f commit 7328eaa

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

health_check/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
HEALTH_CHECK.setdefault("DISK_USAGE_MAX", 90)
55
HEALTH_CHECK.setdefault("MEMORY_MIN", 100)
66
HEALTH_CHECK.setdefault("WARNINGS_AS_ERRORS", True)
7+
HEALTH_CHECK.setdefault("DISABLE_THREADING", False)

health_check/mixins.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ def _run(plugin):
4040

4141
connections.close_all()
4242

43-
with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor:
44-
for plugin in executor.map(_run, self.plugins):
45-
if plugin.critical_service:
46-
if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]:
47-
errors.extend(
48-
e
49-
for e in plugin.errors
50-
if not isinstance(e, ServiceWarning)
51-
)
52-
else:
53-
errors.extend(plugin.errors)
43+
def _collect_errors(plugin):
44+
if plugin.critical_service:
45+
if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]:
46+
errors.extend(
47+
e for e in plugin.errors if not isinstance(e, ServiceWarning)
48+
)
49+
else:
50+
errors.extend(plugin.errors)
51+
52+
if HEALTH_CHECK["DISABLE_THREADING"]:
53+
for plugin in self.plugins:
54+
_run(plugin)
55+
_collect_errors(plugin)
56+
else:
57+
with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor:
58+
for plugin in executor.map(_run, self.plugins):
59+
_collect_errors(plugin)
5460

5561
return errors

tests/test_mixins.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from unittest.mock import patch
2+
13
import pytest
24

35
from health_check.backends import BaseHealthCheckBackend
6+
from health_check.conf import HEALTH_CHECK
47
from health_check.mixins import CheckMixin
58
from health_check.plugins import plugin_dir
69

@@ -28,11 +31,42 @@ def setup(self):
2831
yield
2932
plugin_dir.reset()
3033

31-
def test_plugins(self):
34+
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
35+
def test_plugins(self, monkeypatch, disable_threading):
36+
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)
37+
3238
assert len(Checker().plugins) == 2
3339

34-
def test_errors(self):
40+
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
41+
def test_errors(self, monkeypatch, disable_threading):
42+
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)
43+
3544
assert len(Checker().errors) == 1
3645

37-
def test_run_check(self):
46+
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
47+
def test_run_check(self, monkeypatch, disable_threading):
48+
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)
49+
3850
assert len(Checker().run_check()) == 1
51+
52+
def test_run_check_threading_enabled(self, monkeypatch):
53+
"""Ensure threading used when not disabled."""
54+
55+
# Ensure threading is enabled.
56+
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", False)
57+
58+
# Ensure ThreadPoolExecutor is used
59+
with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
60+
Checker().run_check()
61+
tpe.assert_called()
62+
63+
def test_run_check_threading_disabled(self, monkeypatch):
64+
"""Ensure threading not used when disabled."""
65+
66+
# Ensure threading is disabled.
67+
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", True)
68+
69+
# Ensure ThreadPoolExecutor is not used
70+
with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
71+
Checker().run_check()
72+
tpe.assert_not_called()

0 commit comments

Comments
 (0)