Speedup tests command_line integration (#96349)

This commit is contained in:
Jan Bouwhuis 2023-07-11 18:31:32 +02:00 committed by GitHub
parent 49b6c8ed6e
commit 50442c5688
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 45 deletions

View file

@ -206,16 +206,19 @@ async def test_updating_to_often(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test handling updating when command already running.""" """Test handling updating when command already running."""
wait_till_event = asyncio.Event()
wait_till_event.set()
called = [] called = []
class MockCommandBinarySensor(CommandBinarySensor): class MockCommandBinarySensor(CommandBinarySensor):
"""Mock entity that updates slow.""" """Mock entity that updates."""
async def _async_update(self) -> None: async def _async_update(self) -> None:
"""Update slow.""" """Update the entity."""
called.append(1) called.append(1)
# Add waiting time # Wait till event is set
await asyncio.sleep(1) await wait_till_event.wait()
with patch( with patch(
"homeassistant.components.command_line.binary_sensor.CommandBinarySensor", "homeassistant.components.command_line.binary_sensor.CommandBinarySensor",
@ -232,7 +235,7 @@ async def test_updating_to_often(
"command": "echo 1", "command": "echo 1",
"payload_on": "1", "payload_on": "1",
"payload_off": "0", "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() await hass.async_block_till_done()
assert called assert called
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=15))
wait_till_event.set()
asyncio.wait(0)
assert ( assert (
"Updating Command Line Binary Sensor Test took longer than the scheduled update interval" "Updating Command Line Binary Sensor Test took longer than the scheduled update interval"
not in caplog.text not in caplog.text
) )
called.clear()
caplog.clear()
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) # Simulate update takes too long
await hass.async_block_till_done() 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 ( assert (
"Updating Command Line Binary Sensor Test took longer than the scheduled update interval" "Updating Command Line Binary Sensor Test took longer than the scheduled update interval"
in caplog.text in caplog.text
) )
await asyncio.sleep(0.2)
async def test_updating_manually( async def test_updating_manually(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture

View file

@ -293,16 +293,19 @@ async def test_updating_to_often(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test handling updating when command already running.""" """Test handling updating when command already running."""
called = [] called = []
wait_till_event = asyncio.Event()
wait_till_event.set()
class MockCommandCover(CommandCover): class MockCommandCover(CommandCover):
"""Mock entity that updates slow.""" """Mock entity that updates."""
async def _async_update(self) -> None: async def _async_update(self) -> None:
"""Update slow.""" """Update the entity."""
called.append(1) called.append(1)
# Add waiting time # Add waiting time
await asyncio.sleep(1) await wait_till_event.wait()
with patch( with patch(
"homeassistant.components.command_line.cover.CommandCover", "homeassistant.components.command_line.cover.CommandCover",
@ -318,7 +321,7 @@ async def test_updating_to_often(
"command_state": "echo 1", "command_state": "echo 1",
"value_template": "{{ value }}", "value_template": "{{ value }}",
"name": "Test", "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" "Updating Command Line Cover Test took longer than the scheduled update interval"
not in caplog.text not in caplog.text
) )
called.clear() async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=11))
caplog.clear()
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1))
await hass.async_block_till_done() 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 called
assert ( assert (
"Updating Command Line Cover Test took longer than the scheduled update interval" "Updating Command Line Cover Test took longer than the scheduled update interval"
in caplog.text in caplog.text
) )
await asyncio.sleep(0.2)
async def test_updating_manually( async def test_updating_manually(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture

View file

@ -543,16 +543,18 @@ async def test_updating_to_often(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test handling updating when command already running.""" """Test handling updating when command already running."""
wait_till_event = asyncio.Event()
wait_till_event.set()
called = [] called = []
class MockCommandSensor(CommandSensor): class MockCommandSensor(CommandSensor):
"""Mock entity that updates slow.""" """Mock entity that updates."""
async def _async_update(self) -> None: async def _async_update(self) -> None:
"""Update slow.""" """Update entity."""
called.append(1) called.append(1)
# Add waiting time # Wait till event is set
await asyncio.sleep(1) await wait_till_event.wait()
with patch( with patch(
"homeassistant.components.command_line.sensor.CommandSensor", "homeassistant.components.command_line.sensor.CommandSensor",
@ -567,7 +569,7 @@ async def test_updating_to_often(
"sensor": { "sensor": {
"name": "Test", "name": "Test",
"command": "echo 1", "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() await hass.async_block_till_done()
assert called assert called
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=15))
wait_till_event.set()
asyncio.wait(0)
assert ( assert (
"Updating Command Line Sensor Test took longer than the scheduled update interval" "Updating Command Line Sensor Test took longer than the scheduled update interval"
not in caplog.text not in caplog.text
) )
called.clear()
caplog.clear()
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1)) # Simulate update takes too long
await hass.async_block_till_done() 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 ( assert (
"Updating Command Line Sensor Test took longer than the scheduled update interval" "Updating Command Line Sensor Test took longer than the scheduled update interval"
in caplog.text in caplog.text
) )
await asyncio.sleep(0.2)
async def test_updating_manually( async def test_updating_manually(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture

View file

@ -650,16 +650,19 @@ async def test_updating_to_often(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test handling updating when command already running.""" """Test handling updating when command already running."""
called = [] called = []
wait_till_event = asyncio.Event()
wait_till_event.set()
class MockCommandSwitch(CommandSwitch): class MockCommandSwitch(CommandSwitch):
"""Mock entity that updates slow.""" """Mock entity that updates."""
async def _async_update(self) -> None: async def _async_update(self) -> None:
"""Update slow.""" """Update entity."""
called.append(1) called.append(1)
# Add waiting time # Wait till event is set
await asyncio.sleep(1) await wait_till_event.wait()
with patch( with patch(
"homeassistant.components.command_line.switch.CommandSwitch", "homeassistant.components.command_line.switch.CommandSwitch",
@ -676,7 +679,7 @@ async def test_updating_to_often(
"command_on": "echo 2", "command_on": "echo 2",
"command_off": "echo 3", "command_off": "echo 3",
"name": "Test", "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" "Updating Command Line Switch Test took longer than the scheduled update interval"
not in caplog.text not in caplog.text
) )
called.clear() async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=11))
caplog.clear()
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=1))
await hass.async_block_till_done() 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 called
assert ( assert (
"Updating Command Line Switch Test took longer than the scheduled update interval" "Updating Command Line Switch Test took longer than the scheduled update interval"
in caplog.text in caplog.text
) )
await asyncio.sleep(0.2)
async def test_updating_manually( async def test_updating_manually(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture