mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2026-04-23 00:17:25 +08:00
[BugFix][Optimization] Replace silent failures with catchable exceptions and informative error messages (#6533)
* init * init * fix format * add * add files * add ut * fix some * add ut * add more * add * fix pre-commit * fix pre-commit * fix cover * skip long seq * add * add * fix * remove not need * fix set attr * fix comments * fix comments * fix failed tests --------- Co-authored-by: gongweibao <gognweibao@baidu.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""
|
||||
Tests for InputPreprocessor.create_processor().
|
||||
|
||||
Why mock:
|
||||
- ModelConfig, ReasoningParserManager, ToolParserManager, and concrete processor
|
||||
classes all depend on model files or external resources not available in tests.
|
||||
We mock them at the import boundary to test InputPreprocessor's routing logic.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
def _make_model_config(arch, enable_mm=False):
|
||||
cfg = SimpleNamespace(
|
||||
model="test_model",
|
||||
architectures=[arch],
|
||||
enable_mm=enable_mm,
|
||||
)
|
||||
return cfg
|
||||
|
||||
|
||||
class TestInputPreprocessorBranching(unittest.TestCase):
|
||||
"""Test that create_processor picks the right processor class based on architecture and flags."""
|
||||
|
||||
def test_init_stores_params(self):
|
||||
from fastdeploy.input.preprocess import InputPreprocessor
|
||||
|
||||
config = _make_model_config("LlamaForCausalLM")
|
||||
pp = InputPreprocessor(
|
||||
model_config=config,
|
||||
reasoning_parser="qwen3",
|
||||
tool_parser="ernie_x1",
|
||||
limit_mm_per_prompt={"image": 2},
|
||||
)
|
||||
self.assertEqual(pp.model_name_or_path, "test_model")
|
||||
self.assertEqual(pp.reasoning_parser, "qwen3")
|
||||
self.assertEqual(pp.tool_parser, "ernie_x1")
|
||||
self.assertEqual(pp.limit_mm_per_prompt, {"image": 2})
|
||||
|
||||
def test_create_processor_text_normal_path(self):
|
||||
"""Normal path: non-Ernie, non-MM arch creates a text DataProcessor."""
|
||||
from fastdeploy.input.preprocess import InputPreprocessor
|
||||
|
||||
config = _make_model_config("LlamaForCausalLM", enable_mm=False)
|
||||
pp = InputPreprocessor(model_config=config)
|
||||
|
||||
mock_dp = MagicMock()
|
||||
with (
|
||||
patch.dict("sys.modules", {"fastdeploy.plugins": None, "fastdeploy.plugins.input_processor": None}),
|
||||
patch("fastdeploy.input.preprocess.envs") as mock_envs,
|
||||
patch("fastdeploy.input.text_processor.DataProcessor", return_value=mock_dp),
|
||||
):
|
||||
mock_envs.ENABLE_V1_DATA_PROCESSOR = False
|
||||
pp.create_processor()
|
||||
|
||||
self.assertIs(pp.processor, mock_dp)
|
||||
|
||||
def test_unsupported_mm_arch_raises(self):
|
||||
"""When enable_mm=True and arch is unrecognized, should raise ValueError."""
|
||||
from fastdeploy.input.preprocess import InputPreprocessor
|
||||
|
||||
config = _make_model_config("UnknownMMArch", enable_mm=True)
|
||||
pp = InputPreprocessor(model_config=config)
|
||||
|
||||
with patch.dict("sys.modules", {"fastdeploy.plugins": None, "fastdeploy.plugins.input_processor": None}):
|
||||
with self.assertRaises(ValueError):
|
||||
pp.create_processor()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -99,3 +99,63 @@ async def test_encode_timeout():
|
||||
|
||||
with pytest.raises(TimeoutError):
|
||||
await client.encode_image(request)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_encode_invalid_type():
|
||||
"""Test invalid encode type raises ValueError (line 130).
|
||||
NOTE: Public methods hardcode the type param, so we test the private method directly
|
||||
to verify the validation boundary."""
|
||||
base_url = "http://testserver"
|
||||
client = AsyncTokenizerClient(base_url=base_url)
|
||||
|
||||
request = ImageEncodeRequest(
|
||||
version="v1", req_id="req_invalid", is_gen=False, resolution=256, image_url="http://example.com/image.jpg"
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid encode type"):
|
||||
await client._async_encode_request("invalid_type", request.model_dump())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decode_invalid_type():
|
||||
"""Test invalid decode type raises ValueError (line 186).
|
||||
NOTE: Public methods hardcode the type param, so we test the private method directly
|
||||
to verify the validation boundary."""
|
||||
base_url = "http://testserver"
|
||||
client = AsyncTokenizerClient(base_url=base_url)
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid decode type"):
|
||||
await client._async_decode_request("invalid_type", {})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_encode_network_error_continues_polling():
|
||||
"""Test network error during polling is caught and logged (line 164)."""
|
||||
base_url = "http://testserver"
|
||||
client = AsyncTokenizerClient(base_url=base_url, max_wait=2, poll_interval=0.1)
|
||||
|
||||
# Mock create task
|
||||
respx.post(f"{base_url}/image/encode").mock(
|
||||
return_value=httpx.Response(200, json={"code": 0, "task_tag": "task_network_error"})
|
||||
)
|
||||
|
||||
# First poll fails with network error, second succeeds
|
||||
call_count = 0
|
||||
|
||||
def side_effect(request):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
raise httpx.RequestError("Network error")
|
||||
return httpx.Response(200, json={"state": "Finished", "result": {"key": "value"}})
|
||||
|
||||
respx.get(f"{base_url}/encode/get").mock(side_effect=side_effect)
|
||||
|
||||
request = ImageEncodeRequest(
|
||||
version="v1", req_id="req_network", is_gen=False, resolution=256, image_url="http://example.com/image.jpg"
|
||||
)
|
||||
|
||||
result = await client.encode_image(request)
|
||||
assert result["key"] == "value"
|
||||
|
||||
Reference in New Issue
Block a user