Fix flaky vizio test and add comments to explain logic (#50948)

This commit is contained in:
Raman Gupta 2021-05-22 02:57:30 -04:00 committed by GitHub
parent 78be237447
commit 92d1871de5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -113,6 +113,9 @@ class VizioAppsDataUpdateCoordinator(DataUpdateCoordinator):
"""Update data via library."""
data = await gen_apps_list_from_url(session=async_get_clientsession(self.hass))
if not data:
# For every failure, increase the fail count until we reach the threshold.
# We then log a warning, increase the threshold, and reset the fail count.
# This is here to prevent silent failures but to reduce repeat logs.
if self.fail_count == self.fail_threshold:
_LOGGER.warning(
(
@ -126,6 +129,7 @@ class VizioAppsDataUpdateCoordinator(DataUpdateCoordinator):
else:
self.fail_count += 1
return self.data
# Reset the fail count and threshold when the data is successfully retrieved
self.fail_count = 0
self.fail_threshold = 10
return sorted(data, key=lambda app: app["name"])

View File

@ -94,14 +94,11 @@ async def test_coordinator_update_failure(
assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 1
assert DOMAIN in hass.data
for days in range(1, 10):
# Failing 25 days in a row should result in a single log message
# (first one after 10 days, next one would be at 30 days)
for days in range(1, 25):
async_fire_time_changed(hass, now + timedelta(days=days))
await hass.async_block_till_done()
assert (
"Unable to retrieve the apps list from the external server"
not in caplog.text
)
async_fire_time_changed(hass, now + timedelta(days=10))
await hass.async_block_till_done()
assert "Unable to retrieve the apps list from the external server" in caplog.text
err_msg = "Unable to retrieve the apps list from the external server"
assert len([record for record in caplog.records if err_msg in record.msg]) == 1