Migrate core from threads to async awesomeness (#3248)
* Add event loop to the core * Add block_till_done to HA core object * Fix some tests * Linting core * Fix statemachine tests * Core test fixes * fix block_till_done to wait for loop and queue to empty * fix test_core for passing, and correct start/stop/block_till_done * Fix remote tests * Fix tests: block_till_done * Fix linting * Fix more tests * Fix final linting * Fix remote test * remove unnecessary import * reduce sleep to avoid slowing down the tests excessively * fix remaining tests to wait for non-threadsafe operations * Add async_ doc strings for event loop / coroutine info * Fix command line test to block for the right timeout * Fix py3.4.2 loop var access * Fix SERVICE_CALL_LIMIT being in effect for other tests * Fix lint errors * Fix lint error with proper placement * Fix slave start to not start a timer * Add asyncio compatible listeners. * Increase min Python version to 3.4.2 * Move async backports to util * Add backported async tests * Fix linting * Simplify Python version check * Fix lint * Remove unneeded try/except and queue listener appproriately. * Fix tuple vs. list unorderable error on version compare. * Fix version tests
This commit is contained in:
1 parent
24f1bff7f1
commit
609d7ebea5
98 files changed
+1680
-1109
No files matched your search
+42
-105
@@ -5,15 +5,12 @@ import os
|
||||
import signal
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import time
|
||||
import threading
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pytz
|
||||
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.exceptions import (
|
||||
HomeAssistantError, InvalidEntityFormatError)
|
||||
from homeassistant.exceptions import InvalidEntityFormatError
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.unit_system import (METRIC_SYSTEM)
|
||||
from homeassistant.const import (
|
||||
@@ -39,63 +36,28 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
self.hass.states.set("switch.AC", "off")
|
||||
self.hass = get_test_home_assistant(0)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop everything that was started."""
|
||||
try:
|
||||
self.hass.stop()
|
||||
except HomeAssistantError:
|
||||
# Already stopped after the block till stopped test
|
||||
pass
|
||||
self.hass.stop()
|
||||
|
||||
def test_start(self):
|
||||
def test_start_and_sigterm(self):
|
||||
"""Start the test."""
|
||||
calls = []
|
||||
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START,
|
||||
lambda event: calls.append(1))
|
||||
|
||||
self.hass.start()
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
# @patch('homeassistant.core.time.sleep')
|
||||
def test_block_till_stoped(self):
|
||||
"""Test if we can block till stop service is called."""
|
||||
with patch('time.sleep'):
|
||||
blocking_thread = threading.Thread(
|
||||
target=self.hass.block_till_stopped)
|
||||
|
||||
self.assertFalse(blocking_thread.is_alive())
|
||||
|
||||
blocking_thread.start()
|
||||
|
||||
self.assertTrue(blocking_thread.is_alive())
|
||||
|
||||
self.hass.services.call(ha.DOMAIN, ha.SERVICE_HOMEASSISTANT_STOP)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
# Wait for thread to stop
|
||||
for _ in range(20):
|
||||
if not blocking_thread.is_alive():
|
||||
break
|
||||
time.sleep(0.05)
|
||||
|
||||
self.assertFalse(blocking_thread.is_alive())
|
||||
|
||||
def test_stopping_with_sigterm(self):
|
||||
"""Test for stopping with sigterm."""
|
||||
calls = []
|
||||
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||
lambda event: calls.append(1))
|
||||
|
||||
def send_sigterm(length):
|
||||
"""Send sigterm."""
|
||||
os.kill(os.getpid(), signal.SIGTERM)
|
||||
os.kill(os.getpid(), signal.SIGTERM)
|
||||
|
||||
with patch('homeassistant.core.time.sleep', send_sigterm):
|
||||
self.hass.block_till_stopped()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
@@ -147,16 +109,16 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.bus = ha.EventBus(ha.create_worker_pool(0))
|
||||
self.hass = get_test_home_assistant()
|
||||
self.bus = self.hass.bus
|
||||
self.bus.listen('test_event', lambda x: len)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop down stuff we started."""
|
||||
self.bus._pool.stop()
|
||||
self.hass.stop()
|
||||
|
||||
def test_add_remove_listener(self):
|
||||
"""Test remove_listener method."""
|
||||
self.bus._pool.add_worker()
|
||||
old_count = len(self.bus.listeners)
|
||||
|
||||
def listener(_): pass
|
||||
@@ -177,7 +139,6 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
def test_unsubscribe_listener(self):
|
||||
"""Test unsubscribe listener from returned function."""
|
||||
self.bus._pool.add_worker()
|
||||
calls = []
|
||||
|
||||
def listener(event):
|
||||
@@ -187,14 +148,14 @@ class TestEventBus(unittest.TestCase):
|
||||
unsub = self.bus.listen('test', listener)
|
||||
|
||||
self.bus.fire('test')
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
unsub()
|
||||
|
||||
self.bus.fire('event')
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
@@ -208,8 +169,7 @@ class TestEventBus(unittest.TestCase):
|
||||
# Second time it should not increase runs
|
||||
self.bus.fire('test_event')
|
||||
|
||||
self.bus._pool.add_worker()
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(runs))
|
||||
|
||||
|
||||
@@ -274,15 +234,14 @@ class TestStateMachine(unittest.TestCase):
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.pool = ha.create_worker_pool(0)
|
||||
self.bus = ha.EventBus(self.pool)
|
||||
self.states = ha.StateMachine(self.bus)
|
||||
self.hass = get_test_home_assistant(0)
|
||||
self.states = self.hass.states
|
||||
self.states.set("light.Bowl", "on")
|
||||
self.states.set("switch.AC", "off")
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop down stuff we started."""
|
||||
self.pool.stop()
|
||||
self.hass.stop()
|
||||
|
||||
def test_is_state(self):
|
||||
"""Test is_state method."""
|
||||
@@ -320,14 +279,13 @@ class TestStateMachine(unittest.TestCase):
|
||||
|
||||
def test_remove(self):
|
||||
"""Test remove method."""
|
||||
self.pool.add_worker()
|
||||
events = []
|
||||
self.bus.listen(EVENT_STATE_CHANGED,
|
||||
lambda event: events.append(event))
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED,
|
||||
lambda event: events.append(event))
|
||||
|
||||
self.assertIn('light.bowl', self.states.entity_ids())
|
||||
self.assertTrue(self.states.remove('light.bowl'))
|
||||
self.pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertNotIn('light.bowl', self.states.entity_ids())
|
||||
self.assertEqual(1, len(events))
|
||||
@@ -338,18 +296,18 @@ class TestStateMachine(unittest.TestCase):
|
||||
|
||||
# If it does not exist, we should get False
|
||||
self.assertFalse(self.states.remove('light.Bowl'))
|
||||
self.pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
def test_case_insensitivty(self):
|
||||
"""Test insensitivty."""
|
||||
self.pool.add_worker()
|
||||
runs = []
|
||||
|
||||
self.bus.listen(EVENT_STATE_CHANGED, lambda event: runs.append(event))
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED,
|
||||
lambda event: runs.append(event))
|
||||
|
||||
self.states.set('light.BOWL', 'off')
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertTrue(self.states.is_state('light.bowl', 'off'))
|
||||
self.assertEqual(1, len(runs))
|
||||
@@ -362,22 +320,23 @@ class TestStateMachine(unittest.TestCase):
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=future):
|
||||
self.states.set("light.Bowl", "on", {'attr': 'triggers_change'})
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(state.last_changed,
|
||||
self.states.get('light.Bowl').last_changed)
|
||||
state2 = self.states.get('light.Bowl')
|
||||
assert state2 is not None
|
||||
assert state.last_changed == state2.last_changed
|
||||
|
||||
def test_force_update(self):
|
||||
"""Test force update option."""
|
||||
self.pool.add_worker()
|
||||
events = []
|
||||
self.bus.listen(EVENT_STATE_CHANGED, events.append)
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED, events.append)
|
||||
|
||||
self.states.set('light.bowl', 'on')
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(events))
|
||||
|
||||
self.states.set('light.bowl', 'on', None, True)
|
||||
self.bus._pool.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
|
||||
@@ -400,21 +359,14 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.pool = ha.create_worker_pool(0)
|
||||
self.bus = ha.EventBus(self.pool)
|
||||
|
||||
def add_job(*args, **kwargs):
|
||||
"""Forward calls to add_job on Home Assistant."""
|
||||
# self works because we also have self.pool defined.
|
||||
return ha.HomeAssistant.add_job(self, *args, **kwargs)
|
||||
|
||||
self.services = ha.ServiceRegistry(self.bus, add_job)
|
||||
self.hass = get_test_home_assistant()
|
||||
self.services = self.hass.services
|
||||
self.services.register("Test_Domain", "TEST_SERVICE", lambda x: None)
|
||||
self.hass.block_till_done()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop down stuff we started."""
|
||||
if self.pool.worker_count:
|
||||
self.pool.stop()
|
||||
self.hass.stop()
|
||||
|
||||
def test_has_service(self):
|
||||
"""Test has_service method."""
|
||||
@@ -434,8 +386,6 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
def test_call_with_blocking_done_in_time(self):
|
||||
"""Test call with blocking."""
|
||||
self.pool.add_worker()
|
||||
self.pool.add_worker()
|
||||
calls = []
|
||||
self.services.register("test_domain", "register_calls",
|
||||
lambda x: calls.append(1))
|
||||
@@ -444,28 +394,15 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
def test_call_with_blocking_not_done_in_time(self):
|
||||
"""Test with blocking."""
|
||||
calls = []
|
||||
self.services.register("test_domain", "register_calls",
|
||||
lambda x: calls.append(1))
|
||||
|
||||
orig_limit = ha.SERVICE_CALL_LIMIT
|
||||
ha.SERVICE_CALL_LIMIT = 0.01
|
||||
self.assertFalse(
|
||||
self.services.call('test_domain', 'register_calls', blocking=True))
|
||||
self.assertEqual(0, len(calls))
|
||||
ha.SERVICE_CALL_LIMIT = orig_limit
|
||||
|
||||
def test_call_non_existing_with_blocking(self):
|
||||
"""Test non-existing with blocking."""
|
||||
self.pool.add_worker()
|
||||
self.pool.add_worker()
|
||||
orig_limit = ha.SERVICE_CALL_LIMIT
|
||||
ha.SERVICE_CALL_LIMIT = 0.01
|
||||
self.assertFalse(
|
||||
self.services.call('test_domain', 'i_do_not_exist', blocking=True))
|
||||
ha.SERVICE_CALL_LIMIT = orig_limit
|
||||
prior = ha.SERVICE_CALL_LIMIT
|
||||
try:
|
||||
ha.SERVICE_CALL_LIMIT = 0.01
|
||||
assert not self.services.call('test_domain', 'i_do_not_exist',
|
||||
blocking=True)
|
||||
finally:
|
||||
ha.SERVICE_CALL_LIMIT = prior
|
||||
|
||||
|
||||
class TestConfig(unittest.TestCase):
|
||||
|
||||
Reference in new issue
Block a user