use servlet::{
App, AppConfig, Running,
health::CheckPolicy,
sql::{PgConfig, PgLimits, PgPool, PgReadiness, PgTls},
sync::oneshot,
};
use std::time::Duration;
pub(super) fn settings() -> AppConfig {
let mut config = AppConfig::default();
config.bind = "127.0.0.1:0".parse().unwrap();
config.admin_bind = Some("127.0.0.1:0".parse().unwrap());
config.startup_timeout = Duration::from_secs(3);
config.shutdown_timeout = Duration::from_millis(500);
config.cleanup_reserve = Duration::from_millis(250);
config
}
pub(super) fn config(admin: bool) -> PgConfig {
let key = if admin {
"SERVLET_TEST_PG_ADMIN_URL"
} else {
"SERVLET_TEST_PG_URL"
};
let root = std::fs::read(std::env::var("SERVLET_TEST_PG_ROOT").unwrap()).unwrap();
PgConfig::new(
&std::env::var(key).expect("run python3 -B scripts/postgres_test.py"),
"servlet-pg-test",
)
.unwrap()
.with_tls(PgTls::VerifyFullWithRoot(root))
.unwrap()
.with_limits(PgLimits {
max_connections: 2,
min_connections: 1,
acquire_timeout: Duration::from_secs(1),
..PgLimits::default()
})
.unwrap()
.with_readiness(PgReadiness::Required(CheckPolicy {
interval: Duration::from_millis(20),
timeout: Duration::from_millis(500),
}))
.unwrap()
}
pub(super) async fn start(config: PgConfig) -> (Running, PgPool) {
let (sender, receiver) = oneshot::channel();
let running = App::new("postgres-test")
.config(settings())
.setup(move |mut ctx| async move {
let pool = ctx.postgres("database", config).await?;
sender.send(pool).unwrap();
Ok(())
})
.start()
.await
.unwrap();
(running, receiver.await.unwrap())
}
pub(super) async fn stop(running: Running, pool: PgPool) {
running.handle().shutdown();
running.wait().await.unwrap();
assert!(pool.is_closed());
}