Remove ThreadPool with async executor (#4154)
* Remove ThreadPool with async executor * Fix zigbee * update unittest * fix remote api * add pending task to remote * fix lint * remove unused import * remove old stuff for lazy tests * fix bug and add a exception handler to executor * change executor handling * change to wait from gather * fix unittest
This commit is contained in:
1 parent
b67f1fed52
commit
ece58ce78f
10 files changed
+37
-346
No files matched your search
@@ -40,7 +40,6 @@ def get_test_home_assistant():
|
||||
loop = asyncio.new_event_loop()
|
||||
|
||||
hass = loop.run_until_complete(async_test_home_assistant(loop))
|
||||
hass.allow_pool = True
|
||||
|
||||
# FIXME should not be a daemon. Means hass.stop() not called in teardown
|
||||
stop_event = threading.Event()
|
||||
@@ -97,8 +96,6 @@ def async_test_home_assistant(loop):
|
||||
|
||||
hass.state = ha.CoreState.running
|
||||
|
||||
hass.allow_pool = False
|
||||
|
||||
# Mock async_start
|
||||
orig_start = hass.async_start
|
||||
|
||||
@@ -110,20 +107,6 @@ def async_test_home_assistant(loop):
|
||||
|
||||
hass.async_start = mock_async_start
|
||||
|
||||
# Mock async_init_pool
|
||||
orig_init = hass.async_init_pool
|
||||
|
||||
@ha.callback
|
||||
def mock_async_init_pool():
|
||||
"""Prevent worker pool from being initialized."""
|
||||
if hass.allow_pool:
|
||||
with patch('homeassistant.core._async_monitor_worker_pool'):
|
||||
orig_init()
|
||||
else:
|
||||
assert False, 'Thread pool not allowed. Set hass.allow_pool = True'
|
||||
|
||||
hass.async_init_pool = mock_async_init_pool
|
||||
|
||||
return hass
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ from homeassistant.bootstrap import setup_component
|
||||
@asyncio.coroutine
|
||||
def test_fetching_url(aioclient_mock, hass, test_client):
|
||||
"""Test that it fetches the given url."""
|
||||
hass.allow_pool = True
|
||||
aioclient_mock.get('http://example.com', text='hello world')
|
||||
|
||||
def setup_platform():
|
||||
@@ -40,7 +39,6 @@ def test_fetching_url(aioclient_mock, hass, test_client):
|
||||
@asyncio.coroutine
|
||||
def test_limit_refetch(aioclient_mock, hass, test_client):
|
||||
"""Test that it fetches the given url."""
|
||||
hass.allow_pool = True
|
||||
aioclient_mock.get('http://example.com/5a', text='hello world')
|
||||
aioclient_mock.get('http://example.com/10a', text='hello world')
|
||||
aioclient_mock.get('http://example.com/15a', text='hello planet')
|
||||
|
||||
@@ -14,8 +14,6 @@ from tests.common import assert_setup_component, mock_http_component
|
||||
@asyncio.coroutine
|
||||
def test_loading_file(hass, test_client):
|
||||
"""Test that it loads image from disk."""
|
||||
hass.allow_pool = True
|
||||
|
||||
@mock.patch('os.path.isfile', mock.Mock(return_value=True))
|
||||
@mock.patch('os.access', mock.Mock(return_value=True))
|
||||
def setup_platform():
|
||||
|
||||
+1
-78
@@ -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.pool.add_job.mock_calls) == 1
|
||||
assert len(hass.loop.run_in_executor.mock_calls) == 1
|
||||
|
||||
|
||||
def test_async_run_job_calls_callback():
|
||||
@@ -195,7 +195,6 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
def test_unsubscribe_listener(self):
|
||||
"""Test unsubscribe listener from returned function."""
|
||||
self.hass.allow_pool = False
|
||||
calls = []
|
||||
|
||||
@ha.callback
|
||||
@@ -219,7 +218,6 @@ 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
|
||||
@@ -237,7 +235,6 @@ 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
|
||||
@@ -283,7 +280,6 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
def test_callback_event_listener(self):
|
||||
"""Test a event listener listeners."""
|
||||
self.hass.allow_pool = False
|
||||
callback_calls = []
|
||||
|
||||
@ha.callback
|
||||
@@ -297,7 +293,6 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
def test_coroutine_event_listener(self):
|
||||
"""Test a event listener listeners."""
|
||||
self.hass.allow_pool = False
|
||||
coroutine_calls = []
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -376,7 +371,6 @@ class TestStateMachine(unittest.TestCase):
|
||||
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):
|
||||
@@ -523,7 +517,6 @@ 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(
|
||||
@@ -533,7 +526,6 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
def test_services(self):
|
||||
"""Test services."""
|
||||
self.hass.allow_pool = False
|
||||
expected = {
|
||||
'test_domain': {'test_service': {'description': '', 'fields': {}}}
|
||||
}
|
||||
@@ -556,7 +548,6 @@ 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
|
||||
@@ -567,7 +558,6 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
def test_async_service(self):
|
||||
"""Test registering and calling an async service."""
|
||||
self.hass.allow_pool = False
|
||||
calls = []
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -584,7 +574,6 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
def test_callback_service(self):
|
||||
"""Test registering and calling an async service."""
|
||||
self.hass.allow_pool = False
|
||||
calls = []
|
||||
|
||||
@ha.callback
|
||||
@@ -638,72 +627,6 @@ class TestConfig(unittest.TestCase):
|
||||
self.assertEqual(expected, self.config.as_dict())
|
||||
|
||||
|
||||
class TestWorkerPool(unittest.TestCase):
|
||||
"""Test WorkerPool methods."""
|
||||
|
||||
def test_exception_during_job(self):
|
||||
"""Test exception during a job."""
|
||||
pool = ha.create_worker_pool(1)
|
||||
|
||||
def malicious_job(_):
|
||||
raise Exception("Test breaking worker pool")
|
||||
|
||||
calls = []
|
||||
|
||||
def register_call(_):
|
||||
calls.append(1)
|
||||
|
||||
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))
|
||||
|
||||
|
||||
class TestWorkerPoolMonitor(object):
|
||||
"""Test monitor_worker_pool."""
|
||||
|
||||
@patch('homeassistant.core._LOGGER.warning')
|
||||
def test_worker_pool_monitor(self, mock_warning, event_loop):
|
||||
"""Test we log an error and increase threshold."""
|
||||
hass = MagicMock()
|
||||
hass.pool.worker_count = 3
|
||||
schedule_handle = MagicMock()
|
||||
hass.loop.call_later.return_value = schedule_handle
|
||||
|
||||
ha._async_monitor_worker_pool(hass)
|
||||
assert hass.loop.call_later.called
|
||||
assert hass.bus.async_listen_once.called
|
||||
assert not schedule_handle.called
|
||||
|
||||
check_threshold = hass.loop.call_later.mock_calls[0][1][1]
|
||||
|
||||
hass.pool.queue_size = 8
|
||||
check_threshold()
|
||||
assert not mock_warning.called
|
||||
|
||||
hass.pool.queue_size = 9
|
||||
check_threshold()
|
||||
assert mock_warning.called
|
||||
|
||||
mock_warning.reset_mock()
|
||||
assert not mock_warning.called
|
||||
|
||||
check_threshold()
|
||||
assert not mock_warning.called
|
||||
|
||||
hass.pool.queue_size = 17
|
||||
check_threshold()
|
||||
assert not mock_warning.called
|
||||
|
||||
hass.pool.queue_size = 18
|
||||
check_threshold()
|
||||
assert mock_warning.called
|
||||
|
||||
hass.bus.async_listen_once.mock_calls[0][1][1](None)
|
||||
assert schedule_handle.cancel.called
|
||||
|
||||
|
||||
class TestAsyncCreateTimer(object):
|
||||
"""Test create timer."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user