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:
Pascal Vizeli authored and GitHub committed 2017-02-13 06:24:07 +01:00
1 parent 4623d1071e
commit 41849eab06
8 files changed
+97 -107

No files matched your search

+23 -24
View File
@@ -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