|
| 1 | +/// Tests that validate proxy connection isolation under load. |
| 2 | +/// |
| 3 | +/// These tests verify that: |
| 4 | +/// - Slow queries on one connection don't block other connections |
| 5 | +/// - The proxy accepts new connections after client disconnect |
| 6 | +/// - Concurrent connections under load remain responsive |
| 7 | +/// - Blocked backend connections don't affect other proxy connections |
| 8 | +#[cfg(test)] |
| 9 | +mod tests { |
| 10 | + use crate::common::{connect_with_tls, get_database_port, PROXY}; |
| 11 | + use std::time::Instant; |
| 12 | + use tokio::task::JoinSet; |
| 13 | + use tokio::time::{timeout, Duration}; |
| 14 | + use tokio_postgres::SimpleQueryMessage; |
| 15 | + |
| 16 | + /// Advisory lock ID used in isolation tests. Arbitrary value — just needs to be |
| 17 | + /// unique across concurrently running test suites against the same database. |
| 18 | + const ADVISORY_LOCK_ID: i64 = 99_001; |
| 19 | + |
| 20 | + /// A slow query on one connection does not block other connections through the proxy. |
| 21 | + #[tokio::test] |
| 22 | + async fn slow_query_does_not_block_other_connections() { |
| 23 | + let result = timeout(Duration::from_secs(30), async { |
| 24 | + let client_a = connect_with_tls(PROXY).await; |
| 25 | + let client_b = connect_with_tls(PROXY).await; |
| 26 | + |
| 27 | + // Connection A: run a slow query |
| 28 | + let a_handle = tokio::spawn(async move { |
| 29 | + client_a.simple_query("SELECT pg_sleep(5)").await.unwrap(); |
| 30 | + }); |
| 31 | + |
| 32 | + // Brief pause to ensure A's query is in flight |
| 33 | + tokio::time::sleep(Duration::from_millis(200)).await; |
| 34 | + |
| 35 | + // Connection B: run a fast query, should complete promptly |
| 36 | + let start = Instant::now(); |
| 37 | + let rows = client_b.simple_query("SELECT 1").await.unwrap(); |
| 38 | + let elapsed = start.elapsed(); |
| 39 | + |
| 40 | + assert!(!rows.is_empty(), "Expected result from SELECT 1"); |
| 41 | + assert!( |
| 42 | + elapsed < Duration::from_secs(2), |
| 43 | + "Fast query took {elapsed:?}, expected < 2s — proxy may be blocking" |
| 44 | + ); |
| 45 | + |
| 46 | + a_handle.await.unwrap(); |
| 47 | + }) |
| 48 | + .await; |
| 49 | + |
| 50 | + result.expect("Test timed out after 30s"); |
| 51 | + } |
| 52 | + |
| 53 | + /// Proxy accepts new connections after a client disconnects. |
| 54 | + #[tokio::test] |
| 55 | + async fn proxy_accepts_new_connections_after_client_disconnect() { |
| 56 | + let result = timeout(Duration::from_secs(10), async { |
| 57 | + // First connection: query, then drop |
| 58 | + { |
| 59 | + let client = connect_with_tls(PROXY).await; |
| 60 | + let rows = client.simple_query("SELECT 1").await.unwrap(); |
| 61 | + assert!(!rows.is_empty()); |
| 62 | + } |
| 63 | + // Client dropped here |
| 64 | + |
| 65 | + // Brief pause |
| 66 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 67 | + |
| 68 | + // Second connection: should work fine |
| 69 | + let client = connect_with_tls(PROXY).await; |
| 70 | + let rows = client.simple_query("SELECT 1").await.unwrap(); |
| 71 | + assert!(!rows.is_empty()); |
| 72 | + }) |
| 73 | + .await; |
| 74 | + |
| 75 | + result.expect("Test timed out after 10s"); |
| 76 | + } |
| 77 | + |
| 78 | + /// Concurrent slow and fast connections: fast queries complete promptly under slow load. |
| 79 | + #[tokio::test] |
| 80 | + async fn concurrent_connections_under_slow_load() { |
| 81 | + let result = timeout(Duration::from_secs(30), async { |
| 82 | + let mut join_set = JoinSet::new(); |
| 83 | + |
| 84 | + // 5 slow connections |
| 85 | + for _ in 0..5 { |
| 86 | + join_set.spawn(async { |
| 87 | + let client = connect_with_tls(PROXY).await; |
| 88 | + client.simple_query("SELECT pg_sleep(3)").await.unwrap(); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + // Brief pause to let slow queries start |
| 93 | + tokio::time::sleep(Duration::from_millis(300)).await; |
| 94 | + |
| 95 | + // 5 fast connections, each should complete promptly |
| 96 | + for _ in 0..5 { |
| 97 | + join_set.spawn(async { |
| 98 | + let start = Instant::now(); |
| 99 | + let client = connect_with_tls(PROXY).await; |
| 100 | + let rows = client.simple_query("SELECT 1").await.unwrap(); |
| 101 | + let elapsed = start.elapsed(); |
| 102 | + |
| 103 | + assert!(!rows.is_empty()); |
| 104 | + assert!( |
| 105 | + elapsed < Duration::from_secs(5), |
| 106 | + "Fast query took {elapsed:?} under slow load, expected < 5s" |
| 107 | + ); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + while let Some(result) = join_set.join_next().await { |
| 112 | + result.unwrap(); |
| 113 | + } |
| 114 | + }) |
| 115 | + .await; |
| 116 | + |
| 117 | + result.expect("Test timed out after 30s"); |
| 118 | + } |
| 119 | + |
| 120 | + /// An advisory-lock-blocked connection through the proxy does not block other proxy connections. |
| 121 | + /// |
| 122 | + /// Uses pg_locks polling to deterministically wait for client_b to be blocked on the |
| 123 | + /// advisory lock, rather than relying on a fixed sleep. |
| 124 | + #[tokio::test] |
| 125 | + async fn advisory_lock_blocked_connection_does_not_block_proxy() { |
| 126 | + let lock_query = format!("SELECT pg_advisory_lock({ADVISORY_LOCK_ID})"); |
| 127 | + let unlock_query = format!("SELECT pg_advisory_unlock({ADVISORY_LOCK_ID})"); |
| 128 | + |
| 129 | + let result = timeout(Duration::from_secs(30), async { |
| 130 | + // Connection A: hold an advisory lock (connect directly to PG to avoid proxy interference) |
| 131 | + let pg_port = get_database_port(); |
| 132 | + let client_a = connect_with_tls(pg_port).await; |
| 133 | + client_a |
| 134 | + .simple_query(&lock_query) |
| 135 | + .await |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + let b_lock_query = lock_query.clone(); |
| 139 | + let b_unlock_query = unlock_query.clone(); |
| 140 | + |
| 141 | + // Connection B: through proxy, attempt to acquire the same lock (will block) |
| 142 | + let b_handle = tokio::spawn(async move { |
| 143 | + let client_b = connect_with_tls(PROXY).await; |
| 144 | + // This will block until A releases the lock |
| 145 | + client_b |
| 146 | + .simple_query(&b_lock_query) |
| 147 | + .await |
| 148 | + .unwrap(); |
| 149 | + // Release after acquiring |
| 150 | + client_b |
| 151 | + .simple_query(&b_unlock_query) |
| 152 | + .await |
| 153 | + .unwrap(); |
| 154 | + }); |
| 155 | + |
| 156 | + // Poll pg_locks until client_b is observed waiting for the advisory lock |
| 157 | + let poll_query = format!( |
| 158 | + "SELECT 1 FROM pg_locks WHERE locktype = 'advisory' AND NOT granted AND classid = 0 AND objid = {ADVISORY_LOCK_ID}" |
| 159 | + ); |
| 160 | + let deadline = Instant::now() + Duration::from_secs(10); |
| 161 | + loop { |
| 162 | + let result = client_a.simple_query(&poll_query).await.unwrap(); |
| 163 | + let has_waiting = result.iter().any(|m| matches!(m, SimpleQueryMessage::Row(_))); |
| 164 | + if has_waiting { |
| 165 | + break; |
| 166 | + } |
| 167 | + assert!( |
| 168 | + Instant::now() < deadline, |
| 169 | + "Timed out waiting for client_b to be blocked on advisory lock" |
| 170 | + ); |
| 171 | + tokio::time::sleep(Duration::from_millis(50)).await; |
| 172 | + } |
| 173 | + |
| 174 | + // Connection C: through proxy, should complete immediately despite B being blocked |
| 175 | + let start = Instant::now(); |
| 176 | + let client_c = connect_with_tls(PROXY).await; |
| 177 | + let rows = client_c.simple_query("SELECT 1").await.unwrap(); |
| 178 | + let elapsed = start.elapsed(); |
| 179 | + |
| 180 | + assert!(!rows.is_empty()); |
| 181 | + assert!( |
| 182 | + elapsed < Duration::from_secs(2), |
| 183 | + "Connection C took {elapsed:?}, expected < 2s — blocked connection may be affecting proxy" |
| 184 | + ); |
| 185 | + |
| 186 | + // Release the lock so B can complete |
| 187 | + client_a |
| 188 | + .simple_query(&unlock_query) |
| 189 | + .await |
| 190 | + .unwrap(); |
| 191 | + |
| 192 | + b_handle.await.unwrap(); |
| 193 | + }) |
| 194 | + .await; |
| 195 | + |
| 196 | + result.expect("Test timed out after 30s"); |
| 197 | + } |
| 198 | +} |
0 commit comments