Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

_TEST_INSTANCE_PORT = SERVER_PORT
_LOGGER = logging.getLogger(__name__)
INST_COUNT = 0
INSTANCES = []


def threadsafe_callback_factory(func):
Expand Down Expand Up @@ -98,11 +98,10 @@ def stop_hass():
@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

Expand Down Expand Up @@ -134,8 +133,7 @@ def async_add_job(target, *args):
@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()
Expand All @@ -145,8 +143,7 @@ def mock_async_start():
@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)

Expand Down
10 changes: 7 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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

Expand Down Expand Up @@ -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
Expand Down