Warn ontime.sleep in event loop (#63766)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Matthias Alphart
2022-01-11 17:55:24 +01:00
committed by GitHub
parent 20bdcc7fff
commit dc58bc375a
3 changed files with 64 additions and 22 deletions

View File

@@ -77,7 +77,7 @@ async def test_check_loop_async():
async def test_check_loop_async_integration(caplog):
"""Test check_loop detects when called from event loop from integration context."""
"""Test check_loop detects and raises when called from event loop from integration context."""
with pytest.raises(RuntimeError), patch(
"homeassistant.util.async_.extract_stack",
return_value=[
@@ -100,7 +100,40 @@ async def test_check_loop_async_integration(caplog):
):
hasync.check_loop()
assert (
"Detected I/O inside the event loop. This is causing stability issues. Please report issue for hue doing I/O at homeassistant/components/hue/light.py, line 23: self.light.is_on"
"Detected blocking call inside the event loop. This is causing stability issues. "
"Please report issue for hue doing blocking calls at "
"homeassistant/components/hue/light.py, line 23: self.light.is_on"
in caplog.text
)
async def test_check_loop_async_integration_non_strict(caplog):
"""Test check_loop detects when called from event loop from integration context."""
with patch(
"homeassistant.util.async_.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/core.py",
lineno="23",
line="do_something()",
),
Mock(
filename="/home/paulus/homeassistant/components/hue/light.py",
lineno="23",
line="self.light.is_on",
),
Mock(
filename="/home/paulus/aiohue/lights.py",
lineno="2",
line="something()",
),
],
):
hasync.check_loop(strict=False)
assert (
"Detected blocking call inside the event loop. This is causing stability issues. "
"Please report issue for hue doing blocking calls at "
"homeassistant/components/hue/light.py, line 23: self.light.is_on"
in caplog.text
)
@@ -129,24 +162,25 @@ async def test_check_loop_async_custom(caplog):
):
hasync.check_loop()
assert (
"Detected I/O inside the event loop. This is causing stability issues. Please report issue to the custom component author for hue doing I/O at custom_components/hue/light.py, line 23: self.light.is_on"
in caplog.text
"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 "
"at custom_components/hue/light.py, line 23: self.light.is_on" in caplog.text
)
def test_check_loop_sync(caplog):
"""Test check_loop does nothing when called from thread."""
hasync.check_loop()
assert "Detected I/O inside the event loop" not in caplog.text
assert "Detected blocking call inside the event loop" not in caplog.text
def test_protect_loop_sync():
"""Test protect_loop calls check_loop."""
calls = []
with patch("homeassistant.util.async_.check_loop") as mock_loop:
hasync.protect_loop(calls.append)(1)
assert len(mock_loop.mock_calls) == 1
assert calls == [1]
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)
func.assert_called_once_with(1, test=2)
async def test_gather_with_concurrency():