[Test] Cleanup for helpers (#6523)

Signed-off-by: wangyu <410167048@qq.com>
Signed-off-by: [Your Name] <your.email@example.com>
Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
wangyuandamy-why-3459 authored and GitHub committed 2026-08-25 09:10:17 +08:00
1 parent 8360346a66
commit 4af0ddc1c2
140 files changed
+4121 -4194

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Opt-in cleanup fixtures for GPU memory, pipeline registry, and speaker cache."""
from __future__ import annotations
import pytest
from vllm_omni.config.pipeline_registry import OMNI_PIPELINES
@pytest.fixture
def clean_gpu_memory_between_tests():
"""Opt-in GPU pre/post hooks for a test (no environment-variable gate).
Use as a test parameter or ``@pytest.mark.usefixtures("clean_gpu_memory_between_tests")``.
"""
from tests.helpers.clean import cleanup_test_environment
print("\n=== PRE-TEST DEVICE CLEANUP ===")
cleanup_test_environment()
yield
cleanup_test_environment()
@pytest.fixture
def clean_pipeline_registry():
"""Ensure the OMNI_PIPELINES are in a clean state for a test that mutates it."""
snapshot = dict(OMNI_PIPELINES)
yield
OMNI_PIPELINES.clear()
OMNI_PIPELINES.update(snapshot)
@pytest.fixture
def clean_speaker_cache():
"""Reset the process-wide speaker cache singleton before and after the test."""
import vllm_omni.utils.speaker_cache as _sc
def _reset():
with _sc._SINGLETON_LOCK:
if _sc._SINGLETON is not None:
_sc._SINGLETON.clear()
_sc._SINGLETON = None
_reset()
yield
_reset()
+42 -9
View File
@@ -1,14 +1,47 @@
"""Fixtures config creation & out of tree registry management."""
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Session-scoped config and environment fixtures."""
from __future__ import annotations
import os
import pytest
from vllm_omni.config.pipeline_registry import OMNI_PIPELINES
import torch
from vllm.config import DeviceConfig, VllmConfig, set_current_vllm_config
@pytest.fixture
def clean_pipeline_registry():
"""Ensure the OMNI_PIPELINES are in a clean state for a test that mutates it."""
snapshot = dict(OMNI_PIPELINES)
@pytest.fixture(scope="session", autouse=True)
def default_env():
# Keep behavior but avoid import-time side effects (RFC #2299).
keys = ("VLLM_WORKER_MULTIPROC_METHOD", "VLLM_TARGET_DEVICE")
previous = {key: os.environ.get(key) for key in keys}
os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = previous["VLLM_WORKER_MULTIPROC_METHOD"] or "spawn"
os.environ["VLLM_TARGET_DEVICE"] = previous["VLLM_TARGET_DEVICE"] or (
"cuda" if torch.cuda.is_available() and torch.accelerator.device_count() > 0 else "cpu"
)
yield
OMNI_PIPELINES.clear()
OMNI_PIPELINES.update(snapshot)
for key, value in previous.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
@pytest.fixture(scope="session", autouse=True)
def default_vllm_config():
"""Set a default VllmConfig for the whole test session.
Session scope ensures module-scoped fixtures (e.g. ``omni_runner``) and
deferred imports of ``tests.helpers.runtime`` both see the same context.
Function-scoped autouse ran too late for ``OmniRunner`` setup and could
desynchronize vLLM init vs request preprocessing (e.g. renderer state).
"""
# Use CPU device if no GPU is available (e.g., in CI environments)
has_gpu = torch.cuda.is_available() and torch.accelerator.device_count() > 0
device = "cuda" if has_gpu else "cpu"
device_config = DeviceConfig(device=device)
with set_current_vllm_config(VllmConfig(device_config=device_config)):
yield
-74
View File
@@ -1,74 +0,0 @@
import os
import pytest
import torch
from vllm.distributed import parallel_state
@pytest.fixture(scope="session", autouse=True)
def default_env():
# Keep behavior but avoid import-time side effects (RFC #2299).
keys = ("VLLM_WORKER_MULTIPROC_METHOD", "VLLM_TARGET_DEVICE")
previous = {key: os.environ.get(key) for key in keys}
os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = previous["VLLM_WORKER_MULTIPROC_METHOD"] or "spawn"
os.environ["VLLM_TARGET_DEVICE"] = previous["VLLM_TARGET_DEVICE"] or (
"cuda" if torch.cuda.is_available() and torch.accelerator.device_count() > 0 else "cpu"
)
yield
for key, value in previous.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
@pytest.fixture(scope="session")
def model_prefix() -> str:
prefix = os.environ.get("MODEL_PREFIX", "")
return f"{prefix.rstrip('/')}/" if prefix else ""
@pytest.fixture
def clean_gpu_memory_between_tests():
"""Opt-in GPU pre/post hooks for a test (no environment-variable gate).
Use as a test parameter or ``@pytest.mark.usefixtures("clean_gpu_memory_between_tests")``.
"""
from tests.helpers.env import run_post_test_cleanup, run_pre_test_cleanup
print("\n=== PRE-TEST GPU CLEANUP ===")
run_pre_test_cleanup()
yield
run_post_test_cleanup()
@pytest.fixture(scope="session", autouse=True)
def default_vllm_config():
"""Set a default VllmConfig for the whole test session.
Session scope ensures module-scoped fixtures (e.g. ``omni_runner``) and
deferred imports of ``tests.helpers.runtime`` both see the same context.
Function-scoped autouse ran too late for ``OmniRunner`` setup and could
desynchronize vLLM init vs request preprocessing (e.g. renderer state).
"""
from vllm.config import DeviceConfig, VllmConfig, set_current_vllm_config
# Use CPU device if no GPU is available (e.g., in CI environments)
has_gpu = torch.cuda.is_available() and torch.accelerator.device_count() > 0
device = "cuda" if has_gpu else "cpu"
device_config = DeviceConfig(device=device)
with set_current_vllm_config(VllmConfig(device_config=device_config)):
yield
@pytest.fixture()
def init_fake_tp_group(mocker):
"""Provide a fake TP group so vllm linear layers can be instantiated."""
mock_tp = mocker.MagicMock()
mock_tp.world_size = 1
mock_tp.rank_in_group = 0
old = parallel_state._TP
parallel_state._TP = mock_tp
yield
parallel_state._TP = old
+49 -24
View File
@@ -1,10 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Runtime fixtures (OmniRunner / OmniServer). Imports are deferred to fixture time.
Loading ``tests.helpers.runtime`` at plugin import time (before session fixtures)
pulls in vLLM/vllm_omni too early and breaks initialization order vs the legacy
monolithic conftest. Defer imports until fixtures run so ``default_env`` /
``default_vllm_config`` run first. Implementation helpers live in
``tests.helpers.runtime`` (``iter_omni_server`` / ``iter_omni_runner``).
monolithic conftest. Defer imports until fixtures run so session fixtures in
``tests.helpers.fixtures.config`` run first. Server/runner helpers live in
``tests.helpers.runtime``; request clients live in ``tests.helpers.client``.
"""
from __future__ import annotations
@@ -25,15 +28,14 @@ omni_fixture_lock = threading.Lock()
def omni_server_function(
request: pytest.FixtureRequest,
run_level: str,
model_prefix: str,
) -> Generator[OmniServer, Any, None]:
from tests.helpers.runtime import iter_omni_server
yield from iter_omni_server(request, run_level, model_prefix, omni_fixture_lock)
yield from iter_omni_server(request, run_level, omni_fixture_lock)
@pytest.fixture(scope="module")
def omni_server(request: pytest.FixtureRequest, run_level: str, model_prefix: str) -> Generator[OmniServer, Any, None]:
def omni_server(request: pytest.FixtureRequest, run_level: str) -> Generator[OmniServer, Any, None]:
"""Start vLLM-Omni through the standard or stage-CLI launcher.
The fixture stays module-scoped because multi-stage initialization is costly.
@@ -42,16 +44,16 @@ def omni_server(request: pytest.FixtureRequest, run_level: str, model_prefix: st
"""
from tests.helpers.runtime import iter_omni_server
yield from iter_omni_server(request, run_level, model_prefix, omni_fixture_lock)
yield from iter_omni_server(request, run_level, omni_fixture_lock)
@pytest.fixture
def openai_client(request: pytest.FixtureRequest, run_level: str):
def online_client(request: pytest.FixtureRequest, run_level: str):
"""Resolve ``omni_server`` lazily so parametrized server fixtures work like upstream."""
from tests.helpers.runtime import OpenAIClientHandler
from tests.helpers.client import OnlineOmniClient
server = request.getfixturevalue("omni_server")
return OpenAIClientHandler(
return OnlineOmniClient(
host=server.host,
port=server.port,
api_key="EMPTY",
@@ -61,12 +63,18 @@ def openai_client(request: pytest.FixtureRequest, run_level: str):
@pytest.fixture
def openai_client_function(request: pytest.FixtureRequest, run_level: str):
def openai_client(online_client):
"""Historical alias for :func:`online_client`."""
return online_client
@pytest.fixture
def online_client_function(request: pytest.FixtureRequest, run_level: str):
"""Resolve ``omni_server_function`` lazily for function-scoped reliability tests."""
from tests.helpers.runtime import OpenAIClientHandler
from tests.helpers.client import OnlineOmniClient
server = request.getfixturevalue("omni_server_function")
return OpenAIClientHandler(
return OnlineOmniClient(
host=server.host,
port=server.port,
api_key="EMPTY",
@@ -75,10 +83,15 @@ def openai_client_function(request: pytest.FixtureRequest, run_level: str):
)
@pytest.fixture
def openai_client_function(online_client_function):
"""Historical alias for :func:`online_client_function`."""
return online_client_function
@pytest.fixture(scope="function")
def omni_runner_function(
request: pytest.FixtureRequest,
model_prefix: str,
run_level: str,
) -> Generator[OmniRunner, Any, None]:
"""Function-scoped :class:`~tests.helpers.runtime.OmniRunner` (cf. :func:`omni_server_function`).
@@ -88,30 +101,42 @@ def omni_runner_function(
"""
from tests.helpers.runtime import iter_omni_runner
yield from iter_omni_runner(request, model_prefix, run_level, omni_fixture_lock)
yield from iter_omni_runner(request, run_level, omni_fixture_lock)
@pytest.fixture(scope="module")
def omni_runner(request: pytest.FixtureRequest, model_prefix: str, run_level: str) -> Generator[OmniRunner, Any, None]:
def omni_runner(request: pytest.FixtureRequest, run_level: str) -> Generator[OmniRunner, Any, None]:
"""Module-scoped :class:`~tests.helpers.runtime.OmniRunner` (cf. :func:`omni_server`).
Reuses one runner for the whole module to amortize multi-stage init cost.
"""
from tests.helpers.runtime import iter_omni_runner
yield from iter_omni_runner(request, model_prefix, run_level, omni_fixture_lock)
yield from iter_omni_runner(request, run_level, omni_fixture_lock)
@pytest.fixture
def omni_runner_handler_function(omni_runner_function: OmniRunner):
"""Resolve :class:`~tests.helpers.runtime.OmniRunnerHandler` for :func:`omni_runner_function`."""
from tests.helpers.runtime import OmniRunnerHandler
def offline_client_function(omni_runner_function: OmniRunner):
"""Resolve :class:`~tests.helpers.client.OfflineOmniClient` for :func:`omni_runner_function`."""
from tests.helpers.client import OfflineOmniClient
return OmniRunnerHandler(omni_runner_function)
return OfflineOmniClient(omni_runner_function)
@pytest.fixture
def omni_runner_handler(omni_runner: OmniRunner):
from tests.helpers.runtime import OmniRunnerHandler
def omni_runner_handler_function(offline_client_function):
"""Historical alias for :func:`offline_client_function`."""
return offline_client_function
return OmniRunnerHandler(omni_runner)
@pytest.fixture
def offline_client(omni_runner: OmniRunner):
from tests.helpers.client import OfflineOmniClient
return OfflineOmniClient(omni_runner)
@pytest.fixture
def omni_runner_handler(offline_client):
"""Historical alias for :func:`offline_client`."""
return offline_client
-21
View File
@@ -1,21 +0,0 @@
"""Fixtures for the process-wide speaker cache singleton."""
from __future__ import annotations
import pytest
@pytest.fixture
def fresh_speaker_cache():
"""Reset the process-wide speaker cache singleton before and after the test."""
import vllm_omni.utils.speaker_cache as _sc
def _reset():
with _sc._SINGLETON_LOCK:
if _sc._SINGLETON is not None:
_sc._SINGLETON.clear()
_sc._SINGLETON = None
_reset()
yield
_reset()