Avoid running final writes in executor in test (#84679)

This commit is contained in:
Joakim Plate 2022-12-28 23:40:11 +01:00 committed by GitHub
parent 345081ba15
commit 5c43f0861f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 7 deletions

View file

@ -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)

View file

@ -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(

View file

@ -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)})

View file

@ -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