1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Shield async httpx call in generic (#47852)

* Shield async httpx call

* Don't set last_url/last_image on cancellation

* Add test
This commit is contained in:
uvjustin
2021-03-31 12:46:10 +08:00
committed by GitHub
parent 7a6c88feeb
commit 379843eb54
2 changed files with 74 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ import asyncio
from os import path
from unittest.mock import patch
import httpx
import respx
from homeassistant import config as hass_config
@@ -407,5 +408,56 @@ async def test_reloading(hass, hass_client):
assert body == "hello world"
@respx.mock
async def test_timeout_cancelled(hass, hass_client):
"""Test that timeouts and cancellations return last image."""
respx.get("http://example.com").respond(text="hello world")
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
}
},
)
await hass.async_block_till_done()
client = await hass_client()
resp = await client.get("/api/camera_proxy/camera.config_test")
assert resp.status == 200
assert respx.calls.call_count == 1
assert await resp.text() == "hello world"
respx.get("http://example.com").respond(text="not hello world")
with patch(
"homeassistant.components.generic.camera.GenericCamera._async_camera_image",
side_effect=asyncio.CancelledError(),
):
resp = await client.get("/api/camera_proxy/camera.config_test")
assert respx.calls.call_count == 1
assert resp.status == 500
respx.get("http://example.com").side_effect = [
httpx.RequestError,
httpx.TimeoutException,
]
for total_calls in range(2, 4):
resp = await client.get("/api/camera_proxy/camera.config_test")
assert respx.calls.call_count == total_calls
assert resp.status == 200
assert await resp.text() == "hello world"
def _get_fixtures_base_path():
return path.dirname(path.dirname(path.dirname(__file__)))