use qail_core::ast::Qail;
use qail_pg::PgDriver;
use std::time::Instant;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("╔═══════════════════════════════════════════════════════════╗");
println!("║ BATTLE TEST #2: Network Cut (Timeout) Test ✂️ ║");
println!("╚═══════════════════════════════════════════════════════════╝\n");
let mut driver =
PgDriver::connect_with_password("localhost", 5432, "postgres", "postgres", "postgres")
.await?;
println!("1️⃣ Connection established");
println!("\n2️⃣ Setting statement_timeout to 2000ms...");
driver.set_statement_timeout(2000).await?;
println!(" ✓ Timeout set");
println!("\n3️⃣ Running DO block with pg_sleep(5) (Should timeout in 2s)...");
let start = Instant::now();
let sleep_cmd = Qail::do_block("BEGIN PERFORM pg_sleep(5); END;", "plpgsql");
let result = driver.execute(&sleep_cmd).await;
let duration = start.elapsed();
println!("\n ⏱️ Duration: {:.2?}", duration);
match result {
Ok(_) => {
println!(" ❌ FAIL: Query completed successfully! (Should have timed out)");
println!(" Timeout logic is BROKEN.");
return Err("Test failed: No timeout occurred".into());
}
Err(e) => {
let err_str = e.to_string();
println!(" Error: {}", err_str);
if duration.as_secs() >= 5 {
println!(" ❌ FAIL: Timeout error received, BUT it waited full 5s!");
println!(" Client-side timeout didn't work / Server timeout ignored.");
return Err("Test failed: Waited too long".into());
} else if duration.as_secs_f64() < 1.9 {
println!(" ❌ FAIL: Failed too fast? ({:.2?}s)", duration);
return Err("Test failed: Too fast".into());
}
if err_str.contains("canceling statement due to statement timeout")
|| err_str.contains("57014")
{
println!("\n╔═══════════════════════════════════════════════════════════╗");
println!("║ ✅ PASS: Query interrupted by server timeout correctly! ║");
println!("╚═══════════════════════════════════════════════════════════╝");
} else {
println!("\n ⚠️ Warning: Got error, but not standard timeout message.");
println!(" Pass assuming error is related to interruption.");
}
}
}
println!("\n4️⃣ Verifying connection health after timeout...");
driver.reset_statement_timeout().await?;
let _ = driver
.fetch_all(&Qail::session_show("server_version"))
.await?;
println!(" ✓ Connection still alive");
Ok(())
}