fix: do not monkeypatch attributes on Starlette State in tests

pytest 9.2 reads the old value of a monkeypatched attribute from the
instance __dict__. Starlette State keeps attributes in an internal
dict, so the old value looks unset and the teardown deletes the
attribute for the rest of the test session. The first test that
touches app.state.exiting passes but removes it, and every later
monkeypatch.setattr on it fails with AttributeError.

Use plain assignments instead.
This commit is contained in:
YueGuobin committed 2026-11-04 09:22:15 +08:00
1 parent 8f763c0439
commit a56fce4c36
3 files changed
+50 -29

No files matched your search

+13 -9
View File
@@ -20,21 +20,25 @@ from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from fastapi import FastAPI
from gns3server.api.routes.compute import compute_api
from gns3server.api.routes.compute.notifications import project_ws_notifications
@pytest.mark.asyncio
async def test_compute_ws_stops_when_app_is_exiting(app: FastAPI, monkeypatch) -> None:
async def test_compute_ws_stops_when_app_is_exiting() -> None:
monkeypatch.setattr(compute_api.state, "exiting", True)
websocket = MagicMock()
websocket.app = compute_api
websocket.client = SimpleNamespace(host="127.0.0.1", port=1000)
websocket.send_text = AsyncMock()
# plain assignment: see the note in test_notifications.py — monkeypatch
# cannot round-trip attributes of Starlette's State on pytest 9.2+
compute_api.state.exiting = True
try:
websocket = MagicMock()
websocket.app = compute_api
websocket.client = SimpleNamespace(host="127.0.0.1", port=1000)
websocket.send_text = AsyncMock()
await asyncio.wait_for(project_ws_notifications(websocket=websocket), timeout=1)
await asyncio.wait_for(project_ws_notifications(websocket=websocket), timeout=1)
websocket.send_text.assert_not_awaited()
websocket.send_text.assert_not_awaited()
finally:
compute_api.state.exiting = False
@@ -111,29 +111,43 @@ class TestNotificationWebSocketShutdown:
Otherwise uvicorn has to wait for them during graceful shutdown.
"""
@pytest.mark.asyncio
async def test_controller_ws_stops_when_app_is_exiting(self, app: FastAPI, monkeypatch) -> None:
monkeypatch.setattr(app.state, "exiting", True)
websocket = _fake_websocket(app)
await asyncio.wait_for(controller_ws_notifications(websocket, current_user=MagicMock()), timeout=1)
websocket.send_text.assert_not_awaited()
# Do not use monkeypatch.setattr on app.state: Starlette's State keeps
# attributes in an internal dict, not in the instance __dict__, so pytest
# 9.2+ cannot read back the old value and its teardown deletes the
# attribute for the rest of the test session.
@pytest.mark.asyncio
async def test_project_ws_stops_when_app_is_exiting(self, app: FastAPI, project, monkeypatch) -> None:
async def test_controller_ws_stops_when_app_is_exiting(self, app: FastAPI) -> None:
app.state.exiting = True
try:
websocket = _fake_websocket(app)
await asyncio.wait_for(controller_ws_notifications(websocket, current_user=MagicMock()), timeout=1)
websocket.send_text.assert_not_awaited()
finally:
app.state.exiting = False
@pytest.mark.asyncio
async def test_project_ws_stops_when_app_is_exiting(self, app: FastAPI, project) -> None:
project.auto_close = False
monkeypatch.setattr(app.state, "exiting", True)
websocket = _fake_websocket(app)
await asyncio.wait_for(project_ws_notifications(project.id, websocket, current_user=MagicMock()), timeout=1)
websocket.send_text.assert_not_awaited()
app.state.exiting = True
try:
websocket = _fake_websocket(app)
await asyncio.wait_for(project_ws_notifications(project.id, websocket, current_user=MagicMock()), timeout=1)
websocket.send_text.assert_not_awaited()
finally:
app.state.exiting = False
@pytest.mark.asyncio
async def test_marker_ws_stops_when_app_is_exiting(self, app: FastAPI, project, monkeypatch) -> None:
async def test_marker_ws_stops_when_app_is_exiting(self, app: FastAPI, project) -> None:
monkeypatch.setattr(app.state, "exiting", True)
websocket = _fake_websocket(app)
await asyncio.wait_for(
project_marker_ws_notifications(project.id, websocket, current_user=MagicMock()), timeout=1
)
websocket.send_text.assert_not_awaited()
app.state.exiting = True
try:
websocket = _fake_websocket(app)
await asyncio.wait_for(
project_marker_ws_notifications(project.id, websocket, current_user=MagicMock()), timeout=1
)
websocket.send_text.assert_not_awaited()
finally:
app.state.exiting = False