From 50442c56884889d42cb13c4f07220804e2c23e4f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 11 Jul 2023 18:31:32 +0200 Subject: [PATCH] Speedup tests command_line integration (#96349) --- .../command_line/test_binary_sensor.py | 29 ++++++++------- tests/components/command_line/test_cover.py | 34 ++++++++++++------ tests/components/command_line/test_sensor.py | 29 ++++++++------- tests/components/command_line/test_switch.py | 36 +++++++++++++------ 4 files changed, 83 insertions(+), 45 deletions(-) diff --git a/tests/components/command_line/test_binary_sensor.py b/tests/components/command_line/test_binary_sensor.py index 910288d6920..50971219f48 100644 --- a/tests/components/command_line/test_binary_sensor.py +++ b/tests/components/command_line/test_binary_sensor.py @@ -206,16 +206,19 @@ async def test_updating_to_often( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test handling updating when command already running.""" + + wait_till_event = asyncio.Event() + wait_till_event.set() called = [] class MockCommandBinarySensor(CommandBinarySensor): - """Mock entity that updates slow.""" + """Mock entity that updates.""" async def _async_update(self) -> None: - """Update slow.""" + """Update the entity.""" called.append(1) - # Add waiting time - await asyncio.sleep(1) + # Wait till event is set + await wait_till_event.wait() with patch( "homeassistant.components.command_line.binary_sensor.CommandBinarySensor", @@ -232,7 +235,7 @@ async def test_updating_to_often( "command": "echo 1", "payload_on": "1", "payload_off": "0", - "scan_interval": 0.1, + "scan_interval": 10, } } ] @@ -241,24 +244,26 @@ async def test_updating_to_often( await hass.async_block_till_done() assert called + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=15)) + wait_till_event.set() + asyncio.wait(0) assert ( "Updating Command Line Binary Sensor Test took longer than the scheduled update interval" not in caplog.text ) - called.clear() - caplog.clear() - async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) - await hass.async_block_till_done() + # Simulate update takes too long + wait_till_event.clear() + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + await asyncio.sleep(0) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + wait_till_event.set() - assert called assert ( "Updating Command Line Binary Sensor Test took longer than the scheduled update interval" in caplog.text ) - await asyncio.sleep(0.2) - async def test_updating_manually( hass: HomeAssistant, caplog: pytest.LogCaptureFixture diff --git a/tests/components/command_line/test_cover.py b/tests/components/command_line/test_cover.py index d4114f9bbbd..64fa2a60719 100644 --- a/tests/components/command_line/test_cover.py +++ b/tests/components/command_line/test_cover.py @@ -293,16 +293,19 @@ async def test_updating_to_often( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test handling updating when command already running.""" + called = [] + wait_till_event = asyncio.Event() + wait_till_event.set() class MockCommandCover(CommandCover): - """Mock entity that updates slow.""" + """Mock entity that updates.""" async def _async_update(self) -> None: - """Update slow.""" + """Update the entity.""" called.append(1) # Add waiting time - await asyncio.sleep(1) + await wait_till_event.wait() with patch( "homeassistant.components.command_line.cover.CommandCover", @@ -318,7 +321,7 @@ async def test_updating_to_often( "command_state": "echo 1", "value_template": "{{ value }}", "name": "Test", - "scan_interval": 0.1, + "scan_interval": 10, } } ] @@ -331,20 +334,31 @@ async def test_updating_to_often( "Updating Command Line Cover Test took longer than the scheduled update interval" not in caplog.text ) - called.clear() - caplog.clear() - - async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=11)) await hass.async_block_till_done() + assert called + called.clear() + assert ( + "Updating Command Line Cover Test took longer than the scheduled update interval" + not in caplog.text + ) + + # Simulate update takes too long + wait_till_event.clear() + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + await asyncio.sleep(0) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + wait_till_event.set() + + # Finish processing update + await hass.async_block_till_done() assert called assert ( "Updating Command Line Cover Test took longer than the scheduled update interval" in caplog.text ) - await asyncio.sleep(0.2) - async def test_updating_manually( hass: HomeAssistant, caplog: pytest.LogCaptureFixture diff --git a/tests/components/command_line/test_sensor.py b/tests/components/command_line/test_sensor.py index af7bf3222a1..a0f8f2cdf84 100644 --- a/tests/components/command_line/test_sensor.py +++ b/tests/components/command_line/test_sensor.py @@ -543,16 +543,18 @@ async def test_updating_to_often( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test handling updating when command already running.""" + wait_till_event = asyncio.Event() + wait_till_event.set() called = [] class MockCommandSensor(CommandSensor): - """Mock entity that updates slow.""" + """Mock entity that updates.""" async def _async_update(self) -> None: - """Update slow.""" + """Update entity.""" called.append(1) - # Add waiting time - await asyncio.sleep(1) + # Wait till event is set + await wait_till_event.wait() with patch( "homeassistant.components.command_line.sensor.CommandSensor", @@ -567,7 +569,7 @@ async def test_updating_to_often( "sensor": { "name": "Test", "command": "echo 1", - "scan_interval": 0.1, + "scan_interval": 10, } } ] @@ -576,24 +578,27 @@ async def test_updating_to_often( await hass.async_block_till_done() assert called + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=15)) + wait_till_event.set() + asyncio.wait(0) + assert ( "Updating Command Line Sensor Test took longer than the scheduled update interval" not in caplog.text ) - called.clear() - caplog.clear() - async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) - await hass.async_block_till_done() + # Simulate update takes too long + wait_till_event.clear() + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + await asyncio.sleep(0) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + wait_till_event.set() - assert called assert ( "Updating Command Line Sensor Test took longer than the scheduled update interval" in caplog.text ) - await asyncio.sleep(0.2) - async def test_updating_manually( hass: HomeAssistant, caplog: pytest.LogCaptureFixture diff --git a/tests/components/command_line/test_switch.py b/tests/components/command_line/test_switch.py index 12a037f0dd1..09e8c47d708 100644 --- a/tests/components/command_line/test_switch.py +++ b/tests/components/command_line/test_switch.py @@ -650,16 +650,19 @@ async def test_updating_to_often( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test handling updating when command already running.""" + called = [] + wait_till_event = asyncio.Event() + wait_till_event.set() class MockCommandSwitch(CommandSwitch): - """Mock entity that updates slow.""" + """Mock entity that updates.""" async def _async_update(self) -> None: - """Update slow.""" + """Update entity.""" called.append(1) - # Add waiting time - await asyncio.sleep(1) + # Wait till event is set + await wait_till_event.wait() with patch( "homeassistant.components.command_line.switch.CommandSwitch", @@ -676,7 +679,7 @@ async def test_updating_to_often( "command_on": "echo 2", "command_off": "echo 3", "name": "Test", - "scan_interval": 0.1, + "scan_interval": 10, } } ] @@ -689,20 +692,31 @@ async def test_updating_to_often( "Updating Command Line Switch Test took longer than the scheduled update interval" not in caplog.text ) - called.clear() - caplog.clear() - - async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=11)) await hass.async_block_till_done() + assert called + called.clear() + assert ( + "Updating Command Line Switch Test took longer than the scheduled update interval" + not in caplog.text + ) + + # Simulate update takes too long + wait_till_event.clear() + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + await asyncio.sleep(0) + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + wait_till_event.set() + + # Finish processing update + await hass.async_block_till_done() assert called assert ( "Updating Command Line Switch Test took longer than the scheduled update interval" in caplog.text ) - await asyncio.sleep(0.2) - async def test_updating_manually( hass: HomeAssistant, caplog: pytest.LogCaptureFixture