mirror of
https://github.com/home-assistant/core.git
synced 2025-11-08 10:29:27 +00:00
Enable open protection in the event loop (#117289)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
"""Tests for async util methods from Python source."""
|
||||
|
||||
import contextlib
|
||||
import importlib
|
||||
from pathlib import Path, PurePosixPath
|
||||
import time
|
||||
from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
@@ -198,3 +201,37 @@ async def test_protect_loop_importlib_import_module_in_integration(
|
||||
"Detected blocking call to import_module inside the event loop by "
|
||||
"integration 'hue' at homeassistant/components/hue/light.py, line 23"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
async def test_protect_loop_open(caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test open of a file in /proc is not reported."""
|
||||
block_async_io.enable()
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
open("/proc/does_not_exist").close()
|
||||
assert "Detected blocking call to open with args" not in caplog.text
|
||||
|
||||
|
||||
async def test_protect_open(caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test opening a file in the event loop logs."""
|
||||
block_async_io.enable()
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
open("/config/data_not_exist").close()
|
||||
|
||||
assert "Detected blocking call to open with args" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path",
|
||||
[
|
||||
"/config/data_not_exist",
|
||||
Path("/config/data_not_exist"),
|
||||
PurePosixPath("/config/data_not_exist"),
|
||||
],
|
||||
)
|
||||
async def test_protect_open_path(path: Any, caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test opening a file by path in the event loop logs."""
|
||||
block_async_io.enable()
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
open(path).close()
|
||||
|
||||
assert "Detected blocking call to open with args" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user