Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

5.5 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Export wait must time out on inactivity; an absolute deadline kills busy exports mid-write."""

from __future__ import annotations

import sys
import types
from pathlib import Path

import pytest


_BACKEND_DIR = Path(__file__).resolve().parent.parent
if str(_BACKEND_DIR) not in sys.path:
    sys.path.insert(0, str(_BACKEND_DIR))

TIMEOUT = 10.0
ONE_HOUR = 3600.0
READ_SECONDS = 4.0


@pytest.fixture
def waiting_orchestrator(monkeypatch):
    """(orchestrator, clock, script) on a fake clock; a read past the end of *script* is a quiet one."""
    import time as real_time

    from core.export import orchestrator as orchestrator_module

    clock = types.SimpleNamespace(now = 0.0)
    # swap the module reference: patching time.monotonic would freeze every thread's clock
    fake_time = types.SimpleNamespace(monotonic = lambda: clock.now, time = real_time.time)
    monkeypatch.setattr(orchestrator_module, "time", fake_time)

    orch = orchestrator_module.ExportOrchestrator()
    script: list = []

    def fake_read(timeout = None):
        clock.now += min(READ_SECONDS, timeout) if timeout is not None else READ_SECONDS
        return script.pop(0) if script else None

    monkeypatch.setattr(orch, "_read_resp", fake_read)
    monkeypatch.setattr(orch, "_ensure_subprocess_alive", lambda: True)
    return orch, clock, script


def test_a_worker_that_keeps_logging_survives_past_the_timeout(waiting_orchestrator) -> None:
    orch, clock, script = waiting_orchestrator
    script.extend(
        [
            {"type": "log", "stream": "stdout", "line": f"writing shard {n}", "ts": 0.0}
            for n in range(6)
        ]
    )
    script.append({"type": "export_merged_done", "path": "/out/model"})

    resp = orch._wait_response("export_merged_done", timeout = TIMEOUT)

    assert resp["type"] == "export_merged_done"
    assert clock.now > TIMEOUT, "the fixture must run the wait past the timeout to be meaningful"


def test_a_status_message_also_resets_the_deadline(waiting_orchestrator) -> None:
    orch, clock, script = waiting_orchestrator
    script.extend(
        [{"type": "status", "message": f"Quantizing block {n}", "ts": 0.0} for n in range(6)]
    )
    script.append({"type": "export_gguf_done", "path": "/out/model.gguf"})

    assert orch._wait_response("export_gguf_done", timeout = TIMEOUT)["type"] == "export_gguf_done"
    assert clock.now > TIMEOUT


def test_a_quiet_worker_still_times_out(waiting_orchestrator) -> None:
    orch, clock, _script = waiting_orchestrator

    with pytest.raises(RuntimeError):
        orch._wait_response("export_merged_done", timeout = TIMEOUT)

    assert clock.now < TIMEOUT * 2, "a quiet wait must end near the timeout, not run on"


def test_max_wait_caps_a_chatty_wait(waiting_orchestrator) -> None:
    """Cleanup must fail fast even while the worker keeps logging, since the log gate never closes."""
    orch, clock, script = waiting_orchestrator
    script.extend(
        [
            {"type": "log", "stream": "stdout", "line": f"freeing buffer {n}", "ts": 0.0}
            for n in range(50)
        ]
    )

    with pytest.raises(RuntimeError, match = "gave up after"):
        orch._wait_response("cleanup_done", timeout = TIMEOUT, max_wait = TIMEOUT)

    assert clock.now < TIMEOUT * 2, "the cap must hold regardless of how much the worker prints"
    assert script, "the wait must give up with the worker still talking, not drain the script"


def test_cleanup_passes_a_hard_cap(monkeypatch) -> None:
    """The cap belongs to the cleanup call site, not just to _wait_response."""
    from core.export import orchestrator as orchestrator_module

    seen: list = []
    orch = orchestrator_module.ExportOrchestrator()
    monkeypatch.setattr(orch, "_ensure_subprocess_alive", lambda: True)
    monkeypatch.setattr(orch, "_send_cmd", lambda cmd: None)
    monkeypatch.setattr(orch, "_shutdown_subprocess", lambda *a, **k: True)
    monkeypatch.setattr(
        orch,
        "_wait_response",
        lambda expected_type, timeout = None, max_wait = None: (
            seen.append((timeout, max_wait)) or {"success": True}
        ),
    )

    cap = orchestrator_module._CLEANUP_TIMEOUT
    assert orch.cleanup_memory() is True
    assert seen == [(cap, cap)], seen


def test_a_multi_quant_export_is_allowed_to_stay_silent_for_the_whole_batch(monkeypatch) -> None:
    """The silence budget scales with quant count, since a batch export prints nothing while it runs."""
    from core.export import orchestrator as orchestrator_module

    # injected per test: a module-level sys.modules entry would shadow the real one suite-wide
    tv_stub = types.ModuleType("utils.transformers_version")
    tv_stub.sidecar_swap_in_progress = lambda: False
    tv_stub.SidecarSwapInProgress = type("SidecarSwapInProgress", (RuntimeError,), {})
    monkeypatch.setitem(sys.modules, "utils.transformers_version", tv_stub)

    seen: list = []

    def record(expected_type, timeout = None):
        seen.append(timeout)
        return {"success": True, "message": "", "output_path": None}

    orch = orchestrator_module.ExportOrchestrator()
    monkeypatch.setattr(orch, "_ensure_subprocess_alive", lambda: True)
    monkeypatch.setattr(orch, "_send_cmd", lambda cmd: None)
    monkeypatch.setattr(orch, "_wait_response", record)

    orch._run_export("gguf", {"quantization_method": "Q4_K_M"})
    orch._run_export("gguf", {"quantization_method": ["Q4_K_M"] * 12})

    assert seen == [ONE_HOUR, ONE_HOUR * 12], seen