Speed up tests

This commit is contained in:
Paulus Schoutsen 2015-09-12 22:56:49 -07:00
parent bb42e264cb
commit cfc23b0091
10 changed files with 97 additions and 106 deletions

View File

@ -244,17 +244,17 @@ class Throttle(object):
Wrapper that allows wrapped to be called only once per min_time. Wrapper that allows wrapped to be called only once per min_time.
If we cannot acquire the lock, it is running so return None. If we cannot acquire the lock, it is running so return None.
""" """
if lock.acquire(False): if not lock.acquire(False):
return None
try: try:
last_call = wrapper.last_call last_call = wrapper.last_call
# Check if method is never called or no_throttle is given # Check if method is never called or no_throttle is given
force = not last_call or kwargs.pop('no_throttle', False) force = not last_call or kwargs.pop('no_throttle', False)
if force or datetime.now() - last_call > self.min_time: if force or utcnow() - last_call > self.min_time:
result = method(*args, **kwargs) result = method(*args, **kwargs)
wrapper.last_call = datetime.now() wrapper.last_call = utcnow()
return result return result
else: else:
return None return None

View File

@ -38,8 +38,8 @@ def get_test_home_assistant(num_threads=None):
hass.config.latitude = 32.87336 hass.config.latitude = 32.87336
hass.config.longitude = -117.22743 hass.config.longitude = -117.22743
# if not loader.PREPARED: if 'custom_components.test' not in loader.AVAILABLE_COMPONENTS:
loader. prepare(hass) loader.prepare(hass)
return hass return hass

View File

@ -5,6 +5,7 @@ tests.test_component_demo
Tests demo component. Tests demo component.
""" """
import unittest import unittest
from unittest.mock import patch
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.components.demo as demo import homeassistant.components.demo as demo
@ -23,13 +24,15 @@ class TestDemo(unittest.TestCase):
""" Stop down stuff we started. """ """ Stop down stuff we started. """
self.hass.stop() self.hass.stop()
def test_if_demo_state_shows_by_default(self): @patch('homeassistant.components.sun.setup')
def test_if_demo_state_shows_by_default(self, mock_sun_setup):
""" Test if demo state shows if we give no configuration. """ """ Test if demo state shows if we give no configuration. """
demo.setup(self.hass, {demo.DOMAIN: {}}) demo.setup(self.hass, {demo.DOMAIN: {}})
self.assertIsNotNone(self.hass.states.get('a.Demo_Mode')) self.assertIsNotNone(self.hass.states.get('a.Demo_Mode'))
def test_hiding_demo_state(self): @patch('homeassistant.components.sun.setup')
def test_hiding_demo_state(self, mock_sun_setup):
""" Test if you can hide the demo card. """ """ Test if you can hide the demo card. """
demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}}) demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}})

View File

@ -8,6 +8,8 @@ Tests the history component.
import time import time
import os import os
import unittest import unittest
from unittest.mock import patch
from datetime import timedelta
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -68,11 +70,7 @@ class TestComponentHistory(unittest.TestCase):
self.init_recorder() self.init_recorder()
states = [] states = []
# Create 10 states for 5 different entities for i in range(5):
# After the first 5, sleep a second and save the time
# history.get_states takes the latest states BEFORE point X
for i in range(10):
state = ha.State( state = ha.State(
'test.point_in_time_{}'.format(i % 5), 'test.point_in_time_{}'.format(i % 5),
"State {}".format(i), "State {}".format(i),
@ -80,19 +78,27 @@ class TestComponentHistory(unittest.TestCase):
mock_state_change_event(self.hass, state) mock_state_change_event(self.hass, state)
self.hass.pool.block_till_done() self.hass.pool.block_till_done()
recorder._INSTANCE.block_till_done()
if i < 5:
states.append(state) states.append(state)
if i == 4: recorder._INSTANCE.block_till_done()
time.sleep(1)
point = dt_util.utcnow()
self.assertEqual( point = dt_util.utcnow() + timedelta(seconds=1)
states,
sorted( with patch('homeassistant.util.dt.utcnow', return_value=point):
history.get_states(point), key=lambda state: state.entity_id)) for i in range(5):
state = ha.State(
'test.point_in_time_{}'.format(i % 5),
"State {}".format(i),
{'attribute_test': i})
mock_state_change_event(self.hass, state)
self.hass.pool.block_till_done()
# Get states returns everything before POINT
self.assertEqual(states,
sorted(history.get_states(point),
key=lambda state: state.entity_id))
# Test get_state here because we have a DB setup # Test get_state here because we have a DB setup
self.assertEqual( self.assertEqual(
@ -113,9 +119,10 @@ class TestComponentHistory(unittest.TestCase):
set_state('YouTube') set_state('YouTube')
start = dt_util.utcnow() start = dt_util.utcnow()
point = start + timedelta(seconds=1)
end = point + timedelta(seconds=1)
time.sleep(1) with patch('homeassistant.util.dt.utcnow', return_value=point):
states = [ states = [
set_state('idle'), set_state('idle'),
set_state('Netflix'), set_state('Netflix'),
@ -123,10 +130,7 @@ class TestComponentHistory(unittest.TestCase):
set_state('YouTube'), set_state('YouTube'),
] ]
time.sleep(1) with patch('homeassistant.util.dt.utcnow', return_value=end):
end = dt_util.utcnow()
set_state('Netflix') set_state('Netflix')
set_state('Plex') set_state('Plex')

View File

@ -7,9 +7,9 @@ Tests switch component.
# pylint: disable=too-many-public-methods,protected-access # pylint: disable=too-many-public-methods,protected-access
import unittest import unittest
import homeassistant.loader as loader from homeassistant import loader
from homeassistant.components import switch
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
import homeassistant.components.switch as switch
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant

View File

@ -7,13 +7,13 @@ Tests component helpers.
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import unittest import unittest
from common import get_test_home_assistant
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.loader as loader import homeassistant.loader as loader
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID
from homeassistant.helpers import extract_entity_ids from homeassistant.helpers import extract_entity_ids
from tests.common import get_test_home_assistant
class TestComponentsCore(unittest.TestCase): class TestComponentsCore(unittest.TestCase):
""" Tests homeassistant.components module. """ """ Tests homeassistant.components module. """

View File

@ -15,7 +15,7 @@ from homeassistant.const import (
CONF_LATITUDE, CONF_LONGITUDE, CONF_TEMPERATURE_UNIT, CONF_NAME, CONF_LATITUDE, CONF_LONGITUDE, CONF_TEMPERATURE_UNIT, CONF_NAME,
CONF_TIME_ZONE) CONF_TIME_ZONE)
from common import get_test_config_dir, mock_detect_location_info from tests.common import get_test_config_dir, mock_detect_location_info
CONFIG_DIR = get_test_config_dir() CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE) YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)

View File

@ -8,10 +8,10 @@ Provides tests to verify that Home Assistant core works.
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
import os import os
import unittest import unittest
import unittest.mock as mock from unittest.mock import patch
import time import time
import threading import threading
from datetime import datetime from datetime import datetime, timedelta
import pytz import pytz
@ -55,29 +55,26 @@ class TestHomeAssistant(unittest.TestCase):
self.hass.pool.block_till_done() self.hass.pool.block_till_done()
self.assertEqual(1, len(calls)) self.assertEqual(1, len(calls))
# @patch('homeassistant.core.time.sleep')
def test_block_till_stoped(self): def test_block_till_stoped(self):
""" Test if we can block till stop service is called. """ """ Test if we can block till stop service is called. """
blocking_thread = threading.Thread(target=self.hass.block_till_stopped) with patch('time.sleep'):
blocking_thread = threading.Thread(
target=self.hass.block_till_stopped)
self.assertFalse(blocking_thread.is_alive()) self.assertFalse(blocking_thread.is_alive())
blocking_thread.start() blocking_thread.start()
# Threads are unpredictable, try 20 times if we're ready
wait_loops = 0
while not blocking_thread.is_alive() and wait_loops < 20:
wait_loops += 1
time.sleep(0.05)
self.assertTrue(blocking_thread.is_alive()) self.assertTrue(blocking_thread.is_alive())
self.hass.services.call(ha.DOMAIN, ha.SERVICE_HOMEASSISTANT_STOP) self.hass.services.call(ha.DOMAIN, ha.SERVICE_HOMEASSISTANT_STOP)
self.hass.pool.block_till_done() self.hass.pool.block_till_done()
# Threads are unpredictable, try 20 times if we're ready # Wait for thread to stop
wait_loops = 0 for _ in range(20):
while blocking_thread.is_alive() and wait_loops < 20: if not blocking_thread.is_alive():
wait_loops += 1 break
time.sleep(0.05) time.sleep(0.05)
self.assertFalse(blocking_thread.is_alive()) self.assertFalse(blocking_thread.is_alive())
@ -88,13 +85,9 @@ class TestHomeAssistant(unittest.TestCase):
lambda event: calls.append(1)) lambda event: calls.append(1))
def raise_keyboardinterrupt(length): def raise_keyboardinterrupt(length):
# We don't want to patch the sleep of the timer.
if length == 1:
raise KeyboardInterrupt raise KeyboardInterrupt
self.hass.start() with patch('homeassistant.core.time.sleep', raise_keyboardinterrupt):
with mock.patch('time.sleep', raise_keyboardinterrupt):
self.hass.block_till_stopped() self.hass.block_till_stopped()
self.assertEqual(1, len(calls)) self.assertEqual(1, len(calls))
@ -400,9 +393,10 @@ class TestStateMachine(unittest.TestCase):
def test_last_changed_not_updated_on_same_state(self): def test_last_changed_not_updated_on_same_state(self):
state = self.states.get('light.Bowl') state = self.states.get('light.Bowl')
time.sleep(1) future = dt_util.utcnow() + timedelta(hours=10)
self.states.set("light.Bowl", "on") with patch('homeassistant.util.dt.utcnow', return_value=future):
self.states.set("light.Bowl", "on", {'attr': 'triggers_change'})
self.assertEqual(state.last_changed, self.assertEqual(state.last_changed,
self.states.get('light.Bowl').last_changed) self.states.get('light.Bowl').last_changed)

View File

@ -10,7 +10,7 @@ import unittest
import homeassistant.loader as loader import homeassistant.loader as loader
import homeassistant.components.http as http import homeassistant.components.http as http
from common import get_test_home_assistant, MockModule from tests.common import get_test_home_assistant, MockModule
class TestLoader(unittest.TestCase): class TestLoader(unittest.TestCase):
@ -24,9 +24,9 @@ class TestLoader(unittest.TestCase):
def test_set_component(self): def test_set_component(self):
""" Test if set_component works. """ """ Test if set_component works. """
loader.set_component('switch.test', http) loader.set_component('switch.test_set', http)
self.assertEqual(http, loader.get_component('switch.test')) self.assertEqual(http, loader.get_component('switch.test_set'))
def test_get_component(self): def test_get_component(self):
""" Test if get_component works. """ """ Test if get_component works. """

View File

@ -6,10 +6,11 @@ Tests Home Assistant util methods.
""" """
# pylint: disable=too-many-public-methods # pylint: disable=too-many-public-methods
import unittest import unittest
import time from unittest.mock import patch
from datetime import datetime, timedelta from datetime import datetime, timedelta
import homeassistant.util as util from homeassistant import util
import homeassistant.util.dt as dt_util
class TestUtil(unittest.TestCase): class TestUtil(unittest.TestCase):
@ -169,21 +170,19 @@ class TestUtil(unittest.TestCase):
def test_throttle(self): def test_throttle(self):
""" Test the add cooldown decorator. """ """ Test the add cooldown decorator. """
calls1 = [] calls1 = []
calls2 = []
@util.Throttle(timedelta(milliseconds=500)) @util.Throttle(timedelta(seconds=4))
def test_throttle1(): def test_throttle1():
calls1.append(1) calls1.append(1)
calls2 = [] @util.Throttle(timedelta(seconds=4), timedelta(seconds=2))
@util.Throttle(
timedelta(milliseconds=500), timedelta(milliseconds=250))
def test_throttle2(): def test_throttle2():
calls2.append(1) calls2.append(1)
# Ensure init is ok now = dt_util.utcnow()
self.assertEqual(0, len(calls1)) plus3 = now + timedelta(seconds=3)
self.assertEqual(0, len(calls2)) plus5 = plus3 + timedelta(seconds=2)
# Call first time and ensure methods got called # Call first time and ensure methods got called
test_throttle1() test_throttle1()
@ -206,25 +205,16 @@ class TestUtil(unittest.TestCase):
self.assertEqual(2, len(calls1)) self.assertEqual(2, len(calls1))
self.assertEqual(1, len(calls2)) self.assertEqual(1, len(calls2))
# Sleep past the no throttle interval for throttle2 with patch('homeassistant.util.utcnow', return_value=plus3):
time.sleep(.3)
test_throttle1() test_throttle1()
test_throttle2() test_throttle2()
self.assertEqual(2, len(calls1)) self.assertEqual(2, len(calls1))
self.assertEqual(1, len(calls2)) self.assertEqual(1, len(calls2))
test_throttle1(no_throttle=True) with patch('homeassistant.util.utcnow', return_value=plus5):
test_throttle2(no_throttle=True)
self.assertEqual(3, len(calls1))
self.assertEqual(2, len(calls2))
time.sleep(.5)
test_throttle1() test_throttle1()
test_throttle2() test_throttle2()
self.assertEqual(4, len(calls1)) self.assertEqual(3, len(calls1))
self.assertEqual(3, len(calls2)) self.assertEqual(2, len(calls2))