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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
chore: deflake flaky i/o tests (#5475)
Akshay Agrawale84e9685422a
diff --git a/tests/_server/api/endpoints/test_files.py b/tests/_server/api/endpoints/test_files.pyindex ac7f13918f..b5ff8df7aa 100644--- a/tests/_server/api/endpoints/test_files.py+++ b/tests/_server/api/endpoints/test_files.py@@ -3,8 +3,9 @@ from __future__ import annotationsimport osimport random+import timefrom pathlib import Path-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Callablefrom tests._server.conftest import get_session_managerfrom tests._server.mocks import (@@ -23,6 +24,18 @@ HEADERS = {}+def _try_assert_n_times(n: int, assert_fn: Callable[[], None]) -> None:+ n_tries = 0+ while n_tries <= n - 1:+ try:+ assert_fn()+ return+ except Exception:+ n_tries += 1+ time.sleep(0.1)+ assert_fn()++@with_session(SESSION_ID)def test_rename(client: TestClient) -> None:current_filename = get_session_manager(@@ -46,7 +59,10 @@ def test_rename(client: TestClient) -> None:)assert response.json() == {"success": True}- assert new_path.exists()+ def _new_path_exists():+ assert new_path.exists()++ _try_assert_n_times(5, _new_path_exists)@with_session(SESSION_ID)@@ -83,11 +99,15 @@ def test_save_file(client: TestClient) -> None:},)assert response.status_code == 200, response.text- assert "import marimo" in response.text- file_contents = path.read_text()- assert "import marimo as mo" in file_contents- assert "@app.cell(hide_code=True)" in file_contents- assert "my_cell" in file_contents++ def _assert_contents():+ file_contents = path.read_text()+ assert "import marimo" in response.text+ assert "import marimo as mo" in file_contents+ assert "@app.cell(hide_code=True)" in file_contents+ assert "my_cell" in file_contents++ _try_assert_n_times(5, _assert_contents)# save backresponse = client.post(@@ -141,14 +161,18 @@ def test_save_with_header(client: TestClient) -> None:assert response.status_code == 200, response.textassert "import marimo" in response.text- file_contents = path.read_text()- assert "import marimo as mo" in file_contents- # Race condition with uv (seen in python 3.10)- if file_contents.startswith("# ///"):- file_contents = file_contents.split("# ///")[2].lstrip()- assert file_contents.startswith(header.rstrip()), "Header was removed"- assert "@app.cell(hide_code=True)" in file_contents- assert "my_cell" in file_contents++ def _assert_contents():+ file_contents = path.read_text()+ assert "import marimo as mo" in file_contents+ # Race condition with uv (seen in python 3.10)+ if file_contents.startswith("# ///"):+ file_contents = file_contents.split("# ///")[2].lstrip()+ assert file_contents.startswith(header.rstrip()), "Header was removed"+ assert "@app.cell(hide_code=True)" in file_contents+ assert "my_cell" in file_contents++ _try_assert_n_times(5, _assert_contents)@with_session(SESSION_ID)@@ -188,14 +212,20 @@ def test_save_with_invalid_file(client: TestClient) -> None:assert response.status_code == 200, response.textassert "import marimo" in response.text- file_contents = path.read_text()- assert "@app.cell(hide_code=True)" in file_contents- assert "my_cell" in file_contents- # Race condition with uv (seen in python 3.10)- if file_contents.startswith("# ///"):- file_contents = file_contents.split("# ///")[2].lstrip()- assert file_contents.startswith("import marimo"), "Header was not removed"+ def _assert_contents():+ file_contents = path.read_text()+ assert "@app.cell(hide_code=True)" in file_contents+ assert "my_cell" in file_contents++ # Race condition with uv (seen in python 3.10)+ if file_contents.startswith("# ///"):+ file_contents = file_contents.split("# ///")[2].lstrip()+ assert file_contents.startswith("import marimo"), (+ "Header was not removed"+ )++ _try_assert_n_times(5, _assert_contents)@with_session(SESSION_ID)@@ -228,8 +258,11 @@ def test_save_app_config(client: TestClient) -> None:assert filenamepath = Path(filename)- file_contents = path.read_text()- assert 'marimo.App(width="medium"' not in file_contents+ def _wait_for_file_reset():+ file_contents = path.read_text()+ assert 'marimo.App(width="medium"' not in file_contents++ _try_assert_n_times(5, _wait_for_file_reset)response = client.post("/api/kernel/save_app_config",@@ -240,8 +273,12 @@ def test_save_app_config(client: TestClient) -> None:)assert response.status_code == 200, response.textassert "import marimo" in response.text- file_contents = path.read_text()- assert 'marimo.App(width="medium"' in file_contents++ def _assert_contents():+ file_contents = path.read_text()+ assert 'marimo.App(width="medium"' in file_contents++ _try_assert_n_times(5, _assert_contents)@with_session(SESSION_ID)@@ -266,9 +303,13 @@ def test_copy_file(client: TestClient) -> None:)assert response.status_code == 200, response.textassert filename_copy in response.text- file_contents = open(copied_file).read()- assert "import marimo as mo" in file_contents- assert 'marimo.App(width="full"' in file_contents++ def _assert_contents():+ file_contents = open(copied_file).read()+ assert "import marimo as mo" in file_contents+ assert 'marimo.App(width="full"' in file_contents++ _try_assert_n_times(5, _assert_contents)@with_websocket_session(SESSION_ID)diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.pyindex 7c71c3b849..a44fe97b7a 100644--- a/tests/_server/export/test_exporter.py+++ b/tests/_server/export/test_exporter.py@@ -1,5 +1,6 @@from __future__ import annotations+import asyncioimport jsonimport sysfrom typing import TYPE_CHECKING, Any@@ -461,8 +462,23 @@ async def test_run_until_completion_with_console_output(mock_echo: MagicMock):argv=None,)assert did_error is False- mock_echo.assert_any_call("hello stdout", file=sys.stderr, nl=False)- mock_echo.assert_any_call("hello stderr", file=sys.stderr, nl=False)++ def _assert_contents():+ mock_echo.assert_any_call("hello stdout", file=sys.stderr, nl=False)+ mock_echo.assert_any_call("hello stderr", file=sys.stderr, nl=False)++ n_tries = 0+ limit = 5+ while n_tries <= limit:+ try:+ _assert_contents()+ break+ except Exception:+ n_tries += 1+ await asyncio.sleep(0.1)+ if n_tries > limit:+ _assert_contents()+cell_ops = [op for op in session_view.operations if isinstance(op, CellOp)]snapshot("run_until_completion_with_console_output.txt",