From 86d4d101764b454423b6d622d840943c724f1bd3 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 26 Feb 2017 14:05:18 -0800 Subject: [PATCH] Ensure we properly close HASS instances. (#6234) --- tests/common.py | 12 +++++++++++- tests/conftest.py | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/common.py b/tests/common.py index 82623dd0e2d..93ddc7c2f65 100644 --- a/tests/common.py +++ b/tests/common.py @@ -23,7 +23,7 @@ import homeassistant.util.yaml as yaml from homeassistant.const import ( STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE, - ATTR_DISCOVERED, SERVER_PORT) + ATTR_DISCOVERED, SERVER_PORT, EVENT_HOMEASSISTANT_STOP) from homeassistant.components import sun, mqtt, recorder from homeassistant.components.http.auth import auth_middleware from homeassistant.components.http.const import ( @@ -32,6 +32,7 @@ from homeassistant.util.async import run_callback_threadsafe _TEST_INSTANCE_PORT = SERVER_PORT _LOGGER = logging.getLogger(__name__) +INST_COUNT = 0 def get_test_config_dir(*add_path): @@ -85,6 +86,8 @@ def get_test_home_assistant(): @asyncio.coroutine def async_test_home_assistant(loop): """Return a Home Assistant object pointing at test config dir.""" + global INST_COUNT + INST_COUNT += 1 loop._thread_ident = threading.get_ident() hass = ha.HomeAssistant(loop) @@ -122,6 +125,13 @@ def async_test_home_assistant(loop): hass.async_start = mock_async_start + @ha.callback + def clear_instance(event): + global INST_COUNT + INST_COUNT -= 1 + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, clear_instance) + return hass diff --git a/tests/conftest.py b/tests/conftest.py index 1e987a5f0a2..33c5d9f0917 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,6 +38,15 @@ location.elevation = test_real(location.elevation) util.get_local_ip = lambda: '127.0.0.1' +@pytest.fixture(autouse=True) +def verify_cleanup(): + """Verify that the test has cleaned up resources correctly.""" + yield + + from tests import common + assert common.INST_COUNT < 2 + + @pytest.fixture def hass(loop): """Fixture to provide a test instance of HASS."""