mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
ping: Suppress ProcessLookupError on timeout (#134281)
This commit is contained in:
parent
3a493bb6c0
commit
cad49453eb
@ -160,7 +160,7 @@ class PingDataSubProcess(PingData):
|
||||
)
|
||||
|
||||
if pinger:
|
||||
with suppress(TypeError):
|
||||
with suppress(TypeError, ProcessLookupError):
|
||||
await pinger.kill() # type: ignore[func-returns-value]
|
||||
del pinger
|
||||
|
||||
|
59
tests/components/ping/test_helpers.py
Normal file
59
tests/components/ping/test_helpers.py
Normal file
@ -0,0 +1,59 @@
|
||||
"""Test the exception handling in subprocess version of async_ping."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.ping.helpers import PingDataSubProcess
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
class MockAsyncSubprocess:
|
||||
"""Minimal mock implementation of asyncio.subprocess.Process for exception testing."""
|
||||
|
||||
def __init__(self, killsig=ProcessLookupError, **kwargs) -> None:
|
||||
"""Store provided exception type for later."""
|
||||
self.killsig = killsig
|
||||
|
||||
async def communicate(self) -> None:
|
||||
"""Fails immediately with a timeout."""
|
||||
raise TimeoutError
|
||||
|
||||
async def kill(self) -> None:
|
||||
"""Raise preset exception when called."""
|
||||
raise self.killsig
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exc", [TypeError, ProcessLookupError])
|
||||
async def test_async_ping_expected_exceptions(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
exc: Exception,
|
||||
) -> None:
|
||||
"""Test PingDataSubProcess.async_ping handles expected exceptions."""
|
||||
with patch(
|
||||
"asyncio.create_subprocess_exec", return_value=MockAsyncSubprocess(killsig=exc)
|
||||
):
|
||||
# Actual parameters irrelevant, as subprocess will not be created
|
||||
ping = PingDataSubProcess(hass, host="10.10.10.10", count=3, privileged=False)
|
||||
assert await ping.async_ping() is None
|
||||
|
||||
|
||||
async def test_async_ping_unexpected_exceptions(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test PingDataSubProcess.async_ping does not suppress unexpected exceptions."""
|
||||
with patch(
|
||||
"asyncio.create_subprocess_exec",
|
||||
return_value=MockAsyncSubprocess(killsig=KeyboardInterrupt),
|
||||
):
|
||||
# Actual parameters irrelevant, as subprocess will not be created
|
||||
ping = PingDataSubProcess(hass, host="10.10.10.10", count=3, privileged=False)
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
assert await ping.async_ping() is None
|
Loading…
x
Reference in New Issue
Block a user