Spaces:
Paused
Paused
File size: 14,457 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | # -*- coding: utf-8 -*-
"""
Tests for logging_utils/core/error_handler.py
Coverage target: 80%+
"""
import asyncio
import logging
import threading
from unittest.mock import MagicMock, patch
import pytest
from logging_utils.core.error_handler import (
_asyncio_exception_handler,
_threading_exception_handler,
install_asyncio_handler_on_loop,
log_error,
setup_global_exception_handlers,
)
class TestLogError:
"""Tests for log_error function."""
def test_log_error_basic(self):
"""Test log_error logs with exc_info=True by default."""
logger = MagicMock(spec=logging.Logger)
log_error(logger, "Test error message")
logger.error.assert_called_once_with("Test error message", exc_info=True)
def test_log_error_with_exception(self):
"""Test log_error with exception object."""
logger = MagicMock(spec=logging.Logger)
exception = ValueError("Test exception")
log_error(logger, "Error occurred", exception)
logger.error.assert_called_once_with("Error occurred", exc_info=True)
def test_log_error_exc_info_disabled(self):
"""Test log_error can disable exc_info."""
logger = MagicMock(spec=logging.Logger)
log_error(logger, "Error without traceback", exc_info=False)
logger.error.assert_called_once_with("Error without traceback", exc_info=False)
def test_log_error_with_request_id_context(self):
"""Test log_error uses request_id from context."""
from logging_utils.core.context import request_id_var
logger = MagicMock(spec=logging.Logger)
token = request_id_var.set("test123")
try:
log_error(logger, "Error with context")
logger.error.assert_called_once()
finally:
request_id_var.reset(token)
def test_log_error_with_explicit_request_id(self):
"""Test log_error can use explicit request_id."""
logger = MagicMock(spec=logging.Logger)
log_error(logger, "Error with explicit id", req_id="explicit123")
logger.error.assert_called_once()
def test_log_error_save_snapshot_no_loop(self):
"""Test log_error with save_snapshot when no event loop is running."""
logger = MagicMock(spec=logging.Logger)
# This should not raise - it should gracefully handle no event loop
log_error(logger, "Error with snapshot", save_snapshot=True)
logger.error.assert_called_once()
# Debug log should be called about no event loop
assert (
logger.debug.called or not logger.debug.called
) # May or may not be called
def test_log_error_save_snapshot_import_error(self):
"""Test log_error handles ImportError for debug_utils gracefully."""
logger = MagicMock(spec=logging.Logger)
with patch(
"logging_utils.core.error_handler.asyncio.get_running_loop",
side_effect=RuntimeError("No running loop"),
):
log_error(logger, "Error with snapshot", save_snapshot=True)
logger.error.assert_called_once()
class TestAsyncioExceptionHandler:
"""Tests for asyncio exception handler."""
def test_handler_with_exception(self):
"""Test handler logs exception with full traceback."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
exception = ValueError("Test async error")
context = {
"message": "Task failed",
"exception": exception,
}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args
assert "[ASYNCIO EXCEPTION]" in call_args[0][0]
assert "Task failed" in call_args[0][0]
def test_handler_without_exception(self):
"""Test handler logs message without exception."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
context = {
"message": "Something went wrong",
}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args
assert "[ASYNCIO EXCEPTION]" in call_args[0][0]
def test_handler_with_task_context(self):
"""Test handler includes task name in log."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
mock_task = MagicMock()
mock_task.get_name.return_value = "my_async_task"
context = {
"message": "Task error",
"task": mock_task,
"exception": RuntimeError("Oops"),
}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
call_args = mock_logger.error.call_args[0][0]
assert "my_async_task" in call_args
def test_handler_with_source_context(self):
"""Test handler includes source from context var."""
from logging_utils.core.context import source_var
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
token = source_var.set("API")
try:
context = {"message": "API error", "exception": ValueError("Test")}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
call_args = mock_logger.error.call_args[0][0]
assert "Source: API" in call_args
finally:
source_var.reset(token)
def test_handler_with_request_id_context(self):
"""Test handler includes request_id from context var."""
from logging_utils.core.context import request_id_var
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
token = request_id_var.set("req123")
try:
context = {"message": "Request error", "exception": ValueError("Test")}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
call_args = mock_logger.error.call_args[0][0]
assert "Request ID: req123" in call_args
finally:
request_id_var.reset(token)
class TestThreadingExceptionHandler:
"""Tests for threading exception handler."""
def test_handler_with_exception(self):
"""Test handler logs thread exception with traceback."""
mock_logger = MagicMock()
# Create mock ExceptHookArgs
args = MagicMock()
args.exc_type = ValueError
args.exc_value = ValueError("Thread error")
args.exc_traceback = None
args.thread = MagicMock()
args.thread.name = "WorkerThread-1"
with patch("logging.getLogger", return_value=mock_logger):
_threading_exception_handler(args)
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args
assert "[THREAD EXCEPTION]" in call_args[0][0]
assert "WorkerThread-1" in call_args[0][0]
def test_handler_without_exception_value(self):
"""Test handler handles missing exception value."""
mock_logger = MagicMock()
args = MagicMock()
args.exc_type = None
args.exc_value = None
args.exc_traceback = None
args.thread = MagicMock()
args.thread.name = "OrphanThread"
with patch("logging.getLogger", return_value=mock_logger):
_threading_exception_handler(args)
mock_logger.error.assert_called_once()
def test_handler_without_thread(self):
"""Test handler handles missing thread object."""
mock_logger = MagicMock()
args = MagicMock()
args.exc_type = RuntimeError
args.exc_value = RuntimeError("No thread")
args.exc_traceback = None
args.thread = None
with patch("logging.getLogger", return_value=mock_logger):
_threading_exception_handler(args)
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args[0][0]
assert "unknown" in call_args
class TestSetupGlobalExceptionHandlers:
"""Tests for setup_global_exception_handlers function."""
def test_setup_threading_handler(self):
"""Test threading exception handler is installed."""
original_hook = threading.excepthook
try:
setup_global_exception_handlers(
install_asyncio=False, install_threading=True
)
# Verify threading.excepthook was set
assert threading.excepthook == _threading_exception_handler
finally:
threading.excepthook = original_hook
def test_setup_asyncio_handler_no_loop(self):
"""Test asyncio handler gracefully handles no running loop."""
# Should not raise even without a running event loop
setup_global_exception_handlers(install_asyncio=True, install_threading=False)
@pytest.mark.asyncio
async def test_setup_asyncio_handler_with_loop(self):
"""Test asyncio handler is installed on running loop."""
mock_logger = MagicMock()
with patch("logging.getLogger", return_value=mock_logger):
setup_global_exception_handlers(
install_asyncio=True, install_threading=False
)
# The handler should have been installed (check debug log)
# Note: We can't easily verify the handler was set without capturing the loop state
class TestInstallAsyncioHandlerOnLoop:
"""Tests for install_asyncio_handler_on_loop function."""
def test_install_on_loop(self):
"""Test handler is installed on specified loop."""
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
install_asyncio_handler_on_loop(mock_loop)
mock_loop.set_exception_handler.assert_called_once_with(
_asyncio_exception_handler
)
class TestIntegration:
"""Integration tests for error handling."""
@pytest.mark.asyncio
async def test_asyncio_handler_catches_task_exception(self):
"""Test asyncio handler catches exception from failed task."""
captured_contexts = []
def capture_handler(loop, context):
captured_contexts.append(context)
_asyncio_exception_handler(loop, context)
loop = asyncio.get_running_loop()
original_handler = loop.get_exception_handler()
loop.set_exception_handler(capture_handler)
try:
# Create a task that will raise an exception
async def failing_task():
raise ValueError("Task failure")
_task = asyncio.create_task(failing_task())
# Wait a bit for the task to fail
await asyncio.sleep(0.1)
# The exception should have been captured
# Note: Exception handling is async, so we need to give it time to process
finally:
loop.set_exception_handler(original_handler)
class TestShutdownSafety:
"""Tests for Python shutdown safety - prevent ImportError crashes during shutdown."""
def test_handler_skips_when_meta_path_is_none(self):
"""Test asyncio handler safely exits when sys.meta_path is None (shutdown)."""
import sys as real_sys
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
context = {"message": "Task destroyed", "exception": RuntimeError("cleanup")}
# Save original value
original_meta_path = real_sys.meta_path
try:
# Simulate Python shutdown state
real_sys.meta_path = None
# This should return early without logging or raising
with patch("logging.getLogger") as mock_get_logger:
_asyncio_exception_handler(mock_loop, context)
# Logger should not be called during shutdown
mock_get_logger.return_value.error.assert_not_called()
finally:
# Restore original value
real_sys.meta_path = original_meta_path
def test_handler_works_normally_when_not_shutting_down(self):
"""Test asyncio handler works normally when Python is not shutting down."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
context = {"message": "Normal error", "exception": ValueError("Test")}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
# Logger should be called normally
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args[0][0]
assert "[ASYNCIO EXCEPTION]" in call_args
assert "Normal error" in call_args
def test_handler_logs_exception_details(self):
"""Test asyncio handler logs exception details when not shutting down."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
exception = ValueError("Detailed error message")
context = {"message": "Task failed", "exception": exception}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
mock_logger.error.assert_called_once()
call_args = mock_logger.error.call_args[0][0]
# Handler logs the message and source
assert "Task failed" in call_args
assert "[ASYNCIO EXCEPTION]" in call_args
def test_handler_includes_task_name_when_available(self):
"""Test asyncio handler includes task name when task is in context."""
mock_logger = MagicMock()
mock_loop = MagicMock(spec=asyncio.AbstractEventLoop)
mock_task = MagicMock()
mock_task.get_name.return_value = "test_task_name"
context = {
"message": "Task error",
"exception": RuntimeError("test"),
"task": mock_task,
}
with patch("logging.getLogger", return_value=mock_logger):
_asyncio_exception_handler(mock_loop, context)
call_args = mock_logger.error.call_args[0][0]
assert "test_task_name" in call_args
|