Improve reliablity of recorder purge test (#92641)

pull/92713/head
J. Nick Koston 2023-05-06 18:28:43 -05:00 committed by GitHub
parent 7cb25e9710
commit ce3cb5864a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 29 deletions

View File

@ -38,7 +38,6 @@ from homeassistant.const import EVENT_STATE_CHANGED, EVENT_THEMES_UPDATED, STATE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import dt as dt_util
from homeassistant.util.json import json_loads
from .common import (
async_recorder_block_till_done,
@ -1452,6 +1451,11 @@ async def _add_test_events(hass: HomeAssistant, iterations: int = 1):
five_days_ago = utcnow - timedelta(days=5)
eleven_days_ago = utcnow - timedelta(days=11)
event_data = {"test_attr": 5, "test_attr_10": "nice"}
# Make sure recording is done before freezing time
# because the time freeze can affect the recorder
# thread as well can cause the test to fail
await async_wait_recording_done(hass)
for _ in range(iterations):
for event_id in range(6):
if event_id < 2:
@ -1469,34 +1473,6 @@ async def _add_test_events(hass: HomeAssistant, iterations: int = 1):
await async_wait_recording_done(hass)
async def _add_events_with_event_data(hass: HomeAssistant, iterations: int = 1):
"""Add a few events with linked event_data for testing."""
utcnow = dt_util.utcnow()
five_days_ago = utcnow - timedelta(days=5)
eleven_days_ago = utcnow - timedelta(days=11)
await hass.async_block_till_done()
for _ in range(iterations):
for event_id in range(6):
if event_id < 2:
timestamp = eleven_days_ago
event_type = "EVENT_TEST_AUTOPURGE_WITH_EVENT_DATA"
shared_data = '{"type":{"EVENT_TEST_AUTOPURGE_WITH_EVENT_DATA"}'
elif event_id < 4:
timestamp = five_days_ago
event_type = "EVENT_TEST_PURGE_WITH_EVENT_DATA"
shared_data = '{"type":{"EVENT_TEST_PURGE_WITH_EVENT_DATA"}'
else:
timestamp = utcnow
event_type = "EVENT_TEST_WITH_EVENT_DATA"
shared_data = '{"type":{"EVENT_TEST_WITH_EVENT_DATA"}'
with freeze_time(timestamp):
hass.bus.async_fire(event_type, json_loads(shared_data))
await async_wait_recording_done(hass)
async def _add_test_statistics(hass: HomeAssistant):
"""Add multiple statistics to the db for testing."""
utcnow = dt_util.utcnow()