Protect loop set default executor (#37438)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Paulus SchoutsenandJ. Nick Koston authored and GitHub committed 2020-07-06 15:58:53 -07:00
1 parent f8651d9faa
commit f49ce5d1b4
21 files changed
+387 -244

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ def get_test_home_assistant():
# pylint: disable=protected-access
async def async_test_home_assistant(loop):
"""Return a Home Assistant object pointing at test config dir."""
hass = ha.HomeAssistant(loop)
hass = ha.HomeAssistant()
store = auth_store.AuthStore(hass)
hass.auth = auth.AuthManager(hass, store, {}, {})
ensure_auth_manager_loaded(hass.auth)
+3 -3
View File
@@ -8,8 +8,8 @@ from homeassistant.core import callback as ha_callback
from tests.async_mock import patch
@pytest.fixture(scope="session")
def hk_driver():
@pytest.fixture
def hk_driver(loop):
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
with patch("pyhap.accessory_driver.Zeroconf"), patch(
"pyhap.accessory_driver.AccessoryEncoder"
@@ -18,7 +18,7 @@ def hk_driver():
), patch(
"pyhap.accessory_driver.AccessoryDriver.persist"
):
yield AccessoryDriver(pincode=b"123-45-678", address="127.0.0.1")
yield AccessoryDriver(pincode=b"123-45-678", address="127.0.0.1", loop=loop)
@pytest.fixture
+5
View File
@@ -68,6 +68,11 @@ from tests.components.homekit.common import patch_debounce
IP_ADDRESS = "127.0.0.1"
@pytest.fixture(autouse=True)
def always_patch_driver(hk_driver):
"""Load the hk_driver fixture."""
@pytest.fixture(name="device_reg")
def device_reg_fixture(hass):
"""Return an empty, loaded, registry."""
+1 -1
View File
@@ -215,7 +215,7 @@ def test_density_to_air_quality():
assert density_to_air_quality(300) == 5
async def test_show_setup_msg(hass):
async def test_show_setup_msg(hass, hk_driver):
"""Test show setup message as persistence notification."""
pincode = b"123-45-678"
+6 -1
View File
@@ -1,11 +1,12 @@
"""Set up some common test helper things."""
import asyncio
import functools
import logging
import pytest
import requests_mock as _requests_mock
from homeassistant import core as ha, loader, util
from homeassistant import core as ha, loader, runner, util
from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
from homeassistant.auth.providers import homeassistant, legacy_api_password
from homeassistant.components import mqtt
@@ -40,6 +41,10 @@ from tests.test_util.aiohttp import mock_aiohttp_client # noqa: E402, isort:ski
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False))
# Disable fixtures overriding our beautiful policy
asyncio.set_event_loop_policy = lambda policy: None
def pytest_configure(config):
"""Register marker for tests that log exceptions."""
+74 -58
View File
@@ -7,17 +7,15 @@ from unittest.mock import Mock
import pytest
from homeassistant import bootstrap, core
from homeassistant import bootstrap, core, runner
import homeassistant.config as config_util
from homeassistant.exceptions import HomeAssistantError
import homeassistant.util.dt as dt_util
from tests.async_mock import patch
from tests.common import (
MockConfigEntry,
MockModule,
MockPlatform,
flush_store,
get_test_config_dir,
mock_coro,
mock_entity_platform,
@@ -351,6 +349,7 @@ async def test_setup_hass(
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
caplog,
loop,
):
"""Test it works."""
verbose = Mock()
@@ -365,13 +364,15 @@ async def test_setup_hass(
"homeassistant.components.http.start_http_server_and_save_config"
):
hass = await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
),
)
assert "Waiting on integrations to complete setup" not in caplog.text
@@ -399,6 +400,7 @@ async def test_setup_hass_takes_longer_than_log_slow_startup(
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
caplog,
loop,
):
"""Test it works."""
verbose = Mock()
@@ -420,13 +422,15 @@ async def test_setup_hass_takes_longer_than_log_slow_startup(
"homeassistant.components.http.start_http_server_and_save_config"
):
await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
),
)
assert "Waiting on integrations to complete setup" in caplog.text
@@ -438,19 +442,22 @@ async def test_setup_hass_invalid_yaml(
mock_mount_local_lib_path,
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
loop,
):
"""Test it works."""
with patch(
"homeassistant.config.async_hass_config_yaml", side_effect=HomeAssistantError
), patch("homeassistant.components.http.start_http_server_and_save_config"):
hass = await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
),
)
assert "safe_mode" in hass.config.components
@@ -463,49 +470,52 @@ async def test_setup_hass_config_dir_nonexistent(
mock_mount_local_lib_path,
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
loop,
):
"""Test it works."""
mock_ensure_config_exists.return_value = False
assert (
await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
),
)
is None
)
async def test_setup_hass_safe_mode(
hass,
mock_enable_logging,
mock_is_virtual_env,
mock_mount_local_lib_path,
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
loop,
):
"""Test it works."""
# Add a config entry to storage.
MockConfigEntry(domain="browser").add_to_hass(hass)
hass.config_entries._async_schedule_save()
await flush_store(hass.config_entries._store)
with patch("homeassistant.components.browser.setup") as browser_setup, patch(
"homeassistant.components.http.start_http_server_and_save_config"
), patch(
"homeassistant.config_entries.ConfigEntries.async_domains",
return_value=["browser"],
):
hass = await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=True,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=True,
),
)
assert "safe_mode" in hass.config.components
@@ -522,6 +532,7 @@ async def test_setup_hass_invalid_core_config(
mock_mount_local_lib_path,
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
loop,
):
"""Test it works."""
with patch(
@@ -529,13 +540,15 @@ async def test_setup_hass_invalid_core_config(
return_value={"homeassistant": {"non-existing": 1}},
), patch("homeassistant.components.http.start_http_server_and_save_config"):
hass = await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=False,
log_rotate_days=10,
log_file="",
log_no_color=False,
skip_pip=True,
safe_mode=False,
),
)
assert "safe_mode" in hass.config.components
@@ -547,6 +560,7 @@ async def test_setup_safe_mode_if_no_frontend(
mock_mount_local_lib_path,
mock_ensure_config_exists,
mock_process_ha_config_upgrade,
loop,
):
"""Test we setup safe mode if frontend didn't load."""
verbose = Mock()
@@ -566,13 +580,15 @@ async def test_setup_safe_mode_if_no_frontend(
},
), patch("homeassistant.components.http.start_http_server_and_save_config"):
hass = await bootstrap.async_setup_hass(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
runner.RuntimeConfig(
config_dir=get_test_config_dir(),
verbose=verbose,
log_rotate_days=log_rotate_days,
log_file=log_file,
log_no_color=log_no_color,
skip_pip=True,
safe_mode=False,
),
)
assert "safe_mode" in hass.config.components
+11 -14
View File
@@ -1104,14 +1104,13 @@ def test_timer_out_of_sync(mock_monotonic, loop):
assert abs(target - 14.2) < 0.001
@asyncio.coroutine
def test_hass_start_starts_the_timer(loop):
async def test_hass_start_starts_the_timer(loop):
"""Test when hass starts, it starts the timer."""
hass = ha.HomeAssistant(loop=loop)
hass = ha.HomeAssistant()
try:
with patch("homeassistant.core._async_create_timer") as mock_timer:
yield from hass.async_start()
await hass.async_start()
assert hass.state == ha.CoreState.running
assert not hass._track_task
@@ -1119,21 +1118,20 @@ def test_hass_start_starts_the_timer(loop):
assert mock_timer.mock_calls[0][1][0] is hass
finally:
yield from hass.async_stop()
await hass.async_stop()
assert hass.state == ha.CoreState.not_running
@asyncio.coroutine
def test_start_taking_too_long(loop, caplog):
async def test_start_taking_too_long(loop, caplog):
"""Test when async_start takes too long."""
hass = ha.HomeAssistant(loop=loop)
hass = ha.HomeAssistant()
caplog.set_level(logging.WARNING)
try:
with patch(
"homeassistant.core.timeout", side_effect=asyncio.TimeoutError
), patch("homeassistant.core._async_create_timer") as mock_timer:
yield from hass.async_start()
await hass.async_start()
assert hass.state == ha.CoreState.running
assert len(mock_timer.mock_calls) == 1
@@ -1141,14 +1139,13 @@ def test_start_taking_too_long(loop, caplog):
assert "Something is blocking Home Assistant" in caplog.text
finally:
yield from hass.async_stop()
await hass.async_stop()
assert hass.state == ha.CoreState.not_running
@asyncio.coroutine
def test_track_task_functions(loop):
async def test_track_task_functions(loop):
"""Test function to start/stop track task and initial state."""
hass = ha.HomeAssistant(loop=loop)
hass = ha.HomeAssistant()
try:
assert hass._track_task
@@ -1158,7 +1155,7 @@ def test_track_task_functions(loop):
hass.async_track_tasks()
assert hass._track_task
finally:
yield from hass.async_stop()
await hass.async_stop()
async def test_service_executed_with_subservices(hass):