#[cfg(test)]
mod tests {
use crate::rpc::{EnhancedConnectionPool, EnhancedPoolConfig, WeightedEndpoint, LoadBalanceStrategy};
use crate::core::RpcEndpoint;
use std::time::Duration;
fn create_test_endpoints() -> Vec<RpcEndpoint> {
vec![
RpcEndpoint {
url: "https://api.mainnet-beta.solana.com".to_string(),
priority: 1,
rate_limit_rps: 100,
timeout_ms: 5000,
healthy: true,
},
RpcEndpoint {
url: "https://solana-api.projectserum.com".to_string(),
priority: 2,
rate_limit_rps: 200,
timeout_ms: 3000,
healthy: true,
},
RpcEndpoint {
url: "https://rpc.ankr.com/solana".to_string(),
priority: 3,
rate_limit_rps: 150,
timeout_ms: 4000,
healthy: true,
},
]
}
fn create_test_config() -> EnhancedPoolConfig {
EnhancedPoolConfig {
endpoints: create_test_endpoints(),
max_connections_per_endpoint: 10,
health_check_interval: Duration::from_secs(1),
circuit_breaker_threshold: 3,
circuit_breaker_timeout: Duration::from_secs(5),
enable_load_balancing: true,
request_timeout: Duration::from_secs(30),
enable_connection_multiplexing: true,
enable_compression: true,
}
}
#[tokio::test]
async fn test_enhanced_pool_creation() {
let _endpoints = create_test_endpoints();
let config = create_test_config();
assert_eq!(config.endpoints.len(), 3);
assert_eq!(config.max_connections_per_endpoint, 10);
}
#[tokio::test]
async fn test_weighted_endpoint_creation() {
let endpoint = RpcEndpoint {
url: "https://api.mainnet-beta.solana.com".to_string(),
priority: 1,
rate_limit_rps: 100,
timeout_ms: 5000,
healthy: true,
};
let weighted = WeightedEndpoint {
endpoint: endpoint.clone(),
weight: 0.5,
priority: endpoint.priority,
region: "us-east".to_string(),
response_time_ms: 100.0,
success_rate: 0.95,
last_health_check_ms: Some(1234567890),
consecutive_failures: 0,
};
assert_eq!(weighted.weight, 0.5);
assert_eq!(weighted.endpoint.healthy, true);
}
#[tokio::test]
async fn test_load_balancer_strategy() {
let strategy = LoadBalanceStrategy::RoundRobin;
assert!(matches!(strategy, LoadBalanceStrategy::RoundRobin));
let strategy = LoadBalanceStrategy::WeightedRoundRobin;
assert!(matches!(strategy, LoadBalanceStrategy::WeightedRoundRobin));
let strategy = LoadBalanceStrategy::LeastConnections;
assert!(matches!(strategy, LoadBalanceStrategy::LeastConnections));
let strategy = LoadBalanceStrategy::ResponseTime;
assert!(matches!(strategy, LoadBalanceStrategy::ResponseTime));
}
#[tokio::test]
async fn test_circuit_breaker_config() {
let config = create_test_config();
assert!(config.circuit_breaker_threshold > 0);
assert!(config.circuit_breaker_timeout > Duration::from_secs(0));
assert_eq!(config.circuit_breaker_threshold, 3);
assert_eq!(config.circuit_breaker_timeout, Duration::from_secs(5));
}
#[tokio::test]
async fn test_enhanced_pool_config_validation() {
let config = create_test_config();
assert!(!config.endpoints.is_empty());
assert!(config.max_connections_per_endpoint > 0);
assert!(config.health_check_interval > Duration::from_secs(0));
assert!(config.circuit_breaker_threshold > 0);
assert!(config.circuit_breaker_timeout > Duration::from_secs(0));
}
#[tokio::test]
async fn test_basic_pool_functionality() {
let endpoints = create_test_endpoints();
assert_eq!(endpoints.len(), 3);
for endpoint in &endpoints {
assert!(!endpoint.url.is_empty());
assert!(endpoint.priority > 0);
assert!(endpoint.rate_limit_rps > 0);
assert!(endpoint.timeout_ms > 0);
}
}
#[tokio::test]
async fn test_health_checker_configuration() {
let config = create_test_config();
assert!(config.health_check_interval > Duration::from_secs(0));
assert_eq!(config.circuit_breaker_threshold, 3);
assert!(config.circuit_breaker_timeout > Duration::from_secs(0));
}
#[tokio::test]
async fn test_connection_multiplexing_config() {
let config = create_test_config();
assert!(config.enable_connection_multiplexing);
assert!(config.enable_compression);
assert!(config.enable_load_balancing);
}
#[tokio::test]
async fn test_timeout_configuration() {
let config = create_test_config();
assert!(config.request_timeout > Duration::from_secs(0));
assert_eq!(config.request_timeout, Duration::from_secs(30));
}
#[tokio::test]
async fn test_endpoint_priority_ordering() {
let endpoints = create_test_endpoints();
let priorities: Vec<u8> = endpoints.iter().map(|e| e.priority).collect();
assert_eq!(priorities, vec![1u8, 2u8, 3u8]);
}
#[tokio::test]
async fn test_rate_limit_configuration() {
let endpoints = create_test_endpoints();
for endpoint in &endpoints {
assert!(endpoint.rate_limit_rps > 0);
assert!(endpoint.healthy);
}
}
#[tokio::test]
async fn test_pool_config_completeness() {
let config = create_test_config();
assert!(!config.endpoints.is_empty());
assert!(config.max_connections_per_endpoint > 0);
assert!(config.health_check_interval > Duration::from_secs(0));
assert!(config.circuit_breaker_threshold > 0);
assert!(config.circuit_breaker_timeout > Duration::from_secs(0));
assert!(config.enable_load_balancing);
assert!(config.request_timeout > Duration::from_secs(0));
assert!(config.enable_connection_multiplexing);
assert!(config.enable_compression);
}
#[tokio::test]
async fn test_enhanced_pool_metrics_structure() {
let config = create_test_config();
assert_eq!(config.endpoints.len(), 3);
let _pool_clone = config; assert!(true); }
}