Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

(#1252) Fix logging unit test failure #1288

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import sys
import threading
from functools import partial
Expand Down Expand Up @@ -81,7 +82,16 @@ def create_dummy_scan_spec(x_steps, y_steps, z_steps):
return [spec.consume().midpoints for spec in specs]


def _destroy_loggers(loggers):
def _reset_loggers(loggers):
"""Clear all handlers and tear down the logging hierarchy, leave logger references intact."""
clear_log_handlers(loggers)
for logger in loggers:
if logger.name != "Hyperion":
# Hyperion parent is configured on module import, do not remove
logger.parent = logging.getLogger()


def clear_log_handlers(loggers):
for logger in loggers:
for handler in logger.handlers:
handler.close()
Expand Down Expand Up @@ -115,12 +125,15 @@ def pytest_runtest_setup(item):
)
else:
print("Skipping log setup for log test - deleting existing handlers")
_destroy_loggers([*ALL_LOGGERS, dodal_logger])
_reset_loggers([*ALL_LOGGERS, dodal_logger])


def pytest_runtest_teardown():
def pytest_runtest_teardown(item):
if "dodal.beamlines.beamline_utils" in sys.modules:
sys.modules["dodal.beamlines.beamline_utils"].clear_devices()
markers = [m.name for m in item.own_markers]
if "skip_log_setup" in markers:
_reset_loggers([*ALL_LOGGERS, dodal_logger])


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/hyperion/test_log/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from hyperion.log import ALL_LOGGERS

from ....conftest import _destroy_loggers
from ....conftest import _reset_loggers


def pytest_runtest_setup():
_destroy_loggers([*ALL_LOGGERS, LOGGER])
_reset_loggers([*ALL_LOGGERS, LOGGER])


def pytest_runtest_teardown():
_destroy_loggers([*ALL_LOGGERS, LOGGER])
_reset_loggers([*ALL_LOGGERS, LOGGER])
6 changes: 3 additions & 3 deletions tests/unit_tests/hyperion/test_log/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
LogUidTaggingCallback,
)

from .conftest import _destroy_loggers
from ....conftest import clear_log_handlers


@pytest.fixture(scope="function")
def clear_and_mock_loggers():
_destroy_loggers([*log.ALL_LOGGERS, dodal_logger])
clear_log_handlers([*log.ALL_LOGGERS, dodal_logger])
mock_open_with_tell = MagicMock()
mock_open_with_tell.tell.return_value = 0
with (
Expand All @@ -30,7 +30,7 @@ def clear_and_mock_loggers():
graylog_emit.reset_mock()
filehandler_emit.reset_mock()
yield filehandler_emit, graylog_emit
_destroy_loggers([*log.ALL_LOGGERS, dodal_logger])
clear_log_handlers([*log.ALL_LOGGERS, dodal_logger])


@pytest.mark.skip_log_setup
Expand Down
Loading