Speedup tests command_line integration (#96349)

pull/96407/head
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
) -> 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

View File

@ -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

View File

@ -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

View File

@ -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