[CI]【Hackathon 9th Sprint No.40】功能模块 fastdeploy/entrypoints/openai/api_server.py 单测补充#5567
[CI]【Hackathon 9th Sprint No.40】功能模块 fastdeploy/entrypoints/openai/api_server.py 单测补充#5567CSWYF3634076 merged 26 commits intoPaddlePaddle:developfrom
Conversation
|
Thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive unit test coverage for the fastdeploy/entrypoints/openai/api_server.py module as part of Hackathon 9th Sprint No.40. The tests focus on previously uncovered code branches using mocks to isolate the API server logic from heavy dependencies.
Key changes:
- Adds 546 lines of test coverage for api_server.py
- Tests include lifecycle management, connection handling, route handlers, and server launchers
- Uses extensive mocking to test various error paths and edge cases
| @@ -0,0 +1,546 @@ | |||
| """ | |||
There was a problem hiding this comment.
PR标题中存在文字重复问题:"功能模块 f功能模块"。建议修改为:"[CI]【Hackathon 9th Sprint No.40】fastdeploy/entrypoints/openai/api_server.py 单测补充" 或 "[CI]【Hackathon 9th Sprint No.40】功能模块 fastdeploy/entrypoints/openai/api_server.py 单测补充"。
For external contributors: The PR title contains a duplication issue in "功能模块 f功能模块". Please revise to either "[CI]【Hackathon 9th Sprint No.40】fastdeploy/entrypoints/openai/api_server.py unit test coverage" or remove the duplicated text.
| api_server = _reload_api_server(args) | ||
|
|
||
| class SlowSemaphore: | ||
| async def acquire(self): |
There was a problem hiding this comment.
The sleep time of 0.01 seconds is intentionally longer than the connection_manager timeout of 0.001 seconds to trigger the timeout exception. Consider adding a brief comment explaining this relationship for better test maintainability.
| async def acquire(self): | |
| async def acquire(self): | |
| # The sleep time of 0.01 seconds is intentionally longer than the | |
| # connection_manager timeout of 0.001 seconds to trigger the timeout exception. |
| assert ping_resp.status_code == 304 # line 323 | ||
|
|
||
| routes = api_server.list_all_routes() | ||
| assert isinstance(routes, dict) and routes["routes"] # lines 309-317 |
There was a problem hiding this comment.
The assertion on line 267 could be more explicit. Consider splitting it into two separate assertions for better test failure diagnostics: assert isinstance(routes, dict) followed by assert "routes" in routes and routes["routes"]. This makes it clearer which specific condition failed if the test breaks.
| assert isinstance(routes, dict) and routes["routes"] # lines 309-317 | |
| assert isinstance(routes, dict) | |
| assert "routes" in routes and routes["routes"] # lines 309-317 |
| """ | ||
| Extra coverage for `fastdeploy.entrypoints.openai.api_server`. | ||
| Tests are lightweight and mock heavy dependencies to exercise branches | ||
| that were previously uncovered. | ||
| """ |
There was a problem hiding this comment.
The file is missing the standard Apache 2.0 copyright header that is present in all other test files in this directory. Please add the copyright header at the beginning of the file, following the same format as other test files like test_api_authentication.py.
| models_resp_fail = asyncio.run(api_server.list_models()) | ||
| assert models_resp_fail.status_code == 304 # lines 433-437 | ||
|
|
||
| # list_models success ModelList branch | ||
| api_server.app.state.dynamic_load_weight = False | ||
| api_server.app.state.model_handler = MagicMock( | ||
| list_models=AsyncMock(return_value=SimpleNamespace(model_dump=lambda: {"models": [1]})) | ||
| ) | ||
| models_resp = asyncio.run(api_server.list_models()) | ||
| assert models_resp.status_code == 200 # lines 433-442 |
There was a problem hiding this comment.
The function test_controller_routes_and_models_listing uses asyncio.run to call async functions (list_models), but this is inconsistent with the test pattern used elsewhere in this file where async functions are tested with @pytest.mark.asyncio. Consider marking this test as async and using await instead of asyncio.run for consistency and to avoid potential issues with nested event loops.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #5567 +/- ##
==========================================
Coverage ? 65.50%
==========================================
Files ? 329
Lines ? 41783
Branches ? 6396
==========================================
Hits ? 27368
Misses ? 12383
Partials ? 2032
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@kesmeey 辛苦按照 Codestyle-Check / Pre Commit (pull_request) 的报错信息做下修改,然后因为有其他已知问题,后续更新 PR 时候,请 rebase 一下最新代码后重新提交~ |
f49e576 to
16a194d
Compare
|
@EmmonsCurse 可以review下_base_test.yml部分 |
EmmonsCurse
left a comment
There was a problem hiding this comment.
LGTM for download run_try~
Removed unnecessary docstring from test_api_server.py
|
0.2⭐️ |
…i_server.py 单测补充 (PaddlePaddle#5567) * Add tests for openai api_server coverage * update * Update tests for openai api_server * fix bugs * test: disable some api_server lifespan/controller tests for local env * Format test_api_server with black * update * update * test: narrow envs patch in api_server tests to avoid side effects * fix: separate MagicMock creation to avoid missing req argument * fix: patch TRACES_ENABLE env var in api_server tests * fix: use os.environ patch for TRACES_ENABLE * test: use fake fastdeploy.envs in api_server tests * test: pass fake Request into chat/completion routes * test: increase coverage for tracing and scheduler control * fix: set dynamic_load_weight in tracing headers test * ci: add retry and validation for FastDeploy.tar.gz download * ci: fix indentation in _base_test.yml * refactor: simplify test_api_server.py (807->480 lines, ~40% reduction) * fix: restore missing args attributes (revision, etc.) in _build_args * fix: patch sys.argv to prevent SystemExit: 2 in api_server tests * improve coverage * Remove docstring from test_api_server.py Removed unnecessary docstring from test_api_server.py --------- Co-authored-by: CSWYF3634076 <wangyafeng@baidu.com>
…i_server.py 单测补充 (PaddlePaddle#5567) * Add tests for openai api_server coverage * update * Update tests for openai api_server * fix bugs * test: disable some api_server lifespan/controller tests for local env * Format test_api_server with black * update * update * test: narrow envs patch in api_server tests to avoid side effects * fix: separate MagicMock creation to avoid missing req argument * fix: patch TRACES_ENABLE env var in api_server tests * fix: use os.environ patch for TRACES_ENABLE * test: use fake fastdeploy.envs in api_server tests * test: pass fake Request into chat/completion routes * test: increase coverage for tracing and scheduler control * fix: set dynamic_load_weight in tracing headers test * ci: add retry and validation for FastDeploy.tar.gz download * ci: fix indentation in _base_test.yml * refactor: simplify test_api_server.py (807->480 lines, ~40% reduction) * fix: restore missing args attributes (revision, etc.) in _build_args * fix: patch sys.argv to prevent SystemExit: 2 in api_server tests * improve coverage * Remove docstring from test_api_server.py Removed unnecessary docstring from test_api_server.py --------- Co-authored-by: CSWYF3634076 <wangyafeng@baidu.com>
Motivation
功能模块 fastdeploy/entrypoints/openai/api_server.py 单测补充
完成单测覆盖行数215-59=156
Modifications
add unittest tests/entrypoints/openai/test_api_server.py
Usage or Command
No need
Accuracy Tests
No need
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.