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