# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import contextlib import io import os import sys import tempfile from pathlib import Path from vllm.utils.system_utils import ( _maybe_force_spawn, suppress_stdout, unique_filepath, ) def test_unique_filepath(): temp_dir = tempfile.mkdtemp() path_fn = lambda i: Path(temp_dir) / f"file_{i}.txt" paths = set() for i in range(10): path = unique_filepath(path_fn) path.write_text("test") paths.add(path) assert len(paths) == 10 assert len(list(Path(temp_dir).glob("*.txt"))) == 10 def test_numa_bind_forces_spawn(monkeypatch): monkeypatch.delenv("VLLM_WORKER_MULTIPROC_METHOD", raising=False) monkeypatch.setattr("sys.argv", ["vllm", "serve", "--numa-bind"]) _maybe_force_spawn() assert os.environ["VLLM_WORKER_MULTIPROC_METHOD"] == "spawn" def test_suppress_stdout_when_sys_stdout_has_no_fd(capfd): with contextlib.redirect_stdout(io.StringIO()), suppress_stdout(): os.write(1, b"c library output\n") assert "c library output" not in capfd.readouterr().out def test_suppress_stdout_keeps_stderr_when_sys_stdout_is_stderr(capfd, monkeypatch): monkeypatch.setattr(sys, "stdout", sys.stderr) with suppress_stdout(): os.write(1, b"c library output\n") os.write(sys.stderr.fileno(), b"error message\n") out, err = capfd.readouterr() assert "c library output" not in out assert "error message" in err