Fix unhandled exceptions in dsmr (#33601)

* reordered the clearing of the closed Event so it can stay set at the end and not
leave a task waiting on close

* fixed the side effect so it returns one TimeoutError and after that success
Previously it reached the end of the single item list and threw an exception

* fixed the error. it doesn't happen on python 3.7.5 but CI is on 3.7.0

* fixed comment
This commit is contained in:
Ziv 2020-04-05 00:38:20 +03:00 committed by GitHub
parent 0a3ec6fea1
commit c3c7b68cac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 15 deletions

View file

@ -8,7 +8,8 @@ Entity to be updated with new values.
import asyncio import asyncio
import datetime import datetime
from decimal import Decimal from decimal import Decimal
from unittest.mock import Mock from itertools import chain, repeat
from unittest.mock import DEFAULT, Mock
import asynctest import asynctest
import pytest import pytest
@ -323,9 +324,10 @@ async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factor
config = {"platform": "dsmr", "reconnect_interval": 0} config = {"platform": "dsmr", "reconnect_interval": 0}
# override the mock to have it fail the first time # override the mock to have it fail the first time and succeed after
first_fail_connection_factory = Mock( first_fail_connection_factory = asynctest.CoroutineMock(
wraps=connection_factory, side_effect=[TimeoutError] return_value=(transport, protocol),
side_effect=chain([TimeoutError], repeat(DEFAULT)),
) )
monkeypatch.setattr( monkeypatch.setattr(
@ -336,7 +338,7 @@ async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factor
# wait for sleep to resolve # wait for sleep to resolve
await hass.async_block_till_done() await hass.async_block_till_done()
assert first_fail_connection_factory.call_count == 2, "connecting not retried" assert first_fail_connection_factory.call_count >= 2, "connecting not retried"
async def test_reconnect(hass, monkeypatch, mock_connection_factory): async def test_reconnect(hass, monkeypatch, mock_connection_factory):
@ -352,7 +354,6 @@ async def test_reconnect(hass, monkeypatch, mock_connection_factory):
async def wait_closed(): async def wait_closed():
await closed.wait() await closed.wait()
closed2.set() closed2.set()
closed.clear()
protocol.wait_closed = wait_closed protocol.wait_closed = wait_closed
@ -365,9 +366,10 @@ async def test_reconnect(hass, monkeypatch, mock_connection_factory):
# wait for lock set to resolve # wait for lock set to resolve
await closed2.wait() await closed2.wait()
closed2.clear() closed2.clear()
assert not closed.is_set() closed.clear()
closed.set()
await hass.async_block_till_done() await hass.async_block_till_done()
assert connection_factory.call_count >= 2, "connecting not retried" assert connection_factory.call_count >= 2, "connecting not retried"
# setting it so teardown can be successful
closed.set()

View file

@ -8,13 +8,6 @@ IGNORE_UNCAUGHT_EXCEPTIONS = [
("tests.components.default_config.test_init", "test_setup"), ("tests.components.default_config.test_init", "test_setup"),
("tests.components.demo.test_init", "test_setting_up_demo"), ("tests.components.demo.test_init", "test_setting_up_demo"),
("tests.components.discovery.test_init", "test_discover_config_flow"), ("tests.components.discovery.test_init", "test_discover_config_flow"),
("tests.components.dsmr.test_sensor", "test_default_setup"),
("tests.components.dsmr.test_sensor", "test_v4_meter"),
("tests.components.dsmr.test_sensor", "test_v5_meter"),
("tests.components.dsmr.test_sensor", "test_belgian_meter"),
("tests.components.dsmr.test_sensor", "test_belgian_meter_low"),
("tests.components.dsmr.test_sensor", "test_tcp"),
("tests.components.dsmr.test_sensor", "test_connection_errors_retry"),
("tests.components.dyson.test_air_quality", "test_purecool_aiq_attributes"), ("tests.components.dyson.test_air_quality", "test_purecool_aiq_attributes"),
("tests.components.dyson.test_air_quality", "test_purecool_aiq_update_state"), ("tests.components.dyson.test_air_quality", "test_purecool_aiq_update_state"),
( (