mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Thread pool tweaks
This commit is contained in:
parent
8d366a7367
commit
68803a46b6
@ -103,12 +103,7 @@ class HomeAssistant(object):
|
||||
def stop(self):
|
||||
"""Stop Home Assistant and shuts down all threads."""
|
||||
_LOGGER.info("Stopping")
|
||||
|
||||
self.bus.fire(EVENT_HOMEASSISTANT_STOP)
|
||||
|
||||
# Wait till all responses to homeassistant_stop are done
|
||||
self.pool.block_till_done()
|
||||
|
||||
self.pool.stop()
|
||||
|
||||
|
||||
|
@ -148,14 +148,11 @@ class HomeAssistant(ha.HomeAssistant):
|
||||
self.bus.fire(ha.EVENT_HOMEASSISTANT_STOP,
|
||||
origin=ha.EventOrigin.remote)
|
||||
|
||||
self.pool.stop()
|
||||
|
||||
# Disconnect master event forwarding
|
||||
disconnect_remote_events(self.remote_api, self.config.api)
|
||||
|
||||
# Wait till all responses to homeassistant_stop are done
|
||||
self.pool.block_till_done()
|
||||
|
||||
self.pool.stop()
|
||||
|
||||
|
||||
class EventBus(ha.EventBus):
|
||||
""" EventBus implementation that forwards fire_event to remote API. """
|
||||
|
@ -284,7 +284,7 @@ class Throttle(object):
|
||||
|
||||
|
||||
class ThreadPool(object):
|
||||
""" A priority queue-based thread pool. """
|
||||
"""A priority queue-based thread pool."""
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
|
||||
def __init__(self, job_handler, worker_count=0, busy_callback=None):
|
||||
@ -311,7 +311,7 @@ class ThreadPool(object):
|
||||
self.add_worker()
|
||||
|
||||
def add_worker(self):
|
||||
""" Adds a worker to the thread pool. Resets warning limit. """
|
||||
"""Add worker to the thread pool and reset warning limit."""
|
||||
with self._lock:
|
||||
if not self.running:
|
||||
raise RuntimeError("ThreadPool not running")
|
||||
@ -324,7 +324,7 @@ class ThreadPool(object):
|
||||
self.busy_warning_limit = self.worker_count * 3
|
||||
|
||||
def remove_worker(self):
|
||||
""" Removes a worker from the thread pool. Resets warning limit. """
|
||||
"""Remove worker from the thread pool and reset warning limit."""
|
||||
with self._lock:
|
||||
if not self.running:
|
||||
raise RuntimeError("ThreadPool not running")
|
||||
@ -354,18 +354,19 @@ class ThreadPool(object):
|
||||
self._work_queue.qsize())
|
||||
|
||||
def block_till_done(self):
|
||||
""" Blocks till all work is done. """
|
||||
"""Block till current work is done."""
|
||||
self._work_queue.join()
|
||||
# import traceback
|
||||
# traceback.print_stack()
|
||||
|
||||
def stop(self):
|
||||
""" Stops all the threads. """
|
||||
"""Finish all the jobs and stops all the threads."""
|
||||
self.block_till_done()
|
||||
|
||||
with self._lock:
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
# Ensure all current jobs finish
|
||||
self.block_till_done()
|
||||
|
||||
# Tell the workers to quit
|
||||
for _ in range(self.worker_count):
|
||||
self.remove_worker()
|
||||
@ -376,7 +377,7 @@ class ThreadPool(object):
|
||||
self.block_till_done()
|
||||
|
||||
def _worker(self):
|
||||
""" Handles jobs for the thread pool. """
|
||||
"""Handle jobs for the thread pool."""
|
||||
while True:
|
||||
# Get new item from work_queue
|
||||
job = self._work_queue.get().item
|
||||
|
@ -64,6 +64,9 @@ def tearDownModule(): # pylint: disable=invalid-name
|
||||
class TestLocative(unittest.TestCase):
|
||||
""" Test Locative """
|
||||
|
||||
def tearDown(self):
|
||||
hass.pool.block_till_done()
|
||||
|
||||
def test_missing_data(self, update_config):
|
||||
data = {
|
||||
'latitude': 1.0,
|
||||
|
@ -102,6 +102,9 @@ def _req(data={}):
|
||||
class TestAlexa(unittest.TestCase):
|
||||
""" Test Alexa. """
|
||||
|
||||
def tearDown(self):
|
||||
hass.pool.block_till_done()
|
||||
|
||||
def test_launch_request(self):
|
||||
data = {
|
||||
'version': '1.0',
|
||||
|
@ -59,6 +59,9 @@ def tearDownModule(): # pylint: disable=invalid-name
|
||||
class TestAPI(unittest.TestCase):
|
||||
""" Test the API. """
|
||||
|
||||
def tearDown(self):
|
||||
hass.pool.block_till_done()
|
||||
|
||||
# TODO move back to http component and test with use_auth.
|
||||
def test_access_denied_without_password(self):
|
||||
req = requests.get(_url(const.URL_API))
|
||||
|
@ -56,6 +56,9 @@ def tearDownModule(): # pylint: disable=invalid-name
|
||||
class TestFrontend(unittest.TestCase):
|
||||
""" Test the frontend. """
|
||||
|
||||
def tearDown(self):
|
||||
hass.pool.block_till_done()
|
||||
|
||||
def test_frontend_and_static(self):
|
||||
""" Tests if we can get the frontend. """
|
||||
req = requests.get(_url(""))
|
||||
|
@ -34,7 +34,7 @@ def _url(path=""):
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Initalizes a Home Assistant server and Slave instance. """
|
||||
global hass, slave, master_api, broken_api
|
||||
global hass, slave, master_api
|
||||
|
||||
hass = get_test_home_assistant()
|
||||
|
||||
@ -64,8 +64,6 @@ def setUpModule(): # pylint: disable=invalid-name
|
||||
|
||||
def tearDownModule(): # pylint: disable=invalid-name
|
||||
""" Stops the Home Assistant server and slave. """
|
||||
global hass, slave
|
||||
|
||||
slave.stop()
|
||||
hass.stop()
|
||||
|
||||
@ -73,6 +71,10 @@ def tearDownModule(): # pylint: disable=invalid-name
|
||||
class TestRemoteMethods(unittest.TestCase):
|
||||
""" Test the homeassistant.remote module. """
|
||||
|
||||
def tearDown(self):
|
||||
slave.pool.block_till_done()
|
||||
hass.pool.block_till_done()
|
||||
|
||||
def test_validate_api(self):
|
||||
""" Test Python API validate_api. """
|
||||
self.assertEqual(remote.APIStatus.OK, remote.validate_api(master_api))
|
||||
@ -193,10 +195,24 @@ class TestRemoteMethods(unittest.TestCase):
|
||||
# Should not raise an exception
|
||||
remote.call_service(broken_api, "test_domain", "test_service")
|
||||
|
||||
def test_json_encoder(self):
|
||||
""" Test the JSON Encoder. """
|
||||
ha_json_enc = remote.JSONEncoder()
|
||||
state = hass.states.get('test.test')
|
||||
|
||||
self.assertEqual(state.as_dict(), ha_json_enc.default(state))
|
||||
|
||||
# Default method raises TypeError if non HA object
|
||||
self.assertRaises(TypeError, ha_json_enc.default, 1)
|
||||
|
||||
|
||||
class TestRemoteClasses(unittest.TestCase):
|
||||
""" Test the homeassistant.remote module. """
|
||||
|
||||
def tearDown(self):
|
||||
slave.pool.block_till_done()
|
||||
hass.pool.block_till_done()
|
||||
|
||||
def test_home_assistant_init(self):
|
||||
""" Test HomeAssistant init. """
|
||||
# Wrong password
|
||||
@ -211,12 +227,8 @@ class TestRemoteClasses(unittest.TestCase):
|
||||
|
||||
def test_statemachine_init(self):
|
||||
""" Tests if remote.StateMachine copies all states on init. """
|
||||
self.assertEqual(len(hass.states.all()),
|
||||
len(slave.states.all()))
|
||||
|
||||
for state in hass.states.all():
|
||||
self.assertEqual(
|
||||
state, slave.states.get(state.entity_id))
|
||||
self.assertEqual(sorted(hass.states.all()),
|
||||
sorted(slave.states.all()))
|
||||
|
||||
def test_statemachine_set(self):
|
||||
""" Tests if setting the state on a slave is recorded. """
|
||||
@ -271,13 +283,3 @@ class TestRemoteClasses(unittest.TestCase):
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(test_value))
|
||||
|
||||
def test_json_encoder(self):
|
||||
""" Test the JSON Encoder. """
|
||||
ha_json_enc = remote.JSONEncoder()
|
||||
state = hass.states.get('test.test')
|
||||
|
||||
self.assertEqual(state.as_dict(), ha_json_enc.default(state))
|
||||
|
||||
# Default method raises TypeError if non HA object
|
||||
self.assertRaises(TypeError, ha_json_enc.default, 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user