Skip to content

Commit

Permalink
Abort tests when instances leaked (home-assistant#7623)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored and pvizeli committed May 17, 2017
1 parent f86edd4 commit a068efc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
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

0 comments on commit a068efc

Please sign in to comment.