From c3c7b68cacd345b3601be055943eede160992e8c Mon Sep 17 00:00:00 2001
From: Ziv <16467659+ziv1234@users.noreply.github.com>
Date: Sun, 5 Apr 2020 00:38:20 +0300
Subject: [PATCH] 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
---
 tests/components/dsmr/test_sensor.py | 18 ++++++++++--------
 tests/ignore_uncaught_exceptions.py  |  7 -------
 2 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/tests/components/dsmr/test_sensor.py b/tests/components/dsmr/test_sensor.py
index ead9e08d00f..9f3b45d40e3 100644
--- a/tests/components/dsmr/test_sensor.py
+++ b/tests/components/dsmr/test_sensor.py
@@ -8,7 +8,8 @@ Entity to be updated with new values.
 import asyncio
 import datetime
 from decimal import Decimal
-from unittest.mock import Mock
+from itertools import chain, repeat
+from unittest.mock import DEFAULT, Mock
 
 import asynctest
 import pytest
@@ -323,9 +324,10 @@ async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factor
 
     config = {"platform": "dsmr", "reconnect_interval": 0}
 
-    # override the mock to have it fail the first time
-    first_fail_connection_factory = Mock(
-        wraps=connection_factory, side_effect=[TimeoutError]
+    # override the mock to have it fail the first time and succeed after
+    first_fail_connection_factory = asynctest.CoroutineMock(
+        return_value=(transport, protocol),
+        side_effect=chain([TimeoutError], repeat(DEFAULT)),
     )
 
     monkeypatch.setattr(
@@ -336,7 +338,7 @@ async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factor
 
     # wait for sleep to resolve
     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):
@@ -352,7 +354,6 @@ async def test_reconnect(hass, monkeypatch, mock_connection_factory):
     async def wait_closed():
         await closed.wait()
         closed2.set()
-        closed.clear()
 
     protocol.wait_closed = wait_closed
 
@@ -365,9 +366,10 @@ async def test_reconnect(hass, monkeypatch, mock_connection_factory):
     # wait for lock set to resolve
     await closed2.wait()
     closed2.clear()
-    assert not closed.is_set()
+    closed.clear()
 
-    closed.set()
     await hass.async_block_till_done()
 
     assert connection_factory.call_count >= 2, "connecting not retried"
+    # setting it so teardown can be successful
+    closed.set()
diff --git a/tests/ignore_uncaught_exceptions.py b/tests/ignore_uncaught_exceptions.py
index f0a47b3e64f..dc93f9a6350 100644
--- a/tests/ignore_uncaught_exceptions.py
+++ b/tests/ignore_uncaught_exceptions.py
@@ -8,13 +8,6 @@ IGNORE_UNCAUGHT_EXCEPTIONS = [
     ("tests.components.default_config.test_init", "test_setup"),
     ("tests.components.demo.test_init", "test_setting_up_demo"),
     ("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_update_state"),
     (
-- 
GitLab