Lazy initialise the worker pool (#4110)

* Lazy initialise the worker pool

* Minimize pool initialization in core tests

* Fix tests on Python 3.4

* Remove passing in thread count to mock HASS

* Tests: Allow pool by default for threaded, disable for async

* Remove JobPriority for thread pool

* Fix wrong block_till_done

* EmulatedHue: Remove unused test code

* Zigbee: do not touch hass.pool

* Init loop in add_job

* Fix core test

* Fix random sensor test
This commit is contained in:
Paulus Schoutsen 2016-10-31 08:47:29 -07:00 committed by GitHub
parent a1e910f1cf
commit 7f699b4261
26 changed files with 140 additions and 185 deletions

View file

@ -56,7 +56,7 @@ def test_async_add_job_add_threaded_job_to_pool(mock_iscoro):
ha.HomeAssistant.async_add_job(hass, job)
assert len(hass.loop.call_soon.mock_calls) == 0
assert len(hass.loop.create_task.mock_calls) == 0
assert len(hass.add_job.mock_calls) == 1
assert len(hass.pool.add_job.mock_calls) == 1
def test_async_run_job_calls_callback():
@ -91,7 +91,7 @@ class TestHomeAssistant(unittest.TestCase):
# pylint: disable=invalid-name
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant(0)
self.hass = get_test_home_assistant()
# pylint: disable=invalid-name
def tearDown(self):
@ -169,7 +169,6 @@ class TestEventBus(unittest.TestCase):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.bus = self.hass.bus
self.bus.listen('test_event', lambda x: len)
# pylint: disable=invalid-name
def tearDown(self):
@ -178,6 +177,7 @@ class TestEventBus(unittest.TestCase):
def test_add_remove_listener(self):
"""Test remove_listener method."""
self.hass.allow_pool = False
old_count = len(self.bus.listeners)
def listener(_): pass
@ -195,8 +195,10 @@ class TestEventBus(unittest.TestCase):
def test_unsubscribe_listener(self):
"""Test unsubscribe listener from returned function."""
self.hass.allow_pool = False
calls = []
@ha.callback
def listener(event):
"""Mock listener."""
calls.append(event)
@ -217,6 +219,7 @@ class TestEventBus(unittest.TestCase):
def test_listen_once_event_with_callback(self):
"""Test listen_once_event method."""
self.hass.allow_pool = False
runs = []
@ha.callback
@ -234,6 +237,7 @@ class TestEventBus(unittest.TestCase):
def test_listen_once_event_with_coroutine(self):
"""Test listen_once_event method."""
self.hass.allow_pool = False
runs = []
@asyncio.coroutine
@ -279,6 +283,7 @@ class TestEventBus(unittest.TestCase):
def test_callback_event_listener(self):
"""Test a event listener listeners."""
self.hass.allow_pool = False
callback_calls = []
@ha.callback
@ -292,6 +297,7 @@ class TestEventBus(unittest.TestCase):
def test_coroutine_event_listener(self):
"""Test a event listener listeners."""
self.hass.allow_pool = False
coroutine_calls = []
@asyncio.coroutine
@ -366,10 +372,11 @@ class TestStateMachine(unittest.TestCase):
# pylint: disable=invalid-name
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant(0)
self.hass = get_test_home_assistant()
self.states = self.hass.states
self.states.set("light.Bowl", "on")
self.states.set("switch.AC", "off")
self.hass.allow_pool = False
# pylint: disable=invalid-name
def tearDown(self):
@ -413,8 +420,12 @@ class TestStateMachine(unittest.TestCase):
def test_remove(self):
"""Test remove method."""
events = []
self.hass.bus.listen(EVENT_STATE_CHANGED,
lambda event: events.append(event))
@ha.callback
def callback(event):
events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
self.assertIn('light.bowl', self.states.entity_ids())
self.assertTrue(self.states.remove('light.bowl'))
@ -436,8 +447,11 @@ class TestStateMachine(unittest.TestCase):
"""Test insensitivty."""
runs = []
self.hass.bus.listen(EVENT_STATE_CHANGED,
lambda event: runs.append(event))
@ha.callback
def callback(event):
runs.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
self.states.set('light.BOWL', 'off')
self.hass.block_till_done()
@ -462,7 +476,12 @@ class TestStateMachine(unittest.TestCase):
def test_force_update(self):
"""Test force update option."""
events = []
self.hass.bus.listen(EVENT_STATE_CHANGED, lambda ev: events.append(ev))
@ha.callback
def callback(event):
events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
self.states.set('light.bowl', 'on')
self.hass.block_till_done()
@ -504,6 +523,7 @@ class TestServiceRegistry(unittest.TestCase):
def test_has_service(self):
"""Test has_service method."""
self.hass.allow_pool = False
self.assertTrue(
self.services.has_service("tesT_domaiN", "tesT_servicE"))
self.assertFalse(
@ -513,6 +533,7 @@ class TestServiceRegistry(unittest.TestCase):
def test_services(self):
"""Test services."""
self.hass.allow_pool = False
expected = {
'test_domain': {'test_service': {'description': '', 'fields': {}}}
}
@ -535,6 +556,7 @@ class TestServiceRegistry(unittest.TestCase):
def test_call_non_existing_with_blocking(self):
"""Test non-existing with blocking."""
self.hass.allow_pool = False
prior = ha.SERVICE_CALL_LIMIT
try:
ha.SERVICE_CALL_LIMIT = 0.01
@ -545,6 +567,7 @@ class TestServiceRegistry(unittest.TestCase):
def test_async_service(self):
"""Test registering and calling an async service."""
self.hass.allow_pool = False
calls = []
@asyncio.coroutine
@ -561,6 +584,7 @@ class TestServiceRegistry(unittest.TestCase):
def test_callback_service(self):
"""Test registering and calling an async service."""
self.hass.allow_pool = False
calls = []
@ha.callback
@ -629,8 +653,9 @@ class TestWorkerPool(unittest.TestCase):
def register_call(_):
calls.append(1)
pool.add_job(ha.JobPriority.EVENT_DEFAULT, (malicious_job, None))
pool.add_job(ha.JobPriority.EVENT_DEFAULT, (register_call, None))
pool.add_job((malicious_job, None))
pool.block_till_done()
pool.add_job((register_call, None))
pool.block_till_done()
self.assertEqual(1, len(calls))