fix: stop leaking a global os.kill mock from the shutdown route test

The bare 'os.kill = MagicMock()' was never restored, so every later test
in the same process ran with a no-op kill. Any test that kills a child
process and waits for it then hangs forever (resident sharkd sessions
waiting on an immortal process). Use monkeypatch so the patch is undone.
This commit is contained in:
YueGuobin 2026-09-08 00:04:04 +08:00
parent fa282e0f82
commit 0c540abbf2
No known key found for this signature in database

View File

@ -29,13 +29,18 @@ pytestmark = pytest.mark.asyncio
class TestControllerRoutes:
async def test_shutdown_local(self, app: FastAPI, client: AsyncClient, config: Config) -> None:
os.kill = MagicMock()
async def test_shutdown_local(self, app: FastAPI, client: AsyncClient, config: Config, monkeypatch) -> None:
# monkeypatch (not bare assignment): a global `os.kill = MagicMock()`
# is never restored and poisons every later test that kills a
# subprocess — resident sharkd sessions would wait() forever on an
# immortal process.
kill_mock = MagicMock()
monkeypatch.setattr(os, "kill", kill_mock)
config.settings.Server.local = True
response = await client.post(app.url_path_for("shutdown"))
assert response.status_code == status.HTTP_204_NO_CONTENT
assert os.kill.called
assert kill_mock.called
async def test_shutdown_non_local(self, app: FastAPI, client: AsyncClient, config: Config) -> None: