2016-09-13 02:16:14 +00:00
|
|
|
"""Tests for async util methods from Python source."""
|
2024-03-08 18:16:38 +00:00
|
|
|
|
2020-11-11 07:34:54 +00:00
|
|
|
import asyncio
|
|
|
|
import time
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2020-11-11 07:34:54 +00:00
|
|
|
|
2016-10-02 22:07:23 +00:00
|
|
|
import pytest
|
2016-09-13 02:16:14 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-03-11 17:01:12 +00:00
|
|
|
from homeassistant.util import async_ as hasync
|
2016-09-13 02:16:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("concurrent.futures.Future")
|
|
|
|
@patch("threading.get_ident")
|
2023-02-20 10:42:56 +00:00
|
|
|
def test_run_callback_threadsafe_from_inside_event_loop(mock_ident, _) -> None:
|
2016-10-02 22:07:23 +00:00
|
|
|
"""Testing calling run_callback_threadsafe from inside an event loop."""
|
|
|
|
callback = MagicMock()
|
2021-02-01 09:54:39 +00:00
|
|
|
|
|
|
|
loop = Mock(spec=["call_soon_threadsafe"])
|
2016-10-02 22:07:23 +00:00
|
|
|
|
|
|
|
loop._thread_ident = None
|
|
|
|
mock_ident.return_value = 5
|
|
|
|
hasync.run_callback_threadsafe(loop, callback)
|
|
|
|
assert len(loop.call_soon_threadsafe.mock_calls) == 1
|
|
|
|
|
|
|
|
loop._thread_ident = 5
|
|
|
|
mock_ident.return_value = 5
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
hasync.run_callback_threadsafe(loop, callback)
|
|
|
|
assert len(loop.call_soon_threadsafe.mock_calls) == 1
|
|
|
|
|
|
|
|
loop._thread_ident = 1
|
|
|
|
mock_ident.return_value = 5
|
|
|
|
hasync.run_callback_threadsafe(loop, callback)
|
|
|
|
assert len(loop.call_soon_threadsafe.mock_calls) == 2
|
|
|
|
|
|
|
|
|
2023-10-19 13:08:52 +00:00
|
|
|
async def test_gather_with_limited_concurrency() -> None:
|
|
|
|
"""Test gather_with_limited_concurrency limits the number of running tasks."""
|
2020-11-11 07:34:54 +00:00
|
|
|
|
|
|
|
runs = 0
|
|
|
|
now_time = time.time()
|
|
|
|
|
|
|
|
async def _increment_runs_if_in_time():
|
|
|
|
if time.time() - now_time > 0.1:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
nonlocal runs
|
|
|
|
runs += 1
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
return runs
|
|
|
|
|
2023-10-19 13:08:52 +00:00
|
|
|
results = await hasync.gather_with_limited_concurrency(
|
2021-07-19 08:46:09 +00:00
|
|
|
2, *(_increment_runs_if_in_time() for i in range(4))
|
2020-11-11 07:34:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert results == [2, 2, -1, -1]
|
2021-02-01 09:54:39 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_shutdown_run_callback_threadsafe(hass: HomeAssistant) -> None:
|
2021-02-01 09:54:39 +00:00
|
|
|
"""Test we can shutdown run_callback_threadsafe."""
|
|
|
|
hasync.shutdown_run_callback_threadsafe(hass.loop)
|
|
|
|
callback = MagicMock()
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
hasync.run_callback_threadsafe(hass.loop, callback)
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_run_callback_threadsafe(hass: HomeAssistant) -> None:
|
2021-02-01 09:54:39 +00:00
|
|
|
"""Test run_callback_threadsafe runs code in the event loop."""
|
|
|
|
it_ran = False
|
|
|
|
|
|
|
|
def callback():
|
|
|
|
nonlocal it_ran
|
|
|
|
it_ran = True
|
|
|
|
|
|
|
|
assert hasync.run_callback_threadsafe(hass.loop, callback)
|
|
|
|
assert it_ran is False
|
|
|
|
|
|
|
|
# Verify that async_block_till_done will flush
|
|
|
|
# out the callback
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert it_ran is True
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_callback_is_always_scheduled(hass: HomeAssistant) -> None:
|
2021-02-01 09:54:39 +00:00
|
|
|
"""Test run_callback_threadsafe always calls call_soon_threadsafe before checking for shutdown."""
|
|
|
|
# We have to check the shutdown state AFTER the callback is scheduled otherwise
|
|
|
|
# the function could continue on and the caller call `future.result()` after
|
|
|
|
# the point in the main thread where callbacks are no longer run.
|
|
|
|
|
|
|
|
callback = MagicMock()
|
|
|
|
hasync.shutdown_run_callback_threadsafe(hass.loop)
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch.object(hass.loop, "call_soon_threadsafe") as mock_call_soon_threadsafe,
|
|
|
|
pytest.raises(RuntimeError),
|
|
|
|
):
|
2021-03-27 08:17:15 +00:00
|
|
|
hasync.run_callback_threadsafe(hass.loop, callback)
|
2021-02-01 09:54:39 +00:00
|
|
|
|
|
|
|
mock_call_soon_threadsafe.assert_called_once()
|
2024-02-26 16:36:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_create_eager_task_312(hass: HomeAssistant) -> None:
|
|
|
|
"""Test create_eager_task schedules a task eagerly in the event loop.
|
|
|
|
|
|
|
|
For Python 3.12+, the task is scheduled eagerly in the event loop.
|
|
|
|
"""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
async def _normal_task():
|
|
|
|
events.append("normal")
|
|
|
|
|
|
|
|
async def _eager_task():
|
|
|
|
events.append("eager")
|
|
|
|
|
|
|
|
task1 = hasync.create_eager_task(_eager_task())
|
|
|
|
task2 = asyncio.create_task(_normal_task())
|
|
|
|
|
|
|
|
assert events == ["eager"]
|
|
|
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
assert events == ["eager", "normal"]
|
|
|
|
await task1
|
|
|
|
await task2
|