Skip to content

Commit

Permalink
Fix slow tests (home-assistant#3444)
Browse files Browse the repository at this point in the history
* Fix RFXtrx tests

* Report slow tests on CI

* Minor rfxtrx clean up

* rfxtrx test tweak
  • Loading branch information
balloob authored Sep 19, 2016
1 parent 256062f commit 87fe83d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 5 additions & 3 deletions homeassistant/components/rfxtrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ def handle_receive(event):

if dummy_connection:
RFXOBJECT =\
rfxtrxmod.Core(device, handle_receive, debug=debug,
transport_protocol=rfxtrxmod.DummyTransport2)
rfxtrxmod.Connect(device, handle_receive, debug=debug,
transport_protocol=rfxtrxmod.DummyTransport2)
else:
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug)
RFXOBJECT = rfxtrxmod.Connect(device, handle_receive, debug=debug)

def _shutdown_rfxtrx(event):
"""Close connection with RFXtrx."""
RFXOBJECT.close_connection()

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx)

return True
Expand Down
26 changes: 12 additions & 14 deletions tests/components/test_rfxtrx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Th tests for the Rfxtrx component."""
# pylint: disable=too-many-public-methods,protected-access
import unittest
import time
from unittest.mock import patch

from homeassistant.bootstrap import _setup_component
from homeassistant.components import rfxtrx as rfxtrx
Expand All @@ -13,17 +13,16 @@ class TestRFXTRX(unittest.TestCase):

def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant(0)
self.hass = get_test_home_assistant()

def tearDown(self):
"""Stop everything that was started."""
rfxtrx.RECEIVED_EVT_SUBSCRIBERS = []
rfxtrx.RFX_DEVICES = {}
if rfxtrx.RFXOBJECT:
rfxtrx.RFXOBJECT.close_connection()
self.hass.stop()

def test_default_config(self):
@patch('RFXtrx.sleep')
def test_default_config(self, mock_sleep):
"""Test configuration."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': {
Expand All @@ -37,19 +36,19 @@ def test_default_config(self):
'automatic_add': True,
'devices': {}}}))

while len(rfxtrx.RFX_DEVICES) < 2:
time.sleep(0.1)

self.assertEqual(len(rfxtrx.RFXOBJECT.sensors()), 2)

def test_valid_config(self):
@patch('RFXtrx.sleep')
def test_valid_config(self, mock_sleep):
"""Test configuration."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': {
'device': '/dev/serial/by-id/usb' +
'-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0',
'dummy': True}}))

self.hass.config.components.remove('rfxtrx')

self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': {
'device': '/dev/serial/by-id/usb' +
Expand All @@ -69,9 +68,9 @@ def test_invalid_config(self):
'-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0',
'invalid_key': True}}))

def test_fire_event(self):
@patch('RFXtrx.sleep')
def test_fire_event(self, mock_sleep):
"""Test fire event."""

self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': {
'device': '/dev/serial/by-id/usb' +
Expand Down Expand Up @@ -110,12 +109,12 @@ def record_event(event):
self.assertEqual(event.values['Command'], "On")
self.assertEqual('on', entity.state)
self.assertEqual(self.hass.states.get('switch.test').state, 'on')
self.assertEqual(1, len(rfxtrx.RFX_DEVICES))
self.assertEqual(1, len(calls))
self.assertEqual(calls[0].data,
{'entity_id': 'switch.test', 'state': 'on'})

def test_fire_event_sensor(self):
@patch('RFXtrx.sleep')
def test_fire_event_sensor(self, mock_sleep):
"""Test fire event."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': {
Expand Down Expand Up @@ -145,7 +144,6 @@ def record_event(event):
rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event)

self.hass.block_till_done()
self.assertEqual(1, len(rfxtrx.RFX_DEVICES))
self.assertEqual(1, len(calls))
self.assertEqual(calls[0].data,
{'entity_id': 'sensor.test'})
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ setenv =
LANG=en_US.UTF-8
PYTHONPATH = {toxinidir}:{toxinidir}/homeassistant
commands =
py.test -v --timeout=30 --cov --cov-report= {posargs}
py.test -v --timeout=30 --duration=10 --cov --cov-report= {posargs}
deps =
-r{toxinidir}/requirements_all.txt
-r{toxinidir}/requirements_test.txt
Expand Down

0 comments on commit 87fe83d

Please sign in to comment.