Fix shell_command timeout test (#96834)
* Fix shell_command timeout test * Improve testpull/96820/head^2
parent
2a18d0a764
commit
5c54fa1ce1
|
@ -1,9 +1,10 @@
|
||||||
"""The tests for the Shell command component."""
|
"""The tests for the Shell command component."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||||
|
|
||||||
import pytest
|
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]
|
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_not_run_forever(
|
||||||
async def test_do_no_run_forever(
|
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test subprocesses terminate after the timeout."""
|
"""Test subprocesses terminate after the timeout."""
|
||||||
|
|
||||||
with patch.object(shell_command, "COMMAND_TIMEOUT", 0.001):
|
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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
shell_command.DOMAIN,
|
shell_command.DOMAIN,
|
||||||
{shell_command.DOMAIN: {"test_service": "sleep 10000"}},
|
{shell_command.DOMAIN: {"test_service": "mock_sleep 10000"}},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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(
|
await hass.services.async_call(
|
||||||
shell_command.DOMAIN, "test_service", blocking=True
|
shell_command.DOMAIN, "test_service", blocking=True
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
mock_process.kill.assert_called_once()
|
||||||
assert "Timed out" in caplog.text
|
assert "Timed out" in caplog.text
|
||||||
assert "sleep 10000" in caplog.text
|
assert "mock_sleep 10000" in caplog.text
|
||||||
|
|
Loading…
Reference in New Issue