From 5c43f0861fcb7268b29ca46bceeeeab684fce482 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Wed, 28 Dec 2022 23:40:11 +0100 Subject: [PATCH] Avoid running final writes in executor in test (#84679) --- homeassistant/helpers/storage.py | 7 ++++--- tests/common.py | 8 ++++---- tests/components/motioneye/test_camera.py | 1 + tests/components/uptimerobot/test_init.py | 1 + 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 44a0da7866bc..bb7b5c850c5f 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -276,12 +276,13 @@ class Store(Generic[_T]): self._data = None try: - await self.hass.async_add_executor_job( - self._write_data, self.path, data - ) + await self._async_write_data(self.path, data) except (json_util.SerializationError, json_util.WriteError) as err: _LOGGER.error("Error writing config for %s: %s", self.key, err) + async def _async_write_data(self, path: str, data: dict) -> None: + await self.hass.async_add_executor_job(self._write_data, self.path, data) + def _write_data(self, path: str, data: dict) -> None: """Write the data.""" os.makedirs(os.path.dirname(path), exist_ok=True) diff --git a/tests/common.py b/tests/common.py index 69c569e445f9..8324abc730d0 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1165,13 +1165,13 @@ def mock_storage(data=None): # Route through original load so that we trigger migration loaded = await orig_load(store) - _LOGGER.info("Loading data for %s: %s", store.key, loaded) + _LOGGER.debug("Loading data for %s: %s", store.key, loaded) return loaded - def mock_write_data(store, path, data_to_write): + async def mock_write_data(store, path, data_to_write): """Mock version of write data.""" # To ensure that the data can be serialized - _LOGGER.info("Writing data to %s: %s", store.key, data_to_write) + _LOGGER.debug("Writing data to %s: %s", store.key, data_to_write) raise_contains_mocks(data_to_write) data[store.key] = json.loads(json.dumps(data_to_write, cls=store._encoder)) @@ -1184,7 +1184,7 @@ def mock_storage(data=None): side_effect=mock_async_load, autospec=True, ), patch( - "homeassistant.helpers.storage.Store._write_data", + "homeassistant.helpers.storage.Store._async_write_data", side_effect=mock_write_data, autospec=True, ), patch( diff --git a/tests/components/motioneye/test_camera.py b/tests/components/motioneye/test_camera.py index d70405cd297a..8cc64b8ff006 100644 --- a/tests/components/motioneye/test_camera.py +++ b/tests/components/motioneye/test_camera.py @@ -164,6 +164,7 @@ async def test_setup_camera_new_data_camera_removed(hass: HomeAssistant) -> None client.async_get_cameras = AsyncMock(return_value={KEY_CAMERAS: []}) async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() + await hass.async_block_till_done() assert not hass.states.get(TEST_CAMERA_ENTITY_ID) assert not device_registry.async_get_device({TEST_CAMERA_DEVICE_IDENTIFIER}) assert not device_registry.async_get_device({(DOMAIN, old_device_id)}) diff --git a/tests/components/uptimerobot/test_init.py b/tests/components/uptimerobot/test_init.py index 00e7f5c27e04..00d9b1c6a851 100644 --- a/tests/components/uptimerobot/test_init.py +++ b/tests/components/uptimerobot/test_init.py @@ -220,6 +220,7 @@ async def test_device_management(hass: HomeAssistant): ): async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL) await hass.async_block_till_done() + await hass.async_block_till_done() devices = dr.async_entries_for_config_entry(dev_reg, mock_entry.entry_id) assert len(devices) == 1