mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Abort tests when instances leaked (#7623)
This commit is contained in:
parent
f86edd4f24
commit
a068efcd47
@ -33,7 +33,7 @@ from homeassistant.util.async import (
|
|||||||
|
|
||||||
_TEST_INSTANCE_PORT = SERVER_PORT
|
_TEST_INSTANCE_PORT = SERVER_PORT
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
INST_COUNT = 0
|
INSTANCES = []
|
||||||
|
|
||||||
|
|
||||||
def threadsafe_callback_factory(func):
|
def threadsafe_callback_factory(func):
|
||||||
@ -98,11 +98,10 @@ def get_test_home_assistant():
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_test_home_assistant(loop):
|
def async_test_home_assistant(loop):
|
||||||
"""Return a Home Assistant object pointing at test config dir."""
|
"""Return a Home Assistant object pointing at test config dir."""
|
||||||
global INST_COUNT
|
|
||||||
INST_COUNT += 1
|
|
||||||
loop._thread_ident = threading.get_ident()
|
loop._thread_ident = threading.get_ident()
|
||||||
|
|
||||||
hass = ha.HomeAssistant(loop)
|
hass = ha.HomeAssistant(loop)
|
||||||
|
INSTANCES.append(hass)
|
||||||
|
|
||||||
orig_async_add_job = hass.async_add_job
|
orig_async_add_job = hass.async_add_job
|
||||||
|
|
||||||
@ -134,8 +133,7 @@ def async_test_home_assistant(loop):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def mock_async_start():
|
def mock_async_start():
|
||||||
"""Start the mocking."""
|
"""Start the mocking."""
|
||||||
# 1. We only mock time during tests
|
# We only mock time during tests and we want to track tasks
|
||||||
# 2. We want block_till_done that is called inside stop_track_tasks
|
|
||||||
with patch('homeassistant.core._async_create_timer'), \
|
with patch('homeassistant.core._async_create_timer'), \
|
||||||
patch.object(hass, 'async_stop_track_tasks'):
|
patch.object(hass, 'async_stop_track_tasks'):
|
||||||
yield from orig_start()
|
yield from orig_start()
|
||||||
@ -145,8 +143,7 @@ def async_test_home_assistant(loop):
|
|||||||
@ha.callback
|
@ha.callback
|
||||||
def clear_instance(event):
|
def clear_instance(event):
|
||||||
"""Clear global instance."""
|
"""Clear global instance."""
|
||||||
global INST_COUNT
|
INSTANCES.remove(hass)
|
||||||
INST_COUNT -= 1
|
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from homeassistant import util, setup
|
|||||||
from homeassistant.util import location
|
from homeassistant.util import location
|
||||||
from homeassistant.components import mqtt
|
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.test_util.aiohttp import mock_aiohttp_client
|
||||||
from tests.mock.zwave import MockNetwork, MockOption
|
from tests.mock.zwave import MockNetwork, MockOption
|
||||||
|
|
||||||
@ -50,8 +50,12 @@ def verify_cleanup():
|
|||||||
"""Verify that the test has cleaned up resources correctly."""
|
"""Verify that the test has cleaned up resources correctly."""
|
||||||
yield
|
yield
|
||||||
|
|
||||||
from tests import common
|
if len(INSTANCES) >= 2:
|
||||||
assert common.INST_COUNT < 2
|
count = len(INSTANCES)
|
||||||
|
for inst in INSTANCES:
|
||||||
|
inst.stop()
|
||||||
|
pytest.exit("Detected non stopped instances "
|
||||||
|
"({}), aborting test run".format(count))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
Loading…
x
Reference in New Issue
Block a user