Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(persistence): idle-pool drain test awaits the drain, dumps pg_stat_activity on failure and holds past one probe cycle
garrytan034c01554a38
diff --git a/test/e2e/persistence-idle-pool.test.ts b/test/e2e/persistence-idle-pool.test.tsindex b9581ad8e4..51b55c62f4 100644--- a/test/e2e/persistence-idle-pool.test.ts+++ b/test/e2e/persistence-idle-pool.test.ts@@ -94,8 +94,46 @@ describe.skipIf(!direct)('idle persistence consumer on PostgreSQL', () => {const backends = async () => Number((await db.admin.unsafe('SELECT count(*)::int AS n FROM pg_stat_activity WHERE datname=$1', [db.name]))[0]!.n);expect(await backends()).toBeGreaterThanOrEqual(3);- await Bun.sleep(IDLE_DRAIN_MS);- expect(await backends()).toBe(1);+ const started = performance.now();+ // The pool drains idle_timeout (20 s) after its last pooled statement. The consumer's start-up fan-out and its+ // background workers are that last statement on a quiet box; a loaded runner can stretch them past the fixed+ // 28 s mark this test used to sleep for, so the drain is awaited (bounded) and then must hold.+ await waitFor(async () => (await backends()) === 1, { timeoutMs: IDLE_DRAIN_MS + 20_000, intervalMs: 500 }).catch(() => undefined);+ const drainedAfterMs = Math.round(performance.now() - started);+ const dump = async () => db.admin.unsafe(`SELECT pid, application_name, backend_type, state, wait_event,+ to_char(backend_start, 'HH24:MI:SS.MS') AS backend_start, to_char(state_change, 'HH24:MI:SS.MS') AS state_change,+ left(query, 160) AS query FROM pg_stat_activity WHERE datname=$1 ORDER BY backend_start`, [db.name]);+ let count = await backends();+ if (count !== 1) console.error(`[idle-pool] ${count} backend(s) after ${drainedAfterMs} ms:`, JSON.stringify(await dump(), null, 1));+ expect(count).toBe(1);+ // Drained means drained: a hold longer than one 5 s probe cycle shows the idle probe itself opens no pooled connection.+ await Bun.sleep(6_000);+ count = await backends();+ if (count !== 1) console.error(`[idle-pool] ${count} backend(s) 6 s after the drain (drain took ${drainedAfterMs} ms):`, JSON.stringify(await dump(), null, 1));+ expect(count).toBe(1);+ });+ } finally { await db.drop(); }+ }, 120_000);++ test('forced probe: a pooled statement 10 s into the idle window delays the drain by idle_timeout and nothing more', async () => {+ // GBRA-60's reproduction of the flake: one ordinary-pool statement mid-window kept a backend alive past the fixed+ // 28 s mark the previous test slept for (8 of 8 runs). The pool still drains to one backend; it does so 20 s after+ // that statement, which is what the awaited drain above tolerates and the old fixed sleep did not.+ const db = await scratchDatabase(direct!, direct!);+ try {+ await withConsumer(db.url, async engine => {+ const backends = async () => Number((await db.admin.unsafe(+ 'SELECT count(*)::int AS n FROM pg_stat_activity WHERE datname=$1', [db.name]))[0]!.n);+ await Bun.sleep(10_000);+ await engine.executeRaw('SELECT 1');+ const late = performance.now();+ await Bun.sleep(IDLE_DRAIN_MS - 10_000);+ // 28 s from start, 18 s after the late statement: the pooled backend it used is still inside idle_timeout.+ expect(await backends()).toBeGreaterThanOrEqual(2);+ await waitFor(async () => (await backends()) === 1, { timeoutMs: 30_000, intervalMs: 500 });+ const drainedMs = performance.now() - late;+ expect(drainedMs).toBeGreaterThanOrEqual(19_000);+ expect(drainedMs).toBeLessThan(30_000);});} finally { await db.drop(); }}, 120_000);