From a56fce4c36115afa670de5c3e1e3a16a98bf8b27 Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Wed, 4 Nov 2026 09:22:15 +0800 Subject: [PATCH] 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. --- .../api/routes/compute/test_notifications.py | 22 ++++---- .../routes/controller/test_notifications.py | 52 ++++++++++++------- tests/controller/test_compute.py | 5 +- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/tests/api/routes/compute/test_notifications.py b/tests/api/routes/compute/test_notifications.py index 59e87134c..77c8cbd0d 100644 --- a/tests/api/routes/compute/test_notifications.py +++ b/tests/api/routes/compute/test_notifications.py @@ -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 diff --git a/tests/api/routes/controller/test_notifications.py b/tests/api/routes/controller/test_notifications.py index c21ab6af1..e2bd523e5 100644 --- a/tests/api/routes/controller/test_notifications.py +++ b/tests/api/routes/controller/test_notifications.py @@ -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 diff --git a/tests/controller/test_compute.py b/tests/controller/test_compute.py index 40f09efaf..4ac4ef137 100644 --- a/tests/controller/test_compute.py +++ b/tests/controller/test_compute.py @@ -640,7 +640,10 @@ async def test_connect_notification_poison_frame_autoreconnects(compute, monkeyp # would execute module-level from-imports while the autouse fixture's # monkeypatches are active, freezing patched objects into namespaces) monkeypatch.delattr(sys, "_called_from_test", raising=False) - monkeypatch.setattr(gns3_app.state, "exiting", False) + # plain assignment instead of monkeypatch.setattr: Starlette's State keeps + # attributes in an internal dict, so pytest 9.2+ teardown would delete the + # attribute for the rest of the test session + gns3_app.state.exiting = False async def fake_connect(): compute._reconnect_attempted = True