Core cleanup: two stage shutdown (#5876)
* Core cleanup: two stage shutdown * fix spell * fix * add async logger to close * change aiohttp to use CLOSE * address paulus comments * Fix tests * Add unittest
This commit is contained in:
1 parent
4623d1071e
commit
41849eab06
8 files changed
+97
-107
No files matched your search
@@ -1,7 +1,6 @@
|
||||
"""The tests for the MQTT eventstream component."""
|
||||
from collections import namedtuple
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
from homeassistant.bootstrap import setup_component
|
||||
@@ -21,16 +20,15 @@ from tests.common import (
|
||||
)
|
||||
|
||||
|
||||
class TestMqttEventStream(unittest.TestCase):
|
||||
class TestMqttEventStream(object):
|
||||
"""Test the MQTT eventstream module."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
super(TestMqttEventStream, self).setUp()
|
||||
self.hass = get_test_home_assistant()
|
||||
self.mock_mqtt = mock_mqtt_component(self.hass)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@@ -46,24 +44,24 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
|
||||
def test_setup_succeeds(self):
|
||||
""""Test the success of the setup."""
|
||||
self.assertTrue(self.add_eventstream())
|
||||
assert self.add_eventstream()
|
||||
|
||||
def test_setup_with_pub(self):
|
||||
""""Test the setup with subscription."""
|
||||
# Should start off with no listeners for all events
|
||||
self.assertEqual(self.hass.bus.listeners.get('*'), None)
|
||||
assert self.hass.bus.listeners.get('*') is None
|
||||
|
||||
self.assertTrue(self.add_eventstream(pub_topic='bar'))
|
||||
assert self.add_eventstream(pub_topic='bar')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Verify that the event handler has been added as a listener
|
||||
self.assertEqual(self.hass.bus.listeners.get('*'), 1)
|
||||
assert self.hass.bus.listeners.get('*') == 1
|
||||
|
||||
@patch('homeassistant.components.mqtt.subscribe')
|
||||
def test_subscribe(self, mock_sub):
|
||||
""""Test the subscription."""
|
||||
sub_topic = 'foo'
|
||||
self.assertTrue(self.add_eventstream(sub_topic=sub_topic))
|
||||
assert self.add_eventstream(sub_topic=sub_topic)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Verify that the this entity was subscribed to the topic
|
||||
@@ -79,7 +77,7 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
mock_utcnow.return_value = now
|
||||
|
||||
# Add the eventstream component for publishing events
|
||||
self.assertTrue(self.add_eventstream(pub_topic=pub_topic))
|
||||
assert self.add_eventstream(pub_topic=pub_topic)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Reset the mock because it will have already gotten calls for the
|
||||
@@ -93,7 +91,7 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
# The order of the JSON is indeterminate,
|
||||
# so first just check that publish was called
|
||||
mock_pub.assert_called_with(self.hass, pub_topic, ANY)
|
||||
self.assertTrue(mock_pub.called)
|
||||
assert mock_pub.called
|
||||
|
||||
# Get the actual call to publish and make sure it was the one
|
||||
# we were looking for
|
||||
@@ -110,12 +108,12 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
event['event_data'] = {"new_state": new_state, "entity_id": e_id}
|
||||
|
||||
# Verify that the message received was that expected
|
||||
self.assertEqual(json.loads(msg), event)
|
||||
assert json.loads(msg) == event
|
||||
|
||||
@patch('homeassistant.components.mqtt.publish')
|
||||
def test_time_event_does_not_send_message(self, mock_pub):
|
||||
""""Test the sending of a new message if time event."""
|
||||
self.assertTrue(self.add_eventstream(pub_topic='bar'))
|
||||
assert self.add_eventstream(pub_topic='bar')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Reset the mock because it will have already gotten calls for the
|
||||
@@ -123,12 +121,12 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
mock_pub.reset_mock()
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow())
|
||||
self.assertFalse(mock_pub.called)
|
||||
assert not mock_pub.called
|
||||
|
||||
def test_receiving_remote_event_fires_hass_event(self):
|
||||
""""Test the receiving of the remotely fired event."""
|
||||
sub_topic = 'foo'
|
||||
self.assertTrue(self.add_eventstream(sub_topic=sub_topic))
|
||||
assert self.add_eventstream(sub_topic=sub_topic)
|
||||
self.hass.block_till_done()
|
||||
|
||||
calls = []
|
||||
@@ -147,7 +145,7 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
fire_mqtt_message(self.hass, sub_topic, payload)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
assert 1 == len(calls)
|
||||
|
||||
@patch('homeassistant.components.mqtt.publish')
|
||||
def test_mqtt_received_event(self, mock_pub):
|
||||
@@ -160,10 +158,9 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
|
||||
"""
|
||||
SUB_TOPIC = 'from_slaves'
|
||||
self.assertTrue(
|
||||
self.add_eventstream(
|
||||
assert self.add_eventstream(
|
||||
pub_topic='bar',
|
||||
sub_topic=SUB_TOPIC))
|
||||
sub_topic=SUB_TOPIC)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Reset the mock because it will have already gotten calls for the
|
||||
@@ -173,19 +170,21 @@ class TestMqttEventStream(unittest.TestCase):
|
||||
# Use MQTT component message handler to simulate firing message
|
||||
# received event.
|
||||
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
||||
message = MQTTMessage(SUB_TOPIC, 1, 'Hello World!'.encode('utf-8'))
|
||||
message = MQTTMessage(
|
||||
SUB_TOPIC, 1, '{"test": "Hello World!"}'.encode('utf-8'))
|
||||
mqtt.MQTT._mqtt_on_message(self, None, {'hass': self.hass}, message)
|
||||
|
||||
self.hass.block_till_done()
|
||||
|
||||
# 'normal' incoming mqtt messages should be broadcasted
|
||||
self.assertEqual(mock_pub.call_count, 0)
|
||||
assert mock_pub.call_count == 0
|
||||
|
||||
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
||||
message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8'))
|
||||
message = MQTTMessage(
|
||||
'test_topic', 1, '{"test": "Hello World!"}'.encode('utf-8'))
|
||||
mqtt.MQTT._mqtt_on_message(self, None, {'hass': self.hass}, message)
|
||||
|
||||
self.hass.block_till_done()
|
||||
|
||||
# but event from the event stream not
|
||||
self.assertEqual(mock_pub.call_count, 1)
|
||||
assert mock_pub.call_count == 1
|
||||
@@ -4,10 +4,10 @@ import unittest
|
||||
|
||||
import aiohttp
|
||||
|
||||
from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE
|
||||
from homeassistant.bootstrap import setup_component
|
||||
import homeassistant.helpers.aiohttp_client as client
|
||||
from homeassistant.util.async import (
|
||||
run_callback_threadsafe, run_coroutine_threadsafe)
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@@ -93,9 +93,8 @@ class TestHelpersAiohttpClient(unittest.TestCase):
|
||||
assert isinstance(
|
||||
self.hass.data[client.DATA_CONNECTOR], aiohttp.TCPConnector)
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
client.async_cleanup_websession(self.hass), self.hass.loop
|
||||
).result()
|
||||
self.hass.bus.fire(EVENT_HOMEASSISTANT_CLOSE)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.data[client.DATA_CLIENTSESSION].closed
|
||||
assert self.hass.data[client.DATA_CONNECTOR].closed
|
||||
@@ -112,9 +111,8 @@ class TestHelpersAiohttpClient(unittest.TestCase):
|
||||
self.hass.data[client.DATA_CONNECTOR_NOTVERIFY],
|
||||
aiohttp.TCPConnector)
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
client.async_cleanup_websession(self.hass), self.hass.loop
|
||||
).result()
|
||||
self.hass.bus.fire(EVENT_HOMEASSISTANT_CLOSE)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.data[client.DATA_CLIENTSESSION_NOTVERIFY].closed
|
||||
assert self.hass.data[client.DATA_CONNECTOR_NOTVERIFY].closed
|
||||
|
||||
@@ -71,7 +71,7 @@ class TestBootstrap:
|
||||
|
||||
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
||||
mock.patch('os.access', mock.Mock(return_value=True)), \
|
||||
mock.patch('homeassistant.bootstrap.enable_logging',
|
||||
mock.patch('homeassistant.bootstrap.async_enable_logging',
|
||||
mock.Mock(return_value=True)), \
|
||||
patch_yaml_files(files, True):
|
||||
self.hass = bootstrap.from_config_file('config.yaml')
|
||||
@@ -289,7 +289,7 @@ class TestBootstrap:
|
||||
assert not bootstrap.setup_component(self.hass, 'comp', {})
|
||||
assert 'comp' not in self.hass.config.components
|
||||
|
||||
@mock.patch('homeassistant.bootstrap.enable_logging')
|
||||
@mock.patch('homeassistant.bootstrap.async_enable_logging')
|
||||
@mock.patch('homeassistant.bootstrap.async_register_signal_handling')
|
||||
def test_home_assistant_core_config_validation(self, log_mock, sig_mock):
|
||||
"""Test if we pass in wrong information for HA conf."""
|
||||
|
||||
+21
-21
@@ -16,7 +16,7 @@ from homeassistant.util.unit_system import (METRIC_SYSTEM)
|
||||
from homeassistant.const import (
|
||||
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
|
||||
ATTR_NOW, EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_HOMEASSISTANT_START)
|
||||
EVENT_HOMEASSISTANT_CLOSE, EVENT_HOMEASSISTANT_START)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@@ -89,6 +89,26 @@ def test_async_run_job_delegates_non_async():
|
||||
assert len(hass.async_add_job.mock_calls) == 1
|
||||
|
||||
|
||||
def test_stage_shutdown():
|
||||
"""Simulate a shutdown, test calling stuff."""
|
||||
hass = get_test_home_assistant()
|
||||
test_stop = []
|
||||
test_close = []
|
||||
test_all = []
|
||||
|
||||
hass.bus.listen(
|
||||
EVENT_HOMEASSISTANT_STOP, lambda event: test_stop.append(event))
|
||||
hass.bus.listen(
|
||||
EVENT_HOMEASSISTANT_CLOSE, lambda event: test_close.append(event))
|
||||
hass.bus.listen('*', lambda event: test_all.append(event))
|
||||
|
||||
hass.stop()
|
||||
|
||||
assert len(test_stop) == 1
|
||||
assert len(test_close) == 1
|
||||
assert len(test_all) == 1
|
||||
|
||||
|
||||
class TestHomeAssistant(unittest.TestCase):
|
||||
"""Test the Home Assistant core classes."""
|
||||
|
||||
@@ -102,26 +122,6 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
# This test hangs on `loop.add_signal_handler`
|
||||
# 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.assertEqual(1, len(calls))
|
||||
|
||||
# self.hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||
# lambda event: calls.append(1))
|
||||
|
||||
# os.kill(os.getpid(), signal.SIGTERM)
|
||||
|
||||
# self.hass.block_till_done()
|
||||
|
||||
# self.assertEqual(1, len(calls))
|
||||
|
||||
def test_pending_sheduler(self):
|
||||
"""Add a coro to pending tasks."""
|
||||
call_count = []
|
||||
|
||||
Reference in new issue
Block a user