From 39eccf89a884929c3a5eb2076aae7e89ee0cb414 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Fri, 1 Aug 2025 22:41:05 -0700 Subject: [PATCH] Deflake sql.test.ts --- test/js/sql/sql-fixture-ref.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/test/js/sql/sql-fixture-ref.ts b/test/js/sql/sql-fixture-ref.ts index af8f52dafc..c62080a59a 100644 --- a/test/js/sql/sql-fixture-ref.ts +++ b/test/js/sql/sql-fixture-ref.ts @@ -2,20 +2,44 @@ // 1 // 2 // and exiting with code 0. +// +// Due to pipelining and the way the network stuff works, sometimes the second +// function can finish before the first function. The main purpose of this test +// is that both first() and yo(): +// 1. Keep the event loop alive +// 2. Don't get GC'd too early. +// +// Therefore, we must not keep any references to the promises returned by +// first() or yo(). We must not top-level await the results. import { sql } from "bun"; process.exitCode = 1; +let values = []; + async function first() { const result = await sql`select 1 as x`; - console.log(result[0].x); + values.push(result[0].x); + maybeDone(); } async function yo() { const result2 = await sql`select 2 as x`; - console.log(result2[0].x); - process.exitCode = 0; + values.push(result2[0].x); + maybeDone(); } + first(); Bun.gc(true); yo(); Bun.gc(true); + +function maybeDone() { + if (values.length === 2) { + // Determinism. + values.sort(); + + console.log(values[0]); + console.log(values[1]); + process.exitCode = 0; + } +}