|
| 1 | +use rust::setup::{admin_sqlx, admin_tokio}; |
| 2 | +use serial_test::serial; |
| 3 | +use sqlx::{Executor, Row}; |
| 4 | +use std::time::Duration; |
| 5 | +use tokio::time::sleep; |
| 6 | +use tokio_postgres::NoTls; |
| 7 | + |
| 8 | +async fn connection_tokio(db: &str) -> tokio_postgres::Client { |
| 9 | + let (client, connection) = tokio_postgres::connect( |
| 10 | + &format!( |
| 11 | + "host=127.0.0.1 user=pgdog dbname={} password=pgdog port=6432", |
| 12 | + db |
| 13 | + ), |
| 14 | + NoTls, |
| 15 | + ) |
| 16 | + .await |
| 17 | + .unwrap(); |
| 18 | + |
| 19 | + tokio::spawn(async move { |
| 20 | + if let Err(e) = connection.await { |
| 21 | + eprintln!("connection error: {}", e); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + client |
| 26 | +} |
| 27 | + |
| 28 | +async fn get_pool_ids(database: &str) -> (i64, i64) { |
| 29 | + let admin = admin_sqlx().await; |
| 30 | + let pools = admin.fetch_all("SHOW POOLS").await.unwrap(); |
| 31 | + |
| 32 | + let primary_id = pools |
| 33 | + .iter() |
| 34 | + .find(|p| { |
| 35 | + p.get::<String, &str>("database") == database |
| 36 | + && p.get::<String, &str>("user") == "pgdog" |
| 37 | + && p.get::<String, &str>("role") == "primary" |
| 38 | + }) |
| 39 | + .map(|p| p.get::<i64, &str>("id")) |
| 40 | + .unwrap(); |
| 41 | + |
| 42 | + let replica_id = pools |
| 43 | + .iter() |
| 44 | + .find(|p| { |
| 45 | + p.get::<String, &str>("database") == database |
| 46 | + && p.get::<String, &str>("user") == "pgdog" |
| 47 | + && p.get::<String, &str>("role") == "replica" |
| 48 | + }) |
| 49 | + .map(|p| p.get::<i64, &str>("id")) |
| 50 | + .unwrap(); |
| 51 | + |
| 52 | + (primary_id, replica_id) |
| 53 | +} |
| 54 | + |
| 55 | +async fn ban_pools(database: &str) { |
| 56 | + let admin = admin_sqlx().await; |
| 57 | + let (primary_id, replica_id) = get_pool_ids(database).await; |
| 58 | + |
| 59 | + admin |
| 60 | + .execute(format!("BAN {}", primary_id).as_str()) |
| 61 | + .await |
| 62 | + .unwrap(); |
| 63 | + admin |
| 64 | + .execute(format!("BAN {}", replica_id).as_str()) |
| 65 | + .await |
| 66 | + .unwrap(); |
| 67 | +} |
| 68 | + |
| 69 | +async fn unban_pools(database: &str) { |
| 70 | + let admin = admin_sqlx().await; |
| 71 | + let (primary_id, replica_id) = get_pool_ids(database).await; |
| 72 | + |
| 73 | + admin |
| 74 | + .execute(format!("UNBAN {}", primary_id).as_str()) |
| 75 | + .await |
| 76 | + .unwrap(); |
| 77 | + admin |
| 78 | + .execute(format!("UNBAN {}", replica_id).as_str()) |
| 79 | + .await |
| 80 | + .unwrap(); |
| 81 | +} |
| 82 | + |
| 83 | +#[tokio::test] |
| 84 | +#[serial] |
| 85 | +async fn test_client_connection_recovery_default() { |
| 86 | + let admin = admin_tokio().await; |
| 87 | + |
| 88 | + // Ensure recover mode (default) |
| 89 | + admin |
| 90 | + .simple_query("SET client_connection_recovery TO 'recover'") |
| 91 | + .await |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + // Give pools time to reinitialize |
| 95 | + sleep(Duration::from_millis(200)).await; |
| 96 | + |
| 97 | + // Connection that we'll test recovery on |
| 98 | + let conn = connection_tokio("pgdog").await; |
| 99 | + |
| 100 | + // Ban both primary and replica pools to force Banned error |
| 101 | + ban_pools("pgdog").await; |
| 102 | + |
| 103 | + // Give ban time to take effect |
| 104 | + sleep(Duration::from_millis(50)).await; |
| 105 | + |
| 106 | + // This query should fail with Banned error because both pools are banned |
| 107 | + conn.simple_query("BEGIN").await.unwrap(); |
| 108 | + let result = conn.simple_query("SELECT 1").await; |
| 109 | + assert!(result.is_err(), "Expected error when pools are banned"); |
| 110 | + let err = result.unwrap_err(); |
| 111 | + let err_str = err.to_string().to_lowercase(); |
| 112 | + assert!( |
| 113 | + err_str.contains("all replicas down"), |
| 114 | + "Expected 'all replicas down' error, got: {}", |
| 115 | + err |
| 116 | + ); |
| 117 | + |
| 118 | + // Unban pools |
| 119 | + unban_pools("pgdog").await; |
| 120 | + |
| 121 | + // Give unban time to take effect |
| 122 | + sleep(Duration::from_millis(100)).await; |
| 123 | + |
| 124 | + // In recovery mode, the connection should still be usable |
| 125 | + let result = conn.simple_query("BEGIN").await; |
| 126 | + assert!( |
| 127 | + result.is_ok(), |
| 128 | + "Expected query to succeed after recovery, got: {:?}", |
| 129 | + result.err() |
| 130 | + ); |
| 131 | + let result = conn.simple_query("SELECT 1").await; |
| 132 | + assert!( |
| 133 | + result.is_ok(), |
| 134 | + "Expected SELECT to succeed after recovery, got: {:?}", |
| 135 | + result.err() |
| 136 | + ); |
| 137 | + conn.simple_query("COMMIT").await.unwrap(); |
| 138 | + |
| 139 | + // Reset settings |
| 140 | + admin.simple_query("RELOAD").await.unwrap(); |
| 141 | +} |
| 142 | + |
| 143 | +#[tokio::test] |
| 144 | +#[serial] |
| 145 | +async fn test_client_connection_recovery_drop() { |
| 146 | + let admin = admin_tokio().await; |
| 147 | + |
| 148 | + // Set drop mode - client should be disconnected on recoverable errors |
| 149 | + admin |
| 150 | + .simple_query("SET client_connection_recovery TO 'drop'") |
| 151 | + .await |
| 152 | + .unwrap(); |
| 153 | + |
| 154 | + // Give pools time to reinitialize |
| 155 | + sleep(Duration::from_millis(200)).await; |
| 156 | + |
| 157 | + // Connection that we'll test drop behavior on |
| 158 | + let conn = connection_tokio("pgdog").await; |
| 159 | + |
| 160 | + // Ban both pools to force Banned error |
| 161 | + ban_pools("pgdog").await; |
| 162 | + |
| 163 | + // Give ban time to take effect |
| 164 | + sleep(Duration::from_millis(50)).await; |
| 165 | + |
| 166 | + // This query should fail because pools are banned |
| 167 | + conn.simple_query("BEGIN").await.unwrap(); |
| 168 | + let result = conn.simple_query("SELECT 1").await; |
| 169 | + assert!(result.is_err(), "Expected error when pools are banned"); |
| 170 | + |
| 171 | + // Unban pools |
| 172 | + unban_pools("pgdog").await; |
| 173 | + |
| 174 | + // Give unban time to take effect |
| 175 | + sleep(Duration::from_millis(100)).await; |
| 176 | + |
| 177 | + // In drop mode, the connection should be disconnected |
| 178 | + // Subsequent queries should fail because the connection is closed |
| 179 | + let result = conn.simple_query("BEGIN").await; |
| 180 | + assert!( |
| 181 | + result.is_err(), |
| 182 | + "Expected query to fail because connection was dropped" |
| 183 | + ); |
| 184 | + |
| 185 | + // Verify that the error indicates connection was closed |
| 186 | + let err = result.unwrap_err(); |
| 187 | + let err_str = err.to_string().to_lowercase(); |
| 188 | + assert!( |
| 189 | + err_str.contains("connection closed"), |
| 190 | + "Expected 'connection closed' error, got: {}", |
| 191 | + err |
| 192 | + ); |
| 193 | + |
| 194 | + // Reset settings |
| 195 | + admin.simple_query("RELOAD").await.unwrap(); |
| 196 | +} |
0 commit comments