[router] Implement HTTP Dependency Injection Pattern for Router System (#8714)
This commit is contained in:
@@ -83,12 +83,12 @@ impl TestContext {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
// Clone config for the closure
|
||||
let config_clone = config.clone();
|
||||
// Create app context
|
||||
let app_context = common::create_test_context(config.clone());
|
||||
|
||||
// Create router using sync factory in a blocking context
|
||||
let router =
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&config_clone))
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&app_context))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -1433,9 +1433,12 @@ mod pd_mode_tests {
|
||||
cors_allowed_origins: vec![],
|
||||
};
|
||||
|
||||
// Create app context
|
||||
let app_context = common::create_test_context(config);
|
||||
|
||||
// Create router - this might fail due to health check issues
|
||||
let router_result =
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&config))
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&app_context))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -1,2 +1,15 @@
|
||||
pub mod mock_worker;
|
||||
pub mod test_app;
|
||||
|
||||
use sglang_router_rs::config::RouterConfig;
|
||||
use sglang_router_rs::server::AppContext;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Helper function to create AppContext for tests
|
||||
pub fn create_test_context(config: RouterConfig) -> Arc<AppContext> {
|
||||
Arc::new(AppContext::new(
|
||||
config.clone(),
|
||||
reqwest::Client::new(),
|
||||
config.max_concurrent_requests,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use reqwest::Client;
|
||||
use sglang_router_rs::{
|
||||
config::RouterConfig,
|
||||
routers::RouterTrait,
|
||||
server::{build_app, AppState},
|
||||
server::{build_app, AppContext, AppState},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -13,13 +13,17 @@ pub fn create_test_app(
|
||||
client: Client,
|
||||
router_config: &RouterConfig,
|
||||
) -> Router {
|
||||
// Create AppState with the test router
|
||||
// Create AppContext
|
||||
let app_context = Arc::new(AppContext::new(
|
||||
router_config.clone(),
|
||||
client,
|
||||
router_config.max_concurrent_requests,
|
||||
));
|
||||
|
||||
// Create AppState with the test router and context
|
||||
let app_state = Arc::new(AppState {
|
||||
router,
|
||||
client,
|
||||
_concurrency_limiter: Arc::new(tokio::sync::Semaphore::new(
|
||||
router_config.max_concurrent_requests,
|
||||
)),
|
||||
context: app_context,
|
||||
});
|
||||
|
||||
// Configure request ID headers (use defaults if not specified)
|
||||
|
||||
@@ -53,10 +53,12 @@ impl TestContext {
|
||||
|
||||
config.mode = RoutingMode::Regular { worker_urls };
|
||||
|
||||
let router = tokio::task::spawn_blocking(move || RouterFactory::create_router(&config))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let app_context = common::create_test_context(config);
|
||||
let router =
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&app_context))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let router = Arc::from(router);
|
||||
|
||||
if !workers.is_empty() {
|
||||
|
||||
@@ -54,10 +54,12 @@ impl TestContext {
|
||||
|
||||
config.mode = RoutingMode::Regular { worker_urls };
|
||||
|
||||
let router = tokio::task::spawn_blocking(move || RouterFactory::create_router(&config))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let app_context = common::create_test_context(config);
|
||||
let router =
|
||||
tokio::task::spawn_blocking(move || RouterFactory::create_router(&app_context))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let router = Arc::from(router);
|
||||
|
||||
if !workers.is_empty() {
|
||||
|
||||
@@ -181,7 +181,10 @@ mod test_pd_routing {
|
||||
};
|
||||
|
||||
// Router creation will fail due to health checks, but config should be valid
|
||||
let result = RouterFactory::create_router(&config);
|
||||
let app_context =
|
||||
sglang_router_rs::server::AppContext::new(config, reqwest::Client::new(), 64);
|
||||
let app_context = std::sync::Arc::new(app_context);
|
||||
let result = RouterFactory::create_router(&app_context);
|
||||
assert!(result.is_err());
|
||||
let error_msg = result.unwrap_err();
|
||||
// Error should be about health/timeout, not configuration
|
||||
|
||||
Reference in New Issue
Block a user