Files
codex-proxy-rs/backend/crates/gateway-store/tests/config.rs
T

125 lines
4.2 KiB
Rust

//! 验证存储配置的备份路径与连接池容量约束
use gateway_store::{StoreConfig, StoreError, StorePoolConfig};
const PASSWORD: &str = "111111111111111111111111111111111111111111111111";
#[test]
fn store_config_should_derive_backup_staging_from_runtime_data_dir() {
let root = tempfile::tempdir().expect("runtime data root");
let mut config = config_with_pool(serde_json::json!({}));
config
.resolve_and_validate(root.path())
.expect("valid Store configuration");
assert_eq!(
config.backup_staging_dir(),
root.path().join("backup-staging")
);
}
#[test]
fn store_pool_config_defaults_reserve_capacity_for_observability() {
let pool: StorePoolConfig =
serde_json::from_value(serde_json::json!({})).expect("default pool configuration");
assert_eq!(pool, StorePoolConfig::default());
assert_eq!(pool.max_connections, 20);
assert_eq!(pool.acquire_timeout_seconds, 5);
assert_eq!(pool.observability_timeout_seconds, 120);
assert_eq!(pool.analytics_projection_work_mem_mb, None);
assert_eq!(pool.observability_max_connections(), 16);
assert_eq!(
StorePoolConfig {
max_connections: 50,
..pool
}
.observability_max_connections(),
40
);
}
#[test]
fn store_config_rejects_a_pool_that_cannot_reserve_both_traffic_classes() {
let root = tempfile::tempdir().expect("runtime data root");
let mut config: StoreConfig = serde_json::from_value(serde_json::json!({
"database": {
"url": "postgres://codex_proxy@127.0.0.1:5432/codex_proxy",
"password": PASSWORD,
},
"redis": {
"url": "redis://127.0.0.1:6379/",
"password": PASSWORD,
},
"pool": {
"max_connections": 1,
},
}))
.expect("syntactically valid Store configuration");
let error = config
.resolve_and_validate(root.path())
.expect_err("one connection cannot serve both traffic classes");
assert!(matches!(
error,
StoreError::InvalidData { message, .. }
if message.contains("max_connections must be at least 2")
));
}
fn config_with_pool(pool: serde_json::Value) -> StoreConfig {
serde_json::from_value(serde_json::json!({
"database": {
"url": "postgres://codex_proxy@127.0.0.1:5432/codex_proxy",
"password": PASSWORD,
},
"redis": {
"url": "redis://127.0.0.1:6379/",
"password": PASSWORD,
},
"pool": pool,
}))
.expect("test Store configuration")
}
#[test]
fn analytics_projection_work_mem_accepts_whole_mb_within_postgres_limits() {
let root = tempfile::tempdir().unwrap();
for mb in [1_u32, 8, 2_097_151] {
config_with_pool(serde_json::json!({"analytics_projection_work_mem_mb": mb}))
.resolve_and_validate(root.path())
.unwrap();
}
for mb in [0_u32, 2_097_152] {
let error = config_with_pool(serde_json::json!({"analytics_projection_work_mem_mb": mb}))
.resolve_and_validate(root.path())
.unwrap_err();
assert!(
matches!(error, StoreError::InvalidData { message, .. } if message.contains("analytics_projection_work_mem_mb"))
);
}
}
#[test]
fn store_config_accepts_custom_observability_timeout_and_rejects_invalid_values() {
let root = tempfile::tempdir().unwrap();
for seconds in [1_u64, 47, 180] {
let pool: StorePoolConfig = serde_json::from_value(serde_json::json!({
"observability_timeout_seconds": seconds,
}))
.unwrap();
assert_eq!(pool.observability_timeout_seconds, seconds);
}
for seconds in [0_u64, u64::MAX] {
let mut config: serde_json::Value = serde_json::json!({
"database": { "url": "postgres://codex_proxy@127.0.0.1:5432/codex_proxy", "password": PASSWORD },
"redis": { "url": "redis://127.0.0.1:6379/", "password": PASSWORD },
});
config["pool"] = serde_json::json!({"observability_timeout_seconds": seconds});
let mut config: StoreConfig = serde_json::from_value(config).unwrap();
assert!(config.resolve_and_validate(root.path()).is_err());
}
}