[dashboard] Fix flaky test_websocket_refresh_command on Windows CI (#16565)

This commit is contained in:
J. Nick Koston
2026-05-22 08:49:03 -05:00
committed by Jesse Hills
parent 7ae5566472
commit 59db9a4673

View File

@@ -1503,13 +1503,18 @@ async def test_websocket_refresh_command(
) -> None: ) -> None:
"""Test WebSocket refresh command triggers dashboard update.""" """Test WebSocket refresh command triggers dashboard update."""
with patch("esphome.dashboard.web_server.DASHBOARD_SUBSCRIBER") as mock_subscriber: with patch("esphome.dashboard.web_server.DASHBOARD_SUBSCRIBER") as mock_subscriber:
mock_subscriber.request_refresh = Mock() # Signal an asyncio.Event when request_refresh is invoked so the
# test can deterministically wait for the server-side handler to run
# instead of relying on a fixed sleep (flaky on Windows CI under load).
called = asyncio.Event()
mock_subscriber.request_refresh = Mock(side_effect=called.set)
# Send refresh command # Send refresh command
await websocket_client.write_message(json.dumps({"event": "refresh"})) await websocket_client.write_message(json.dumps({"event": "refresh"}))
# Give it a moment to process # Wait for the server to process the message and invoke request_refresh
await asyncio.sleep(0.01) async with asyncio.timeout(5):
await called.wait()
# Verify request_refresh was called # Verify request_refresh was called
mock_subscriber.request_refresh.assert_called_once() mock_subscriber.request_refresh.assert_called_once()