Abort tests when instances leaked (#7623)

This commit is contained in:
Paulus Schoutsen 2017-05-17 15:19:40 -07:00 committed by Pascal Vizeli
parent f86edd4f24
commit a068efcd47
2 changed files with 11 additions and 10 deletions

View file

@ -33,7 +33,7 @@ from homeassistant.util.async import (
_TEST_INSTANCE_PORT = SERVER_PORT
_LOGGER = logging.getLogger(__name__)
INST_COUNT = 0
INSTANCES = []
def threadsafe_callback_factory(func):
@ -98,11 +98,10 @@ 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)
INSTANCES.append(hass)
orig_async_add_job = hass.async_add_job
@ -134,8 +133,7 @@ def async_test_home_assistant(loop):
@asyncio.coroutine
def mock_async_start():
"""Start the mocking."""
# 1. We only mock time during tests
# 2. We want block_till_done that is called inside stop_track_tasks
# We only mock time during tests and we want to track tasks
with patch('homeassistant.core._async_create_timer'), \
patch.object(hass, 'async_stop_track_tasks'):
yield from orig_start()
@ -145,8 +143,7 @@ def async_test_home_assistant(loop):
@ha.callback
def clear_instance(event):
"""Clear global instance."""
global INST_COUNT
INST_COUNT -= 1
INSTANCES.remove(hass)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)

View file

@ -12,7 +12,7 @@ from homeassistant import util, setup
from homeassistant.util import location
from homeassistant.components import mqtt
from tests.common import async_test_home_assistant, mock_coro
from tests.common import async_test_home_assistant, mock_coro, INSTANCES
from tests.test_util.aiohttp import mock_aiohttp_client
from tests.mock.zwave import MockNetwork, MockOption
@ -50,8 +50,12 @@ def verify_cleanup():
"""Verify that the test has cleaned up resources correctly."""
yield
from tests import common
assert common.INST_COUNT < 2
if len(INSTANCES) >= 2:
count = len(INSTANCES)
for inst in INSTANCES:
inst.stop()
pytest.exit("Detected non stopped instances "
"({}), aborting test run".format(count))
@pytest.fixture