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.

13.1 KB
Raw Blame
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations

import threading
import time
from collections import deque

from marimo._messaging.cell_output import CellChannel
from marimo._messaging.console_output_worker import (
    TIMEOUT_S,
    ConsoleMsg,
    FlushMarker,
    _add_output_to_buffer,
    _can_merge_outputs,
    _flush_outputs,
    _write_console_output,
    buffered_writer,
)
from marimo._messaging.context import run_id_context
from tests._messaging.mocks import MockStream


class TestConsoleOutputWorker:
    def test_repeated_evaluations_keep_separate_buffered_output(self) -> None:
        buffered = {}
        for execution_id in ("first", "second"):
            _add_output_to_buffer(
                ConsoleMsg(
                    CellChannel.STDOUT,
                    "cell1",
                    execution_id,
                    "text/plain",
                    run_id="same-run",
                    execution_id=execution_id,
                ),
                buffered,
            )
        stream = MockStream()
        _flush_outputs(buffered, stream)
        assert [
            (n.execution_id, n.console.data) for n in stream.cell_notifications
        ] == [
            ("first", "first"),
            ("second", "second"),
        ]

    def test_flush_keeps_source_code_and_separates_outputs_by_code(
        self,
    ) -> None:
        buffered = {}
        for code in ("x = 1", "x = 2"):
            _add_output_to_buffer(
                ConsoleMsg(
                    CellChannel.STDERR,
                    "cell1",
                    "Traceback",
                    "application/vnd.marimo+traceback",
                    run_id="same-run",
                    execution_id="same-execution",
                    code=code,
                ),
                buffered,
            )
        stream = MockStream()
        _flush_outputs(buffered, stream)
        assert [n.console.code for n in stream.cell_notifications] == [
            "x = 1",
            "x = 2",
        ]

    def test_write_console_output_omits_code_when_absent(self) -> None:
        stream = MockStream()
        _write_console_output(
            stream, CellChannel.STDERR, "cell1", "Hello", "text/plain"
        )
        assert "code" not in stream.operations[0]["console"]

    def test_buffer_preserves_run_ids_across_execution_boundaries(
        self,
    ) -> None:
        buffered = {}
        for run_id in ("first", "second"):
            with run_id_context(run_id):
                _add_output_to_buffer(
                    ConsoleMsg(
                        CellChannel.STDOUT, "cell1", run_id, "text/plain"
                    ),
                    buffered,
                )
        stream = MockStream()
        _flush_outputs(buffered, stream)
        assert [
            notification.run_id for notification in stream.cell_notifications
        ] == [
            "first",
            "second",
        ]

    def test_can_merge_outputs(self) -> None:
        # Same stream and mimetype should be mergeable
        msg1 = ConsoleMsg(
            stream=CellChannel.STDOUT,
            cell_id="cell1",
            data="Hello",
            mimetype="text/plain",
        )
        msg2 = ConsoleMsg(
            stream=CellChannel.STDOUT,
            cell_id="cell1",
            data=" World",
            mimetype="text/plain",
        )
        assert _can_merge_outputs(msg1, msg2) is True

        # Different stream should not be mergeable
        msg3 = ConsoleMsg(
            stream=CellChannel.STDERR,
            cell_id="cell1",
            data="Error",
            mimetype="text/plain",
        )
        assert _can_merge_outputs(msg1, msg3) is False

        # Different mimetype should not be mergeable
        msg4 = ConsoleMsg(
            stream=CellChannel.STDOUT,
            cell_id="cell1",
            data="<h1>Hello</h1>",
            mimetype="text/html",
        )
        assert _can_merge_outputs(msg1, msg4) is False

    def test_add_output_to_buffer_new_cell(self) -> None:
        # Test adding output for a new cell
        outputs_buffered_per_cell = {}
        msg = ConsoleMsg(
            stream=CellChannel.STDOUT,
            cell_id="cell1",
            data="Hello",
            mimetype="text/plain",
        )

        _add_output_to_buffer(msg, outputs_buffered_per_cell)

        assert "cell1" in outputs_buffered_per_cell
        assert len(outputs_buffered_per_cell["cell1"]) == 1
        assert outputs_buffered_per_cell["cell1"][0].data == "Hello"

    def test_add_output_to_buffer_merge(self) -> None:
        # Test merging output for an existing cell with same stream and mimetype
        outputs_buffered_per_cell = {
            "cell1": [
                ConsoleMsg(
                    stream=CellChannel.STDOUT,
                    cell_id="cell1",
                    data="Hello",
                    mimetype="text/plain",
                )
            ]
        }
        msg = ConsoleMsg(
            stream=CellChannel.STDOUT,
            cell_id="cell1",
            data=" World",
            mimetype="text/plain",
        )

        _add_output_to_buffer(msg, outputs_buffered_per_cell)

        assert len(outputs_buffered_per_cell["cell1"]) == 1
        assert outputs_buffered_per_cell["cell1"][0].data == "Hello World"

    def test_add_output_to_buffer_no_merge(self) -> None:
        # Test adding output for an existing cell with different stream or mimetype
        outputs_buffered_per_cell = {
            "cell1": [
                ConsoleMsg(
                    stream=CellChannel.STDOUT,
                    cell_id="cell1",
                    data="Hello",
                    mimetype="text/plain",
                )
            ]
        }
        msg = ConsoleMsg(
            stream=CellChannel.STDERR,
            cell_id="cell1",
            data="Error",
            mimetype="text/plain",
        )

        _add_output_to_buffer(msg, outputs_buffered_per_cell)

        assert len(outputs_buffered_per_cell["cell1"]) == 2
        assert outputs_buffered_per_cell["cell1"][0].data == "Hello"
        assert outputs_buffered_per_cell["cell1"][1].data == "Error"

    def test_write_console_output(self) -> None:
        # Test writing console output to stream
        stream = MockStream()
        _write_console_output(
            stream,
            CellChannel.STDOUT,
            "cell1",
            "Hello",
            "text/plain",
        )

        assert len(stream.operations) == 1
        assert stream.operations[0]["op"] == "cell-op"
        assert stream.operations[0]["cell_id"] == "cell1"
        assert stream.operations[0]["console"]["channel"] == "stdout"
        assert stream.operations[0]["console"]["mimetype"] == "text/plain"
        assert stream.operations[0]["console"]["data"] == "Hello"

    def test_buffered_writer_basic(self) -> None:
        # Test basic functionality of buffered writer
        stream = MockStream()
        msg_queue: deque[ConsoleMsg | None] = deque()
        cv = threading.Condition()

        # Start the buffered writer in a separate thread
        thread = threading.Thread(
            target=buffered_writer, args=(msg_queue, stream, cv)
        )
        thread.daemon = True
        thread.start()

        try:
            # Add a message to the queue
            with cv:
                msg_queue.append(
                    ConsoleMsg(
                        stream=CellChannel.STDOUT,
                        cell_id="cell1",
                        data="Hello",
                        mimetype="text/plain",
                    )
                )
                cv.notify()

            # Wait for the timeout to expire and the message to be written
            # Use a longer timeout to ensure the message is processed
            time.sleep(TIMEOUT_S * 5)

            for _ in range(10):
                time.sleep(0.1)
                if len(stream.operations) == 1:
                    break

            # Check that the message was written to the stream
            assert len(stream.operations) == 1
            assert stream.operations[0]["console"]["data"] == "Hello"

        finally:
            # Signal the writer to terminate
            with cv:
                msg_queue.append(None)
                cv.notify()
            thread.join(timeout=1.0)

    def test_buffered_writer_flush_marker_flushes_immediately(self) -> None:
        # A FlushMarker should cause buffered output to be written out
        # without waiting for the TIMEOUT_S batching window, and the
        # marker's done event should be set once flushing is complete.
        stream = MockStream()
        msg_queue: deque[ConsoleMsg | FlushMarker | None] = deque()
        cv = threading.Condition()

        thread = threading.Thread(
            target=buffered_writer, args=(msg_queue, stream, cv)
        )
        thread.daemon = True
        thread.start()

        try:
            marker = FlushMarker()
            with cv:
                msg_queue.append(
                    ConsoleMsg(
                        stream=CellChannel.STDOUT,
                        cell_id="cell1",
                        data="Hello",
                        mimetype="text/plain",
                    )
                )
                msg_queue.append(marker)
                cv.notify()

            # The flush should complete promptly — well before a batching
            # window would expire.  Give it generous headroom for CI jitter
            # but assert it happens faster than a multi-batch wait.
            assert marker.done.wait(timeout=1.0), (
                "FlushMarker did not signal completion within timeout"
            )
            assert len(stream.operations) == 1
            assert stream.operations[0]["console"]["data"] == "Hello"
        finally:
            with cv:
                msg_queue.append(None)
                cv.notify()
            thread.join(timeout=1.0)

    def test_buffered_writer_flush_marker_on_empty_queue(self) -> None:
        # A FlushMarker on an empty buffer should still signal completion.
        stream = MockStream()
        msg_queue: deque[ConsoleMsg | FlushMarker | None] = deque()
        cv = threading.Condition()

        thread = threading.Thread(
            target=buffered_writer, args=(msg_queue, stream, cv)
        )
        thread.daemon = True
        thread.start()

        try:
            marker = FlushMarker()
            with cv:
                msg_queue.append(marker)
                cv.notify()

            assert marker.done.wait(timeout=1.0)
            assert stream.operations == []
        finally:
            with cv:
                msg_queue.append(None)
                cv.notify()
            thread.join(timeout=1.0)

    def test_buffered_writer_multiple_messages(self) -> None:
        # Test buffered writer with multiple messages
        stream = MockStream()
        msg_queue: deque[ConsoleMsg | None] = deque()
        cv = threading.Condition()

        # Start the buffered writer in a separate thread
        thread = threading.Thread(
            target=buffered_writer, args=(msg_queue, stream, cv)
        )
        thread.daemon = True
        thread.start()

        try:
            # Add multiple messages to the queue
            with cv:
                msg_queue.append(
                    ConsoleMsg(
                        stream=CellChannel.STDOUT,
                        cell_id="cell1",
                        data="Hello",
                        mimetype="text/plain",
                    )
                )
                msg_queue.append(
                    ConsoleMsg(
                        stream=CellChannel.STDOUT,
                        cell_id="cell1",
                        data=" World",
                        mimetype="text/plain",
                    )
                )
                msg_queue.append(
                    ConsoleMsg(
                        stream=CellChannel.STDERR,
                        cell_id="cell1",
                        data="Error",
                        mimetype="text/plain",
                    )
                )
                cv.notify()

            # Wait for the timeout to expire and the messages to be written
            # Use a longer timeout to ensure the messages are processed
            time.sleep(TIMEOUT_S * 5)

            for _ in range(10):
                time.sleep(0.1)
                if len(stream.messages) == 2:
                    break

            # Check that the messages were written to the stream
            assert len(stream.messages) == 2  # Merged stdout messages + stderr

            # First message should be the merged stdout messages
            first_message = stream.operations[0]
            assert first_message["console"]["channel"] == "stdout"
            assert first_message["console"]["data"] == "Hello World"

            # Second message should be the stderr message
            second_message = stream.operations[1]
            assert second_message["console"]["channel"] == "stderr"
            assert second_message["console"]["data"] == "Error"

        finally:
            # Signal the writer to terminate
            with cv:
                msg_queue.append(None)
                cv.notify()
            thread.join(timeout=1.0)