v0.3.2-pre.003

This commit is contained in:
2026-08-29 17:43:52 +02:00
parent ced653bfc0
commit a8c90107b5
13 changed files with 1193 additions and 47 deletions

View File

@@ -0,0 +1,139 @@
// file: crates/ksp-store-lib/unit_tests/settings.rs
// version: 1
fn valid_postgres_settings() -> crate::PostgresStoreSettings {
return crate::PostgresStoreSettings::new(
"postgresql://secret-user:secret-password@db.internal/ksp",
crate::PostgresPoolSettings::default(),
crate::PostgresTlsMode::VerifyFull,
crate::PostgresBootstrapSettings::default(),
);
}
#[test]
fn defaults_match_the_pre_001_runtime_bounds() {
let pool = crate::PostgresPoolSettings::default();
assert_eq!(pool.max_connections(), 8);
assert_eq!(pool.connect_timeout(), std::time::Duration::from_millis(10_000));
assert_eq!(pool.wait_timeout(), std::time::Duration::from_millis(5_000));
assert_eq!(pool.create_timeout(), std::time::Duration::from_millis(10_000));
assert_eq!(pool.recycle_timeout(), std::time::Duration::from_millis(5_000));
let bootstrap = crate::PostgresBootstrapSettings::default();
assert!(bootstrap.auto_migrate());
assert_eq!(bootstrap.migration_timeout(), std::time::Duration::from_millis(30_000));
assert_eq!(bootstrap.migration_lock_timeout(), std::time::Duration::from_millis(10_000));
let store = crate::StoreSettings::with_default_shutdown(crate::StoreBackendSettings::Postgres(valid_postgres_settings()));
assert_eq!(store.shutdown_timeout(), std::time::Duration::from_millis(5_000));
assert_eq!(store.backend_kind(), crate::StoreBackendKind::Postgres);
return;
}
#[test]
fn exact_runtime_boundaries_validate_and_adjacent_values_are_rejected() {
let minimum_pool = crate::PostgresPoolSettings::new(
1,
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
);
assert!(minimum_pool.validate().is_ok());
let maximum_pool = crate::PostgresPoolSettings::new(
64,
std::time::Duration::from_millis(60_000),
std::time::Duration::from_millis(60_000),
std::time::Duration::from_millis(60_000),
std::time::Duration::from_millis(60_000),
);
assert!(maximum_pool.validate().is_ok());
assert!(
crate::PostgresPoolSettings::new(
0,
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
)
.validate()
.is_err()
);
assert!(
crate::PostgresPoolSettings::new(
65,
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
)
.validate()
.is_err()
);
assert!(
crate::PostgresPoolSettings::new(
1,
std::time::Duration::from_millis(99),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
)
.validate()
.is_err()
);
assert!(
crate::PostgresPoolSettings::new(
1,
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(60_001),
std::time::Duration::from_millis(100),
std::time::Duration::from_millis(100),
)
.validate()
.is_err()
);
let minimum_bootstrap = crate::PostgresBootstrapSettings::new(false, std::time::Duration::from_millis(1_000), std::time::Duration::from_millis(100));
assert!(minimum_bootstrap.validate().is_ok());
let maximum_bootstrap = crate::PostgresBootstrapSettings::new(true, std::time::Duration::from_millis(300_000), std::time::Duration::from_millis(120_000));
assert!(maximum_bootstrap.validate().is_ok());
assert!(
crate::PostgresBootstrapSettings::new(true, std::time::Duration::from_millis(999), std::time::Duration::from_millis(100),)
.validate()
.is_err()
);
assert!(
crate::PostgresBootstrapSettings::new(true, std::time::Duration::from_millis(1_000), std::time::Duration::from_millis(120_001),)
.validate()
.is_err()
);
return;
}
#[test]
fn store_shutdown_bound_is_independent_from_backend_and_rejects_outside_values() {
let valid = crate::StoreSettings::new(crate::StoreBackendSettings::Postgres(valid_postgres_settings()), std::time::Duration::from_millis(100));
assert!(valid.validate().is_ok());
let invalid = crate::StoreSettings::new(crate::StoreBackendSettings::Postgres(valid_postgres_settings()), std::time::Duration::from_millis(30_001));
let error = invalid.validate().err();
assert_eq!(error.map(|value| value.code()), std::option::Option::Some(crate::ERROR_CODE_SETTINGS_INVALID));
return;
}
#[test]
fn connection_uri_is_required_but_never_rendered_by_debug_or_validation_error() {
let secret = "postgresql://secret-user:secret-password@db.internal/ksp";
let settings = valid_postgres_settings();
let debug = format!("{settings:?}");
assert!(!debug.contains(secret));
assert!(!debug.contains("secret-user"));
assert!(!debug.contains("secret-password"));
assert!(debug.contains("<redacted>"));
let empty = crate::PostgresStoreSettings::new(
std::string::String::new(),
crate::PostgresPoolSettings::default(),
crate::PostgresTlsMode::Disabled,
crate::PostgresBootstrapSettings::default(),
);
let error = empty.validate().err();
assert_eq!(error.as_ref().map(|value| value.code()), std::option::Option::Some(crate::ERROR_CODE_SETTINGS_INVALID));
assert!(!format!("{:?}", error).contains(secret));
return;
}

View File

@@ -0,0 +1,55 @@
// file: crates/ksp-store-lib/unit_tests/store.rs
// version: 1
fn poll_ready<T>(future: impl std::future::Future<Output = T>) -> T {
let mut future = std::boxed::Box::pin(future);
let waker = std::task::Waker::noop();
let mut context = std::task::Context::from_waker(waker);
return match std::future::Future::poll(future.as_mut(), &mut context) {
std::task::Poll::Ready(value) => value,
std::task::Poll::Pending => panic!("pre.003 Store contract future unexpectedly became pending before any I/O exists"),
};
}
fn valid_store_settings() -> crate::StoreSettings {
let postgres = crate::PostgresStoreSettings::new(
"postgresql://secret-user:secret-password@db.internal/ksp",
crate::PostgresPoolSettings::default(),
crate::PostgresTlsMode::VerifyFull,
crate::PostgresBootstrapSettings::default(),
);
return crate::StoreSettings::with_default_shutdown(crate::StoreBackendSettings::Postgres(postgres));
}
#[test]
fn invalid_settings_are_rejected_before_backend_dispatch() {
let postgres = crate::PostgresStoreSettings::new(
std::string::String::new(),
crate::PostgresPoolSettings::default(),
crate::PostgresTlsMode::Disabled,
crate::PostgresBootstrapSettings::default(),
);
let settings = crate::StoreSettings::with_default_shutdown(crate::StoreBackendSettings::Postgres(postgres));
let result = poll_ready(crate::Store::open(settings));
let error = result.err();
assert_eq!(error.map(|value| value.code()), std::option::Option::Some(crate::ERROR_CODE_SETTINGS_INVALID));
return;
}
#[cfg(feature = "postgres")]
#[test]
fn compiled_postgres_dispatch_does_not_fake_readiness_before_connection_materialization() {
let result = poll_ready(crate::Store::open(valid_store_settings()));
let error = result.err();
assert_eq!(error.map(|value| value.code()), std::option::Option::Some(crate::ERROR_CODE_BACKEND_OPEN_FAILED));
return;
}
#[cfg(not(feature = "postgres"))]
#[test]
fn known_postgres_without_feature_is_rejected_before_io() {
let result = poll_ready(crate::Store::open(valid_store_settings()));
let error = result.err();
assert_eq!(error.map(|value| value.code()), std::option::Option::Some(crate::ERROR_CODE_BACKEND_NOT_COMPILED));
return;
}