Better locking while setting up components + discovery (#4463)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2016-11-19 08:18:33 -08:00
1 parent f3748ce535
commit d930c399fe
3 files changed
+116 -29

No files matched your search

+51 -5
View File
@@ -1,7 +1,9 @@
"""Test discovery helpers."""
from collections import OrderedDict
from unittest.mock import patch
from homeassistant import loader, bootstrap
from homeassistant.core import callback
from homeassistant.helpers import discovery
from homeassistant.util.async import run_coroutine_threadsafe
@@ -20,16 +22,18 @@ class TestHelpersDiscovery:
"""Stop everything that was started."""
self.hass.stop()
@patch('homeassistant.bootstrap.setup_component')
@patch('homeassistant.bootstrap.async_setup_component')
def test_listen(self, mock_setup_component):
"""Test discovery listen/discover combo."""
calls_single = []
calls_multi = []
@callback
def callback_single(service, info):
"""Service discovered callback."""
calls_single.append((service, info))
@callback
def callback_multi(service, info):
"""Service discovered callback."""
calls_multi.append((service, info))
@@ -42,16 +46,16 @@ class TestHelpersDiscovery:
'test_component')
self.hass.block_till_done()
discovery.discover(self.hass, 'another service', 'discovery info',
'test_component')
self.hass.block_till_done()
assert mock_setup_component.called
assert mock_setup_component.call_args[0] == \
(self.hass, 'test_component', None)
assert len(calls_single) == 1
assert calls_single[0] == ('test service', 'discovery info')
discovery.discover(self.hass, 'another service', 'discovery info',
'test_component')
self.hass.block_till_done()
assert len(calls_single) == 1
assert len(calls_multi) == 2
assert ['test service', 'another service'] == [info[0] for info
@@ -63,6 +67,7 @@ class TestHelpersDiscovery:
"""Test discover platform method."""
calls = []
@callback
def platform_callback(platform, info):
"""Platform callback method."""
calls.append((platform, info))
@@ -149,3 +154,44 @@ class TestHelpersDiscovery:
assert len(platform_calls) == 2
assert 'test_component' in self.hass.config.components
assert 'switch' in self.hass.config.components
def test_1st_discovers_2nd_component(self):
"""Test that we don't break if one component discovers the other.
If the first component fires a discovery event to setup the
second component while the second component is about to be setup,
it should not setup the second component twice.
"""
component_calls = []
def component1_setup(hass, config):
"""Setup mock component."""
discovery.discover(hass, 'test_component2')
return True
def component2_setup(hass, config):
"""Setup mock component."""
component_calls.append(1)
return True
loader.set_component(
'test_component1',
MockModule('test_component1', setup=component1_setup))
loader.set_component(
'test_component2',
MockModule('test_component2', setup=component2_setup))
config = OrderedDict()
config['test_component1'] = {}
config['test_component2'] = {}
self.hass.loop.run_until_complete = \
lambda _: self.hass.block_till_done()
bootstrap.from_config_dict(config, self.hass)
self.hass.block_till_done()
# test_component will only be setup once
assert len(component_calls) == 1