Enable open protection in the event loop (#117289)

This commit is contained in:
J. Nick Koston
2024-05-13 08:50:31 +09:00
committed by GitHub
parent d06932bbc2
commit 11f49280c9
2 changed files with 55 additions and 4 deletions

View File

@@ -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