mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Deflake sql.test.ts
This commit is contained in:
30
test/js/sql/sql-fixture-ref.ts
generated
30
test/js/sql/sql-fixture-ref.ts
generated
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user