Fix shell_command timeout test (#96834)

* Fix shell_command timeout test

* Improve test
pull/96820/head^2
Erik Montnemery 2023-07-18 13:37:27 +02:00 committed by GitHub
parent 2a18d0a764
commit 5c54fa1ce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 11 deletions

View File

@ -1,9 +1,10 @@
"""The tests for the Shell command component."""
from __future__ import annotations
import asyncio
import os
import tempfile
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, Mock, patch
import pytest
@ -160,24 +161,37 @@ async def test_stderr_captured(mock_output, hass: HomeAssistant) -> None:
assert test_phrase.encode() + b"\n" == mock_output.call_args_list[0][0][-1]
@pytest.mark.skip(reason="disabled to check if it fixes flaky CI")
async def test_do_no_run_forever(
async def test_do_not_run_forever(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test subprocesses terminate after the timeout."""
with patch.object(shell_command, "COMMAND_TIMEOUT", 0.001):
assert await async_setup_component(
hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "sleep 10000"}},
)
await hass.async_block_till_done()
async def block():
event = asyncio.Event()
await event.wait()
return (None, None)
mock_process = Mock()
mock_process.communicate = block
mock_process.kill = Mock()
mock_create_subprocess_shell = AsyncMock(return_value=mock_process)
assert await async_setup_component(
hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "mock_sleep 10000"}},
)
await hass.async_block_till_done()
with patch.object(shell_command, "COMMAND_TIMEOUT", 0.001), patch(
"homeassistant.components.shell_command.asyncio.create_subprocess_shell",
side_effect=mock_create_subprocess_shell,
):
await hass.services.async_call(
shell_command.DOMAIN, "test_service", blocking=True
)
await hass.async_block_till_done()
mock_process.kill.assert_called_once()
assert "Timed out" in caplog.text
assert "sleep 10000" in caplog.text
assert "mock_sleep 10000" in caplog.text