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
test: fix heartbeat races and ASGI CI hangs (#10835)
Myles Scolnick34c088a6ae39
diff --git a/tests/_plugins/stateless/test_mpl.py b/tests/_plugins/stateless/test_mpl.pyindex 277c8fc159..a18f5846ea 100644--- a/tests/_plugins/stateless/test_mpl.py+++ b/tests/_plugins/stateless/test_mpl.py@@ -1,5 +1,6 @@from __future__ import annotations+from typing import TYPE_CHECKINGfrom unittest.mock import MagicMock, patchimport pytest@@ -9,6 +10,24 @@ from marimo._runtime.commands import DeleteCellCommandfrom marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProvider+if TYPE_CHECKING:+ from collections.abc import Iterator++[email protected](autouse=True)+def marimo_mpl_backend() -> Iterator[None]:+ matplotlib = pytest.importorskip("matplotlib")+ backend = matplotlib.get_backend()+ # Imports outside a notebook can leave the native GUI backend active.+ matplotlib.use("module://marimo._output.mpl")+ try:+ yield+ finally:+ from matplotlib import pyplot as plt++ plt.close("all")+ matplotlib.use(backend)+@pytest.mark.requires("matplotlib")def test_mpl_interactive_repr_png() -> None:@@ -64,17 +83,25 @@ async def test_mpl_interactive(k: Kernel, exec_req: ExecReqProvider) -> None:@pytest.mark.requires("matplotlib")async def test_mpl_show(k: Kernel, exec_req: ExecReqProvider) -> None:- await k.run(- [- exec_req.get(- """- import matplotlib.pyplot as plt- plt.plot([1, 2])- plt.show()- """- )- ]- )+ from marimo._output.mpl import _internal_show++ with patch(+ "marimo._output.mpl._internal_show", wraps=_internal_show+ ) as show:+ await k.run(+ [+ exec_req.get(+ """+ import matplotlib.pyplot as plt+ plt.plot([1, 2])+ plt.show()+ """+ )+ ]+ )++ assert not k.errors+ show.assert_called_once()@pytest.mark.requires("matplotlib")diff --git a/tests/_runtime/watch/test_watch.py b/tests/_runtime/watch/test_watch.pyindex b4ecc44235..31f7897efd 100644--- a/tests/_runtime/watch/test_watch.py+++ b/tests/_runtime/watch/test_watch.py@@ -7,6 +7,8 @@ import pytestfrom marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProvider+pytestmark = pytest.mark.usefixtures("cleanup_watchers")+@pytest.mark.xfail(True, reason="Flaky in CI, can't repro locally", strict=Falsediff --git a/tests/_save/test_hash.py b/tests/_save/test_hash.pyindex d1a6a7017f..5ecbc083bb 100644--- a/tests/_save/test_hash.py+++ b/tests/_save/test_hash.py@@ -1626,6 +1626,7 @@ class TestSideEffects:assert hashes[0] != hashes[1]@staticmethod+ @pytest.mark.usefixtures("cleanup_watchers")async def test_side_effect_file(k: Kernel, exec_req: ExecReqProvider, tmp_path) -> None:@@ -1691,6 +1692,7 @@ class TestSideEffects:assert non_primitive[1] == 1 == v@staticmethod+ @pytest.mark.usefixtures("cleanup_watchers")async def test_side_effect_directory(k: Kernel, exec_req: ExecReqProvider, tmp_path) -> None:@@ -1757,6 +1759,7 @@ class TestSideEffects:assert non_primitive[1] == 1 == v@staticmethod+ @pytest.mark.usefixtures("cleanup_watchers")async def test_side_effect_file_ref(k: Kernel, exec_req: ExecReqProvider, tmp_path) -> None:diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.pyindex 75d01db2aa..334b5447f3 100644--- a/tests/_server/files/test_os_file_system.py+++ b/tests/_server/files/test_os_file_system.py@@ -1,6 +1,7 @@from __future__ import annotationsimport sys+from contextlib import nullcontextfrom pathlib import Pathfrom typing import Literalfrom unittest.mock import Mock, patch@@ -838,8 +839,9 @@ def test_search_reads_metadata_only_for_selected_matches(if disappears:entries[-1].stat.side_effect = FileNotFoundError- with patch("marimo._server.files.os_file_system.os.scandir") as scandir:- scandir.return_value.__enter__.return_value = iter(entries)+ # Keep concurrent directory watchers on the real os.scandir.+ with patch("marimo._server.files.os_file_system.os", wraps=os) as mock_os:+ mock_os.scandir.return_value = nullcontext(iter(entries))results = fs.search("report", path=str(test_dir), limit=1)assert [result.name for result in results] == (diff --git a/tests/_server/test_asgi.py b/tests/_server/test_asgi.pyindex c850dc27fa..9791447190 100644--- a/tests/_server/test_asgi.py+++ b/tests/_server/test_asgi.py@@ -19,6 +19,7 @@ from marimo._server.asgi import (DynamicDirectoryMiddleware,create_asgi_app,)+from tests._server.conftest import join_kernel_thread_tasksif TYPE_CHECKING:from starlette.requests import Request@@ -412,18 +413,26 @@ class TestASGIAppBuilder(unittest.TestCase):path="/", root=self.app1, middleware=[AuthMiddleware]).build()- client = TestClient(marimo_app)-- # HTTP request should succeed (middleware sets user)- response = client.get("/")- assert response.status_code == 200- assert "http" in invoked_for-- # WebSocket should also have user/meta in scope.- with client.websocket_connect("/ws?session_id=test123") as ws:- data = ws.receive_text()- assert data # kernel-ready message-+ auth_app = builder._app_cache[self.app1]+ session_manager = auth_app.app.state.session_manager+ with TestClient(marimo_app) as client:+ try:+ # HTTP request should succeed (middleware sets user)+ response = client.get("/")+ assert response.status_code == 200+ assert "http" in invoked_for++ # WebSocket should also have user/meta in scope.+ with client.websocket_connect("/ws?session_id=test123") as ws:+ data = ws.receive_text()+ assert data # kernel-ready message+ finally:+ # Mounted apps do not run their own lifespan cleanup.+ # Shut down sessions before the client's event loop closes.+ assert client.portal is not None+ client.portal.call(join_kernel_thread_tasks, session_manager)++ assert not session_manager.sessionsassert "websocket" in invoked_fordiff --git a/tests/_session/extensions/test_extensions.py b/tests/_session/extensions/test_extensions.pyindex 823135b39d..6efbdd75f6 100644--- a/tests/_session/extensions/test_extensions.py+++ b/tests/_session/extensions/test_extensions.py@@ -68,7 +68,8 @@ class TestHeartbeatExtension:task = extension.heartbeat_taskextension.on_detach()- await asyncio.sleep(0.1)+ with pytest.raises(asyncio.CancelledError):+ await taskassert task.cancelled()@@ -85,9 +86,17 @@ class TestHeartbeatExtension:),)extension = HeartbeatExtension()+ closed = asyncio.Event()+ mock_session.close.side_effect = closed.setextension.on_attach(mock_session, event_bus)- await asyncio.sleep(1.5)+ try:+ await asyncio.wait_for(closed.wait(), timeout=10)+ finally:+ extension.on_detach()+ assert extension.heartbeat_task is not None+ with pytest.raises(asyncio.CancelledError):+ await extension.heartbeat_taskmock_session.close.assert_called_once()# A persistent banner is broadcast before the session closes, so the@@ -99,7 +108,6 @@ class TestHeartbeatExtension:assert banner.action == "restart"assert "out of memory" in banner.descriptionassert "restart" in banner.description.lower()- extension.on_detach()class TestCachingExtension:diff --git a/tests/conftest.py b/tests/conftest.pyindex 31eee79422..459e5bf560 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -37,7 +37,8 @@ from tests._runtime._helpers.streams import (_MockStream = MockStreamif TYPE_CHECKING:- from collections.abc import Generator+ import threading+ from collections.abc import Callable, Generatorfrom types import ModuleType, TracebackTypefrom typing_extensions import Self@@ -277,6 +278,41 @@ def patch_random_seed(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(UIElement, "_random_seed", random.Random(42))+def cleanup_watchers(+ monkeypatch: pytest.MonkeyPatch,+) -> Generator[None, None, None]:+ from marimo._runtime.threads import Thread+ from marimo._runtime.watch import _directory, _file, _path++ watchers: list[tuple[Thread, threading.Event]] = []++ def track_thread(+ *,+ target: Callable[..., None],+ args: tuple[Path, _path.PathState, threading.Event],+ daemon: bool,+ ) -> Thread:+ thread = Thread(target=target, args=args, daemon=daemon)+ watchers.append((thread, args[2]))+ return thread++ monkeypatch.setattr(_path, "Thread", track_thread)+ # Restore intervals changed by notebook cells after each test.+ monkeypatch.setattr(_file, "_TEST_SLEEP_INTERVAL", None)+ monkeypatch.setattr(_directory, "_TEST_SLEEP_INTERVAL", None)+ try:+ yield+ finally:+ # The watcher target holds its PathState alive, so __del__ cannot+ # stop the thread when the test kernel clears its globals.+ for _, should_exit in watchers:+ should_exit.set()+ for thread, _ in watchers:+ thread.join(timeout=5)+ assert not thread.is_alive()++@dataclasses.dataclassclass MockedKernel:"""Should only be created in fixtures b/c inits a runtime context."""