#![allow(dead_code)]
use anyhow::Result;
use secrecy::SecretString;
use sqlx::PgPool;
use std::env;
pub fn get_test_dsn() -> String {
env::var("PG_EXPORTER_DSN")
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/postgres".to_string())
}
pub async fn create_test_pool() -> Result<PgPool> {
let dsn = get_test_dsn();
let pool = PgPool::connect(&dsn).await?;
Ok(pool)
}
pub fn get_test_dsn_secret() -> SecretString {
SecretString::from(get_test_dsn())
}
pub fn get_available_port() -> u16 {
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
let port = listener
.local_addr()
.expect("Failed to get local addr")
.port();
assert!(port > 1024, "Assigned port {} should be > 1024", port);
port
}
pub async fn wait_for_server(port: u16, max_attempts: u32) -> bool {
use tokio::time::{Duration, sleep};
for attempt in 1..=max_attempts {
if tokio::net::TcpStream::connect(format!("localhost:{}", port))
.await
.is_ok()
{
return true;
}
if attempt % 10 == 0 {
eprintln!(
"Still waiting for server on port {} (attempt {}/{})",
port, attempt, max_attempts
);
}
sleep(Duration::from_millis(100)).await;
}
eprintln!(
"Failed to connect to server on port {} after {} attempts",
port, max_attempts
);
false
}
pub fn get_test_url(port: u16) -> String {
format!("http://localhost:{}", port)
}