refactor: migrate exec bridge to anyio and harden cancellation (#6)

This commit is contained in:
banteg
2025-12-31 01:51:46 +04:00
committed by GitHub
parent 6687a435c9
commit 8eda3f5e84
9 changed files with 492 additions and 310 deletions
+13 -23
View File
@@ -1,29 +1,19 @@
import asyncio
import sys
import pytest
from takopi import exec_bridge
def test_manage_subprocess_kills_when_terminate_times_out(monkeypatch) -> None:
async def fake_wait_for(awaitable, *args, **kwargs):
if hasattr(awaitable, "close"):
awaitable.close()
elif hasattr(awaitable, "cancel"):
awaitable.cancel()
raise asyncio.TimeoutError
@pytest.mark.anyio
async def test_manage_subprocess_kills_when_terminate_times_out() -> None:
async with exec_bridge.manage_subprocess(
sys.executable,
"-c",
"import signal, time; signal.signal(signal.SIGTERM, signal.SIG_IGN); time.sleep(10)",
terminate_timeout=0.01,
) as proc:
assert proc.returncode is None
monkeypatch.setattr(exec_bridge.asyncio, "wait_for", fake_wait_for)
async def run() -> int | None:
async with exec_bridge.manage_subprocess(
sys.executable,
"-c",
"import signal, time; signal.signal(signal.SIGTERM, signal.SIG_IGN); time.sleep(10)",
) as proc:
assert proc.returncode is None
return proc.returncode
rc = asyncio.run(run())
assert rc is not None
assert rc != 0
assert proc.returncode is not None
assert proc.returncode != 0