fix: use dynamic ports in tests to avoid port conflicts#21
fix: use dynamic ports in tests to avoid port conflicts#21LuigimonSoft merged 2 commits intomasterfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes port conflicts in tests by implementing dynamic port allocation. The server now binds to an ephemeral port (port 0) and returns the actual assigned address, allowing tests to run concurrently without port conflicts.
Key changes:
- Modified server to bind to port 0 and return the actual assigned address
- Refactored integration tests to use dynamic ports instead of a shared server instance
- Added static file serving capability with corresponding tests
Reviewed Changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/server.rs | Updated to bind on ephemeral port and return actual address |
| src/test/integration_test.rs | Refactored to spawn individual servers per test with dynamic ports |
| src/test/static_files_test.rs | Added new test file for static file serving functionality |
| src/config.rs | Added static_dir configuration field |
| src/controllers/mod.rs | Updated error handling to use Rejection instead of Infallible |
| src/router.rs | Moved error recovery to server level |
| public/index.html | Added test HTML file for static file serving |
| README.md | Updated documentation about Swagger UI asset downloads |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| .clone() | ||
| .and(warp::path("api-doc.json")) | ||
| .and(warp::get()) | ||
| .and_then(|| async { Ok::<_, Rejection>(warp::reply::json(&swagger::ApiDoc::openapi())) }); |
There was a problem hiding this comment.
The closure is unnecessarily complex. Since warp::reply::json() already returns a valid reply, you can simplify this to .map(|| warp::reply::json(&swagger::ApiDoc::openapi()))
| .and_then(|| async { Ok::<_, Rejection>(warp::reply::json(&swagger::ApiDoc::openapi())) }); | |
| .map(|| warp::reply::json(&swagger::ApiDoc::openapi())); |
|
|
||
| async fn spawn_server() -> (oneshot::Sender<()>, String) { | ||
| std::env::set_var("PORT", "0"); | ||
| let (shutdown, base) = run_server().await; |
There was a problem hiding this comment.
Setting environment variables in tests can cause race conditions when tests run in parallel. Consider passing port 0 directly to the server configuration instead of relying on environment variables.
| let (shutdown, base) = run_server().await; | |
| // Pass port 0 directly to run_server to avoid race conditions. | |
| let (shutdown, base) = run_server(0).await; |
Summary
SWAGGER_UI_DOWNLOAD_URLTesting
cargo testhttps://chatgpt.com/codex/tasks/task_e_68a15462425883209ee6af91b9702e42