Rework timer delays (#16650)

* Calibrate timer for each tick

* Return of timer out of sync detection
This commit is contained in:
Anders Melchiorsen 2018-09-17 10:10:50 +02:00 committed by Paulus Schoutsen
parent 44fdfdf695
commit 3e0c6c176a
2 changed files with 41 additions and 33 deletions

View file

@ -4,7 +4,7 @@ import asyncio
import logging
import os
import unittest
from unittest.mock import patch, MagicMock, sentinel
from unittest.mock import patch, MagicMock
from datetime import datetime, timedelta
from tempfile import TemporaryDirectory
@ -858,23 +858,25 @@ def test_create_timer(mock_monotonic, loop):
funcs.append(func)
return orig_callback(func)
mock_monotonic.side_effect = 10.2, 10.3
mock_monotonic.side_effect = 10.2, 10.8, 11.3
with patch.object(ha, 'callback', mock_callback), \
patch('homeassistant.core.dt_util.utcnow',
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
ha._async_create_timer(hass)
assert len(funcs) == 2
fire_time_event, stop_timer = funcs
assert len(hass.loop.call_later.mock_calls) == 1
slp_seconds, action = hass.loop.call_later.mock_calls[0][1]
assert abs(slp_seconds - 0.666667) < 0.001
delay, callback, target = hass.loop.call_later.mock_calls[0][1]
assert abs(delay - 0.666667) < 0.001
assert callback is fire_time_event
assert abs(target - 10.866667) < 0.001
with patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
action()
assert len(funcs) == 2
fire_time_event, stop_timer = funcs
return_value=datetime(2018, 12, 31, 3, 4, 6, 100000)):
callback(target)
assert len(hass.bus.async_listen_once.mock_calls) == 1
assert len(hass.bus.async_fire.mock_calls) == 1
@ -884,14 +886,14 @@ def test_create_timer(mock_monotonic, loop):
assert event_type == EVENT_HOMEASSISTANT_STOP
assert callback is stop_timer
slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[1][1]
assert abs(slp_seconds - 0.9) < 0.001
delay, callback, target = hass.loop.call_later.mock_calls[1][1]
assert abs(delay - 0.9) < 0.001
assert callback is fire_time_event
assert abs(nxt - 11.2) < 0.001
assert abs(target - 12.2) < 0.001
event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
assert event_type == EVENT_TIME_CHANGED
assert event_data[ATTR_NOW] is sentinel.mock_date
assert event_data[ATTR_NOW] == datetime(2018, 12, 31, 3, 4, 6, 100000)
@patch('homeassistant.core.monotonic')
@ -905,28 +907,31 @@ def test_timer_out_of_sync(mock_monotonic, loop):
funcs.append(func)
return orig_callback(func)
mock_monotonic.side_effect = 10.2, 11.3, 11.3
mock_monotonic.side_effect = 10.2, 13.3, 13.4
with patch.object(ha, 'callback', mock_callback), \
patch('homeassistant.core.dt_util.utcnow',
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
ha._async_create_timer(hass)
_, action = hass.loop.call_later.mock_calls[0][1]
delay, callback, target = hass.loop.call_later.mock_calls[0][1]
with patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
action()
with patch.object(ha, '_LOGGER', MagicMock()) as mock_logger, \
patch('homeassistant.core.dt_util.utcnow',
return_value=datetime(2018, 12, 31, 3, 4, 8, 200000)):
callback(target)
assert len(mock_logger.error.mock_calls) == 1
assert len(funcs) == 2
fire_time_event, stop_timer = funcs
assert len(hass.loop.call_later.mock_calls) == 2
slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[1][1]
assert slp_seconds == 1
delay, callback, target = hass.loop.call_later.mock_calls[1][1]
assert abs(delay - 0.8) < 0.001
assert callback is fire_time_event
assert abs(nxt - 12.3) < 0.001
assert abs(target - 14.2) < 0.001
@asyncio.coroutine