core/tests/util/test_executor.py

95 lines
2.6 KiB
Python
Raw Normal View History

"""Test Home Assistant executor util."""
import concurrent.futures
import time
from unittest.mock import patch
import pytest
from homeassistant.util import executor
from homeassistant.util.executor import InterruptibleThreadPoolExecutor
2023-02-20 10:42:56 +00:00
async def test_executor_shutdown_can_interrupt_threads(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the executor shutdown can interrupt threads."""
iexecutor = InterruptibleThreadPoolExecutor()
def _loop_sleep_in_executor():
while True:
time.sleep(0.1)
sleep_futures = [iexecutor.submit(_loop_sleep_in_executor) for _ in range(100)]
iexecutor.shutdown()
for future in sleep_futures:
with pytest.raises((concurrent.futures.CancelledError, SystemExit)):
future.result()
assert "is still running at shutdown" in caplog.text
assert "time.sleep(0.1)" in caplog.text
2023-02-20 10:42:56 +00:00
async def test_executor_shutdown_only_logs_max_attempts(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the executor shutdown will only log max attempts."""
iexecutor = InterruptibleThreadPoolExecutor()
def _loop_sleep_in_executor():
time.sleep(0.2)
iexecutor.submit(_loop_sleep_in_executor)
with patch.object(executor, "EXECUTOR_SHUTDOWN_TIMEOUT", 0.3):
iexecutor.shutdown()
assert "time.sleep(0.2)" in caplog.text
Add python 3.11 to the CI (#88038) * Remove profiler.memory service guppy3 is not python3.11 compat https://github.com/zhuyifei1999/guppy3/issues/41 This service will return if and when guppy3 becomes python3.11 compat * squash * temp remove * temp dump tests * temp dump tests * drop a few more to get a run * drop a few more to get a run * Account for changed python3.11 enum.IntFlag behavior in zha There may be additional changes needed, but I could only see what needed to be updated based on the tests * merge * restore * restore * legacy value * tweak a bit for the python 3.11 timings * block cchardet * conditional * adjust est * test * not yet * tweak * give a little leeway for timing * Fix otbr tests * Increase database test timeout It looks like we need a little more time to run with the addiitonal tests in #87019 * Increase database test timeout It looks like we need a little more time to run with the addiitonal tests in #87019 * Fix aprs tests with python 3.11 * merge fix * hints * Update homeassistant/package_constraints.txt * Update script/gen_requirements_all.py * Constrain uamqp for Python 3.10 only * Bump vulcan-api to 2.3.0 see https://github.com/kapi2289/vulcan-api/pull/126 see https://github.com/home-assistant/core/pull/88038 see https://github.com/home-assistant/docker/pull/260 * add ban * Bump python-matter-server to 2.1.1 * revert * Update tests/asyncio_legacy.py --------- Co-authored-by: Erik <erik@montnemery.com> Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Marcel van der Veldt <m.vanderveldt@outlook.com>
2023-02-16 17:38:32 +00:00
assert "is still running at shutdown" in caplog.text
iexecutor.shutdown()
2023-02-20 10:42:56 +00:00
async def test_executor_shutdown_does_not_log_shutdown_on_first_attempt(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the executor shutdown does not log on first attempt."""
iexecutor = InterruptibleThreadPoolExecutor()
def _do_nothing():
return
for _ in range(5):
iexecutor.submit(_do_nothing)
iexecutor.shutdown()
assert "is still running at shutdown" not in caplog.text
2023-02-20 10:42:56 +00:00
async def test_overall_timeout_reached(caplog: pytest.LogCaptureFixture) -> None:
"""Test that shutdown moves on when the overall timeout is reached."""
def _loop_sleep_in_executor():
time.sleep(1)
with patch.object(executor, "EXECUTOR_SHUTDOWN_TIMEOUT", 0.5):
iexecutor = InterruptibleThreadPoolExecutor()
for _ in range(6):
iexecutor.submit(_loop_sleep_in_executor)
start = time.monotonic()
iexecutor.shutdown()
finish = time.monotonic()
2024-05-24 06:22:29 +00:00
# Ideally execution time (finish - start) should be < 1.2 sec.
# CI tests might not run in an ideal environment and timing might
# not be accurate, so we let this test pass
# if the duration is below 3 seconds.
assert finish - start < 3.0
iexecutor.shutdown()