mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Speed up tests
This commit is contained in:
@@ -8,10 +8,10 @@ Provides tests to verify that Home Assistant core works.
|
||||
# pylint: disable=too-few-public-methods
|
||||
import os
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
import time
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pytz
|
||||
|
||||
@@ -55,29 +55,26 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
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. """
|
||||
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.pool.block_till_done()
|
||||
|
||||
self.hass.services.call(ha.DOMAIN, ha.SERVICE_HOMEASSISTANT_STOP)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
# Threads are unpredictable, try 20 times if we're ready
|
||||
wait_loops = 0
|
||||
while blocking_thread.is_alive() and wait_loops < 20:
|
||||
wait_loops += 1
|
||||
# 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())
|
||||
@@ -88,13 +85,9 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
lambda event: calls.append(1))
|
||||
|
||||
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 mock.patch('time.sleep', raise_keyboardinterrupt):
|
||||
with patch('homeassistant.core.time.sleep', raise_keyboardinterrupt):
|
||||
self.hass.block_till_stopped()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
@@ -400,9 +393,10 @@ class TestStateMachine(unittest.TestCase):
|
||||
def test_last_changed_not_updated_on_same_state(self):
|
||||
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.states.get('light.Bowl').last_changed)
|
||||
|
||||
Reference in New Issue
Block a user