Resolve auth_store loading race condition (#21794)

* Add lock in auth_store._async_load()

* Python 3.5 does not like assert_called_once()
This commit is contained in:
Jason Hu 2019-03-08 14:50:24 -08:00 committed by Paulus Schoutsen
parent 3ff2d99cd6
commit 3d8673dbf8
5 changed files with 80 additions and 11 deletions

View file

@ -2,6 +2,7 @@
import asyncio
from unittest.mock import patch
import asynctest
import pytest
from homeassistant.core import valid_entity_id
@ -19,7 +20,6 @@ def registry(hass):
return mock_registry(hass)
@asyncio.coroutine
def test_get_or_create_returns_same_entry(registry):
"""Make sure we do not duplicate entries."""
entry = registry.async_get_or_create('light', 'hue', '1234')
@ -30,7 +30,6 @@ def test_get_or_create_returns_same_entry(registry):
assert entry.entity_id == 'light.hue_1234'
@asyncio.coroutine
def test_get_or_create_suggested_object_id(registry):
"""Test that suggested_object_id works."""
entry = registry.async_get_or_create(
@ -39,7 +38,6 @@ def test_get_or_create_suggested_object_id(registry):
assert entry.entity_id == 'light.beer'
@asyncio.coroutine
def test_get_or_create_suggested_object_id_conflict_register(registry):
"""Test that we don't generate an entity id that is already registered."""
entry = registry.async_get_or_create(
@ -51,7 +49,6 @@ def test_get_or_create_suggested_object_id_conflict_register(registry):
assert entry2.entity_id == 'light.beer_2'
@asyncio.coroutine
def test_get_or_create_suggested_object_id_conflict_existing(hass, registry):
"""Test that we don't generate an entity id that currently exists."""
hass.states.async_set('light.hue_1234', 'on')
@ -59,7 +56,6 @@ def test_get_or_create_suggested_object_id_conflict_existing(hass, registry):
assert entry.entity_id == 'light.hue_1234_2'
@asyncio.coroutine
def test_create_triggers_save(hass, registry):
"""Test that registering entry triggers a save."""
with patch.object(registry, 'async_schedule_save') as mock_schedule_save:
@ -91,7 +87,6 @@ async def test_loading_saving_data(hass, registry):
assert orig_entry2 == new_entry2
@asyncio.coroutine
def test_generate_entity_considers_registered_entities(registry):
"""Test that we don't create entity id that are already registered."""
entry = registry.async_get_or_create('light', 'hue', '1234')
@ -100,7 +95,6 @@ def test_generate_entity_considers_registered_entities(registry):
'light.hue_1234_2'
@asyncio.coroutine
def test_generate_entity_considers_existing_entities(hass, registry):
"""Test that we don't create entity id that currently exists."""
hass.states.async_set('light.kitchen', 'on')
@ -108,7 +102,6 @@ def test_generate_entity_considers_existing_entities(hass, registry):
'light.kitchen_2'
@asyncio.coroutine
def test_is_registered(registry):
"""Test that is_registered works."""
entry = registry.async_get_or_create('light', 'hue', '1234')
@ -166,7 +159,6 @@ async def test_loading_extra_values(hass, hass_storage):
assert entry_disabled_user.disabled_by == entity_registry.DISABLED_USER
@asyncio.coroutine
def test_async_get_entity_id(registry):
"""Test that entity_id is returned."""
entry = registry.async_get_or_create('light', 'hue', '1234')
@ -176,7 +168,7 @@ def test_async_get_entity_id(registry):
assert registry.async_get_entity_id('light', 'hue', '123') is None
async def test_updating_config_entry_id(registry):
def test_updating_config_entry_id(registry):
"""Test that we update config entry id in registry."""
entry = registry.async_get_or_create(
'light', 'hue', '5678', config_entry_id='mock-id-1')
@ -186,7 +178,7 @@ async def test_updating_config_entry_id(registry):
assert entry2.config_entry_id == 'mock-id-2'
async def test_removing_config_entry_id(registry):
def test_removing_config_entry_id(registry):
"""Test that we update config entry id in registry."""
entry = registry.async_get_or_create(
'light', 'hue', '5678', config_entry_id='mock-id-1')
@ -265,3 +257,17 @@ async def test_loading_invalid_entity_id(hass, hass_storage):
'test', 'super_platform', 'id-invalid-start')
assert valid_entity_id(entity_invalid_start.entity_id)
async def test_loading_race_condition(hass):
"""Test only one storage load called when concurrent loading occurred ."""
with asynctest.patch(
'homeassistant.helpers.entity_registry.EntityRegistry.async_load',
) as mock_load:
results = await asyncio.gather(
entity_registry.async_get_registry(hass),
entity_registry.async_get_registry(hass),
)
mock_load.assert_called_once_with()
assert results[0] == results[1]