Allow WS queue to temporarily peak (#34175)

* Allow WS queue to temporarily peak

* Remove unused code
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-04-13 18:50:36 -07:00
1 parent dbcc294d67
commit bea354b82a
6 files changed
+116 -34

No files matched your search

+8 -8
View File
@@ -7,23 +7,23 @@ from homeassistant.setup import async_setup_component
@pytest.fixture
def websocket_client(hass, hass_ws_client, hass_access_token):
async def websocket_client(hass, hass_ws_client):
"""Create a websocket client."""
return hass.loop.run_until_complete(hass_ws_client(hass, hass_access_token))
return await hass_ws_client(hass)
@pytest.fixture
def no_auth_websocket_client(hass, loop, aiohttp_client):
async def no_auth_websocket_client(hass, aiohttp_client):
"""Websocket connection that requires authentication."""
assert loop.run_until_complete(async_setup_component(hass, "websocket_api", {}))
assert await async_setup_component(hass, "websocket_api", {})
client = loop.run_until_complete(aiohttp_client(hass.http.app))
ws = loop.run_until_complete(client.ws_connect(URL))
client = await aiohttp_client(hass.http.app)
ws = await client.ws_connect(URL)
auth_ok = loop.run_until_complete(ws.receive_json())
auth_ok = await ws.receive_json()
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
yield ws
if not ws.closed:
loop.run_until_complete(ws.close())
await ws.close()
@@ -0,0 +1,66 @@
"""Test Websocket API http module."""
from datetime import timedelta
from aiohttp import WSMsgType
from asynctest import patch
import pytest
from homeassistant.components.websocket_api import const, http
from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed
@pytest.fixture
def mock_low_queue():
"""Mock a low queue."""
with patch("homeassistant.components.websocket_api.http.MAX_PENDING_MSG", 5):
yield
@pytest.fixture
def mock_low_peak():
"""Mock a low queue."""
with patch("homeassistant.components.websocket_api.http.PENDING_MSG_PEAK", 5):
yield
async def test_pending_msg_overflow(hass, mock_low_queue, websocket_client):
"""Test get_panels command."""
for idx in range(10):
await websocket_client.send_json({"id": idx + 1, "type": "ping"})
msg = await websocket_client.receive()
assert msg.type == WSMsgType.close
async def test_pending_msg_peak(hass, mock_low_peak, hass_ws_client, caplog):
"""Test pending msg overflow command."""
orig_handler = http.WebSocketHandler
instance = None
def instantiate_handler(*args):
nonlocal instance
instance = orig_handler(*args)
return instance
with patch(
"homeassistant.components.websocket_api.http.WebSocketHandler",
instantiate_handler,
):
websocket_client = await hass_ws_client()
# Kill writer task and fill queue past peak
for _ in range(5):
instance._to_write.put_nowait(None)
# Trigger the peak check
instance._send_message({})
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=const.PENDING_MSG_PEAK_TIME + 1)
)
msg = await websocket_client.receive()
assert msg.type == WSMsgType.close
assert "Client unable to keep up with pending messages" in caplog.text
@@ -2,19 +2,11 @@
from unittest.mock import Mock, patch
from aiohttp import WSMsgType
import pytest
import voluptuous as vol
from homeassistant.components.websocket_api import const, messages
@pytest.fixture
def mock_low_queue():
"""Mock a low queue."""
with patch("homeassistant.components.websocket_api.http.MAX_PENDING_MSG", 5):
yield
async def test_invalid_message_format(websocket_client):
"""Test sending invalid JSON."""
await websocket_client.send_json({"type": 5})
@@ -46,14 +38,6 @@ async def test_quiting_hass(hass, websocket_client):
assert msg.type == WSMsgType.CLOSE
async def test_pending_msg_overflow(hass, mock_low_queue, websocket_client):
"""Test get_panels command."""
for idx in range(10):
await websocket_client.send_json({"id": idx + 1, "type": "ping"})
msg = await websocket_client.receive()
assert msg.type == WSMsgType.close
async def test_unknown_command(websocket_client):
"""Test get_panels command."""
await websocket_client.send_json({"id": 5, "type": "unknown_command"})