1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Don't warn on time.sleep injected by the debugger (#65420)

This commit is contained in:
Erik Montnemery
2022-02-02 17:58:14 +01:00
committed by GitHub
parent c8e64358b1
commit 5a34feb7de
2 changed files with 53 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, Mock, patch
import pytest
from homeassistant import block_async_io
from homeassistant.util import async_ as hasync
@@ -70,10 +71,14 @@ def test_run_callback_threadsafe_from_inside_event_loop(mock_ident, _):
assert len(loop.call_soon_threadsafe.mock_calls) == 2
def banned_function():
"""Mock banned function."""
async def test_check_loop_async():
"""Test check_loop detects when called from event loop without integration context."""
with pytest.raises(RuntimeError):
hasync.check_loop()
hasync.check_loop(banned_function)
async def test_check_loop_async_integration(caplog):
@@ -98,7 +103,7 @@ async def test_check_loop_async_integration(caplog):
),
],
):
hasync.check_loop()
hasync.check_loop(banned_function)
assert (
"Detected blocking call inside the event loop. This is causing stability issues. "
"Please report issue for hue doing blocking calls at "
@@ -129,7 +134,7 @@ async def test_check_loop_async_integration_non_strict(caplog):
),
],
):
hasync.check_loop(strict=False)
hasync.check_loop(banned_function, strict=False)
assert (
"Detected blocking call inside the event loop. This is causing stability issues. "
"Please report issue for hue doing blocking calls at "
@@ -160,7 +165,7 @@ async def test_check_loop_async_custom(caplog):
),
],
):
hasync.check_loop()
hasync.check_loop(banned_function)
assert (
"Detected blocking call inside the event loop. This is causing stability issues. "
"Please report issue to the custom component author for hue doing blocking calls "
@@ -170,7 +175,7 @@ async def test_check_loop_async_custom(caplog):
def test_check_loop_sync(caplog):
"""Test check_loop does nothing when called from thread."""
hasync.check_loop()
hasync.check_loop(banned_function)
assert "Detected blocking call inside the event loop" not in caplog.text
@@ -179,10 +184,38 @@ def test_protect_loop_sync():
func = Mock()
with patch("homeassistant.util.async_.check_loop") as mock_check_loop:
hasync.protect_loop(func)(1, test=2)
mock_check_loop.assert_called_once_with(strict=True)
mock_check_loop.assert_called_once_with(func, strict=True)
func.assert_called_once_with(1, test=2)
async def test_protect_loop_debugger_sleep(caplog):
"""Test time.sleep injected by the debugger is not reported."""
block_async_io.enable()
with patch(
"homeassistant.util.async_.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/.venv/blah/pydevd.py",
lineno="23",
line="do_something()",
),
Mock(
filename="/home/paulus/homeassistant/util/async.py",
lineno="123",
line="protected_loop_func",
),
Mock(
filename="/home/paulus/homeassistant/util/async.py",
lineno="123",
line="check_loop()",
),
],
):
time.sleep(0)
assert "Detected blocking call inside the event loop" not in caplog.text
async def test_gather_with_concurrency():
"""Test gather_with_concurrency limits the number of running tasks."""