Skip to content

Commit 281f0ee

Browse files
author
t
committed
test(node): poll for the closed session instead of sleeping a fixed 300ms
The release-invariant test slept 300ms for a Drop-spawned close before comparing backend pids, which is a coin flip on a loaded CI runner. It now polls pg_stat_activity for the captured pid on a standalone connection, the same discipline poll_until_free documents: a pooled observer would be handed the session under measurement and hide the effect. The conversion was checked against the failure it exists to catch rather than assumed. Pooling the session on an unlock that returned false makes the test fail after the poll deadline, not hang, and no_reap_pool disables idle timeout and max lifetime so nothing but the close under test can retire that backend. A generous deadline would have been the same defect as the sleep.
1 parent d4d2766 commit 281f0ee

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

‎crates/gitlawb-node/src/git/repo_store.rs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,8 +1798,34 @@ mod tests {
17981798
};
17991799
guard.release(true).await;
18001800

1801-
// Give the spawned close a moment, then see which backend we land on.
1802-
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
1801+
// Wait for the backend to actually go away rather than sleeping a fixed
1802+
// span, which is flaky on slow CI. The observer is a STANDALONE
1803+
// connection for the same reason `poll_until_free` uses one: taking it
1804+
// from the pool under test would hand us the very session being measured.
1805+
// Nothing but the close under test can retire that backend, because
1806+
// `no_reap_pool` disables idle timeout and max lifetime, so a zero count
1807+
// here is attributable to `release()` and to nothing else.
1808+
{
1809+
use sqlx::Connection;
1810+
let deadline = std::time::Duration::from_secs(5);
1811+
let start = std::time::Instant::now();
1812+
let mut observer = sqlx::PgConnection::connect_with(&opts)
1813+
.await
1814+
.expect("standalone observer connection");
1815+
while start.elapsed() < deadline {
1816+
let alive: (i64,) =
1817+
sqlx::query_as("SELECT count(*) FROM pg_stat_activity WHERE pid = $1")
1818+
.bind(pid_before)
1819+
.fetch_one(&mut observer)
1820+
.await
1821+
.expect("observer pg_stat_activity probe");
1822+
if alive.0 == 0 {
1823+
break;
1824+
}
1825+
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
1826+
}
1827+
}
1828+
18031829
let pid_after = {
18041830
let mut c = lock_pool.acquire().await.unwrap();
18051831
let pid: (i32,) = sqlx::query_as("SELECT pg_backend_pid()")

0 commit comments

Comments
 (0)