|
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
3 | 3 |
|
4 | 4 | import json |
| 5 | +import os |
5 | 6 | import time |
6 | 7 |
|
7 | 8 | import pytest |
@@ -771,31 +772,35 @@ async def test_output_messages_enabled(client: OpenAI, model_name: str, |
771 | 772 |
|
772 | 773 | @pytest.fixture(scope="module") |
773 | 774 | def server_with_mock(monkeypatch_module: pytest.MonkeyPatch, tmp_path_factory): |
774 | | - import tempfile |
775 | 775 | import textwrap |
776 | 776 |
|
777 | 777 | args = ["--enforce-eager", "--tool-server", "demo"] |
778 | 778 |
|
779 | | - # Create a temporary startup script that patches render_for_completion |
| 779 | + # Create a sitecustomize.py that patches render_for_completion |
| 780 | + # Python automatically imports sitecustomize on startup if it's in sys.path |
780 | 781 | tmp_dir = tmp_path_factory.mktemp("test_setup") |
781 | | - startup_script = tmp_dir / "mock_startup.py" |
782 | | - startup_script.write_text(textwrap.dedent(""" |
783 | | - import sys |
784 | | - from unittest.mock import patch |
785 | | -
|
786 | | - # Mock render_for_completion to return a large token list |
787 | | - def mock_render_for_completion(messages): |
788 | | - return list(range(1000000)) # Return 1M tokens for testing |
789 | | -
|
790 | | - # Patch it at module level before it's imported |
791 | | - patch('vllm.entrypoints.harmony_utils.render_for_completion', |
792 | | - mock_render_for_completion).start() |
| 782 | + sitecustomize = tmp_dir / "sitecustomize.py" |
| 783 | + sitecustomize.write_text(textwrap.dedent(""" |
| 784 | + import os |
| 785 | + if os.environ.get('VLLM_TEST_MOCK_LARGE_PROMPT') == '1': |
| 786 | + from unittest.mock import patch |
| 787 | +
|
| 788 | + # Mock render_for_completion to return a large token list |
| 789 | + def mock_render_for_completion(messages): |
| 790 | + return list(range(1000000)) # Return 1M tokens for testing |
| 791 | +
|
| 792 | + # Patch it at module level before it's imported |
| 793 | + patch('vllm.entrypoints.harmony_utils.render_for_completion', |
| 794 | + mock_render_for_completion).start() |
793 | 795 | """)) |
794 | 796 |
|
795 | 797 | with monkeypatch_module.context() as m: |
796 | 798 | m.setenv("VLLM_ENABLE_RESPONSES_API_STORE", "1") |
797 | | - # Use PYTHONSTARTUP to run our mock setup before the server starts |
798 | | - m.setenv("PYTHONSTARTUP", str(startup_script)) |
| 799 | + m.setenv("VLLM_TEST_MOCK_LARGE_PROMPT", "1") |
| 800 | + # Add tmp_dir to PYTHONPATH so sitecustomize.py is found |
| 801 | + current_pythonpath = os.environ.get("PYTHONPATH", "") |
| 802 | + new_pythonpath = f"{tmp_dir}:{current_pythonpath}" if current_pythonpath else str(tmp_dir) |
| 803 | + m.setenv("PYTHONPATH", new_pythonpath) |
799 | 804 | with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: |
800 | 805 | yield remote_server |
801 | 806 |
|
|
0 commit comments