Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

test: deflake websocket room-membership assertion (#10615)

Kiran Gadhaveeeda2fae4f88
diff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.py
index bff1668eb2..97b1f45d02 100644
--- a/tests/_server/api/endpoints/test_ws.py
+++ b/tests/_server/api/endpoints/test_ws.py
@@ -19,6 +19,7 @@ from marimo._server.api.endpoints.ws.ws_connection_validator import (
from marimo._server.codes import WebSocketCodes
from marimo._server.session_manager import SessionManager
from marimo._session.model import ConnectionState, SessionMode
+from marimo._types.ids import SessionId
from marimo._utils.parse_dataclass import parse_raw
from tests._server.api.endpoints.ws_helpers import (
HEADERS,
@@ -313,15 +314,22 @@ async def test_connects_to_existing_session_with_same_file(
# Connect second client - should connect to same session
with client.websocket_connect(ws_2) as websocket2:
- # Check in the same room
- session_manager = get_session_manager(client)
- assert len(session_manager.sessions) == 1
- assert session_manager.sessions["123"].room.size == 2
-
+ # Read the ready message before inspecting the room. Opening the
+ # websocket only waits for the server to accept the connection,
+ # which happens before the consumer joins the room. The ready
+ # message is sent after the join, so it is the earliest point at
+ # which the room membership is observable.
data2 = websocket2.receive_json()
assert_parse_ready_response(data2)
assert data2["data"]["resumed"] is True
+ # Check in the same room
+ session_manager = get_session_manager(client)
+ assert len(session_manager.sessions) == 1
+ assert (
+ session_manager.sessions[SessionId("123")].room.size == 2
+ )
+
messages2 = flush_messages(websocket2, at_least=3)
# This can/may change if implementation changes, but this is a snapshot to
# make sure it doesn't change when we don't expect it to