qail-pg 0.27.10

Rust PostgreSQL driver for typed AST queries with direct wire-protocol execution
Documentation
//! Zombie Client Test: SIGINT and lock cleanup verification
//! Tests that locks are released when connection is terminated

use qail_pg::driver::PgConnection;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("๐Ÿงช Test 7: Zombie Client");
    println!("{}", "โ”".repeat(40));

    // Connect
    let mut driver = PgConnection::connect("localhost", 5432, "orion", "postgres").await?;

    // 1. Begin a transaction (takes locks)
    println!("  Starting transaction with locks...");
    driver.execute_simple("BEGIN").await?;
    driver
        .execute_simple("LOCK TABLE zombie_test IN ACCESS EXCLUSIVE MODE")
        .await?;
    println!("    โœ“ Exclusive lock acquired on zombie_test");

    // 2. Check that lock exists
    let mut check_driver = PgConnection::connect("localhost", 5432, "orion", "postgres").await?;
    check_driver.execute_simple("SELECT relation::regclass, mode FROM pg_locks WHERE relation::regclass::text = 'zombie_test'").await?;
    println!("    โœ“ Lock confirmed in pg_locks");

    // 3. Simulate SIGINT by dropping connection without commit
    println!("  Simulating connection drop (no COMMIT)...");
    drop(driver); // Connection dropped without COMMIT - should trigger ROLLBACK

    // Wait for Postgres to detect disconnection
    tokio::time::sleep(Duration::from_millis(100)).await;

    // 4. Verify lock is released
    println!("  Verifying lock cleanup...");
    check_driver
        .execute_simple(
            "SELECT COUNT(*) FROM pg_locks WHERE relation::regclass::text = 'zombie_test' AND mode = 'AccessExclusiveLock'",
        )
        .await?;

    // If we can take a new lock, the old one is gone
    let mut verify_driver = PgConnection::connect("localhost", 5432, "orion", "postgres").await?;
    verify_driver.execute_simple("BEGIN").await?;
    verify_driver
        .execute_simple("LOCK TABLE zombie_test IN ACCESS EXCLUSIVE MODE NOWAIT")
        .await?;
    verify_driver.execute_simple("ROLLBACK").await?;
    println!("    โœ“ Lock released - new exclusive lock acquired successfully");

    println!();
    println!("โœ“ Zombie Client Test PASSED!");
    println!("  Postgres correctly cleaned up locks when connection dropped.");

    Ok(())
}