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.

tests: flakey tests (#3914)

Myles Scolnicke8e57706f1af
diff --git a/tests/_server/api/endpoints/test_execution.py b/tests/_server/api/endpoints/test_execution.py
index a9930c1a68..293320b662 100644
--- a/tests/_server/api/endpoints/test_execution.py
+++ b/tests/_server/api/endpoints/test_execution.py
@@ -5,6 +5,7 @@ import json
import time
from typing import TYPE_CHECKING, Any
+from marimo._types.ids import CellId_t, SessionId
from marimo._utils.lists import first
from tests._server.conftest import get_session_manager
from tests._server.mocks import token_header, with_read_session, with_session
@@ -12,7 +13,7 @@ from tests._server.mocks import token_header, with_read_session, with_session
if TYPE_CHECKING:
from starlette.testclient import TestClient
-SESSION_ID = "session-123"
+SESSION_ID = SessionId("session-123")
HEADERS = {
"Marimo-Session-Id": SESSION_ID,
**token_header("fake-token"),
@@ -353,10 +354,22 @@ class TestExecutionRoutes_RunMode:
assert app_meta_response["user"] is True
-def get_printed_object(client: TestClient, cell_id: str) -> dict[str, Any]:
+def get_printed_object(
+ client: TestClient, cell_id: CellId_t
+) -> dict[str, Any]:
session = get_session_manager(client).get_session(SESSION_ID)
assert session
- console = first(session.session_view.cell_operations[cell_id].console)
+
+ timeout = 2
+ start = time.time()
+ console = None
+ while time.time() - start < timeout:
+ if cell_id not in session.session_view.cell_operations:
+ time.sleep(0.1)
+ continue
+ console = first(session.session_view.cell_operations[cell_id].console)
+ if console:
+ break
assert console
assert isinstance(console.data, str)
return json.loads(console.data)
diff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.py
index 6f658e7379..9c241357ea 100644
--- a/tests/_server/api/endpoints/test_ws.py
+++ b/tests/_server/api/endpoints/test_ws.py
@@ -487,11 +487,11 @@ async def test_ycell_persistence(client: TestClient) -> None:
client.post("/api/kernel/shutdown", headers=HEADERS)
-async def test_ycell_cleanup_after_timeout(
+async def test_ycell_cleanup_after_close(
client: TestClient, temp_marimo_file: str
) -> None:
"""Test that cell data is cleaned up after all clients disconnect"""
- from marimo._server.api.endpoints.ws import clean_cell, ycells
+ from marimo._server.api.endpoints.ws import ycells
cell_id = "Hbol"
@@ -513,12 +513,8 @@ async def test_ycell_cleanup_after_timeout(
cell_ws.close()
# Cell should still exist
- assert key in ycells
-
- # Wait for cleanup
- await clean_cell(key, timeout=0.01)
-
- # Cell should be removed
+ # Default timeout is 0 so it should be cleaned up immediately
+ await asyncio.sleep(0.1)
assert key not in ycells
client.post("/api/kernel/shutdown", headers=HEADERS)
diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.py
index f8c5dc1bea..b0d103e330 100644
--- a/tests/_server/test_sessions.py
+++ b/tests/_server/test_sessions.py
@@ -150,7 +150,7 @@ def test_kernel_manager_edit_mode() -> None:
@save_and_restore_main
-def test_kernel_manager_interrupt(tmp_path) -> None:
+def test_kernel_manager_interrupt(tmp_path: Path) -> None:
queue_manager = QueueManager(use_multiprocessing=True)
mode = SessionMode.EDIT
@@ -174,34 +174,31 @@ def test_kernel_manager_interrupt(tmp_path) -> None:
import string
# Having trouble persisting the write to a temp file on Windows
- file = (
+ file = Path(
"".join(random.choice(string.ascii_uppercase) for _ in range(10))
+ ".txt"
)
else:
- file = str(tmp_path / "output.txt")
+ file = tmp_path / "output.txt"
- with open(file, "w") as f:
- f.write("-1")
+ Path(file).write_text("-1")
queue_manager.control_queue.put(
CreationRequest(
- execution_requests=tuple(
- [
- ExecutionRequest(
- cell_id="1",
- code=inspect.cleandoc(
- f"""
+ execution_requests=(
+ ExecutionRequest(
+ cell_id="1",
+ code=inspect.cleandoc(
+ f"""
import time
with open("{file}", 'w') as f:
f.write('0')
- time.sleep(5)
+ time.sleep(2)
with open("{file}", 'w') as f:
f.write('1')
"""
- ),
- )
- ]
+ ),
+ ),
),
set_ui_element_value_request=SetUIElementValueRequest(
object_ids=[], values=[]
@@ -210,28 +207,47 @@ def test_kernel_manager_interrupt(tmp_path) -> None:
)
)
- # give time for the file to be written to 0, but not enough for it to be
- # written to 1
- time.sleep(0.5)
+ timeout = 5
+ # Wait for the file to be written to 0
+ start_time = time.time()
+ while time.time() < start_time + timeout / 2:
+ time.sleep(0.1)
+ if file.read_text() == "0":
+ break
kernel_manager.interrupt_kernel()
try:
- with open(file) as f:
- assert f.read() == "0"
+ assert file.read_text() == "0"
# if kernel failed to interrupt, f will read as "1"
time.sleep(1.5)
- with open(file) as f:
- assert f.read() == "0"
+ assert file.read_text() == "0"
finally:
if sys.platform == "win32":
os.remove(file)
+ # Wait for queues to be empty with timeout
+ # This makes the test more resilient to timing issues in CI
+ start_time = time.time()
+ timeout = 5 # 5-second timeout for queues to be empty
+
+ # Give some time for queues to be processed first
+ time.sleep(0.5)
+
+ while time.time() - start_time < timeout:
+ if (
+ queue_manager.input_queue.empty()
+ and queue_manager.control_queue.empty()
+ ):
+ break
+ time.sleep(0.2) # slightly longer interval
+
+ # Now check if queues are empty
assert queue_manager.input_queue.empty()
assert queue_manager.control_queue.empty()
# Assert shutdown
kernel_manager.close_kernel()
- kernel_manager.kernel_task.join()
+ kernel_manager.kernel_task.join(timeout=5)
assert not kernel_manager.is_alive()