mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 11:59:00 +00:00
Add --sql-preconnect CLI flag for PostgreSQL startup connections (#21035)
Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: jarred-sumner-bot <220441119+jarred-sumner-bot@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
70ebe75e6c
commit
e9ccc81e03
84
test/cli/run/sql-preconnect.test.ts
Normal file
84
test/cli/run/sql-preconnect.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
|
||||
|
||||
describe("--sql-preconnect", () => {
|
||||
test("should attempt to preconnect to PostgreSQL on startup", async () => {
|
||||
let connectionAttempts = 0;
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
|
||||
await using server = Bun.listen({
|
||||
port: 0,
|
||||
hostname: "127.0.0.1",
|
||||
socket: {
|
||||
open(socket) {
|
||||
connectionAttempts++;
|
||||
socket.end();
|
||||
if (connectionAttempts >= 1) {
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
data() {},
|
||||
close() {},
|
||||
},
|
||||
});
|
||||
|
||||
const testDir = tempDirWithFiles("sql-preconnect-test", {
|
||||
"index.js": `console.log("Script executed");`,
|
||||
});
|
||||
|
||||
const proc = Bun.spawn({
|
||||
cmd: [bunExe(), "--sql-preconnect", "index.js"],
|
||||
env: {
|
||||
...bunEnv,
|
||||
DATABASE_URL: `postgres://127.0.0.1:${server.port}/MY_DATABASE`,
|
||||
},
|
||||
cwd: testDir,
|
||||
});
|
||||
|
||||
await promise;
|
||||
proc.kill();
|
||||
await proc.exited;
|
||||
|
||||
expect(connectionAttempts).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("should not connect when flag is not used", async () => {
|
||||
let connectionAttempts = 0;
|
||||
|
||||
await using server = Bun.listen({
|
||||
port: 0,
|
||||
hostname: "127.0.0.1",
|
||||
socket: {
|
||||
open(socket) {
|
||||
connectionAttempts++;
|
||||
socket.end();
|
||||
},
|
||||
data() {},
|
||||
close() {},
|
||||
},
|
||||
});
|
||||
|
||||
const testDir = tempDirWithFiles("sql-no-preconnect", {
|
||||
"index.js": `console.log("Normal script executed");`,
|
||||
});
|
||||
|
||||
await using proc = Bun.spawn({
|
||||
cmd: [bunExe(), "index.js"],
|
||||
env: {
|
||||
...bunEnv,
|
||||
DATABASE_URL: `postgres://127.0.0.1:${server.port}/MY_DATABASE`,
|
||||
},
|
||||
cwd: testDir,
|
||||
});
|
||||
|
||||
const [stdout, stderr, exitCode] = await Promise.all([
|
||||
new Response(proc.stdout).text(),
|
||||
new Response(proc.stderr).text(),
|
||||
proc.exited,
|
||||
]);
|
||||
|
||||
expect(exitCode).toBe(0);
|
||||
expect(stdout).toContain("Normal script executed");
|
||||
expect(connectionAttempts).toBe(0); // No connection should be attempted without the flag
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user