-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresjs.exit.test.ts
More file actions
80 lines (71 loc) · 2.66 KB
/
Copy pathpostgresjs.exit.test.ts
File metadata and controls
80 lines (71 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { test, expect } from "vitest";
import { PostgreSqlContainer } from "@testcontainers/postgresql";
import { execFile } from "node:child_process";
function runScript(
script: string,
): Promise<{ exitCode: number; timedOut: boolean }> {
return new Promise((resolve) => {
const child = execFile(
"node",
["--import=tsx", "--input-type=module", "-e", script],
{ timeout: 5_000 },
(error) => {
resolve({
exitCode: error ? Number(error.code ?? 1) : 0,
timedOut: error?.killed === true,
});
},
);
// Pipe to a pipe (not inherit) to mimic CI where SIGPIPE can occur
child.stdout?.resume();
child.stderr?.resume();
});
}
/**
* Verifies that a process with an idle source pool connection exits
* naturally without hanging. Without allowExitOnIdle, pg-pool keeps
* idle connections ref'd, which blocks Node from exiting.
*/
test("process exits without hanging when source pool has idle connections", async () => {
const pg = await new PostgreSqlContainer("postgres:17").start();
const script = `
import { connectToSource } from "./src/sql/postgresjs.ts";
import { Connectable } from "./src/sync/connectable.ts";
const db = connectToSource(Connectable.fromString("${pg.getConnectionUri()}"));
await db.exec("SELECT 1");
// Intentionally do NOT call db.close() — idle connection stays in pool.
// With allowExitOnIdle, the process should still exit promptly.
// Without it, the idle connection's ref'd socket blocks exit.
`;
try {
const { exitCode, timedOut } = await runScript(script);
expect(timedOut, "process should not hang on idle pool connections").toBe(
false,
);
expect(exitCode, "process should exit 0, not SIGPIPE (13)").toBe(0);
} finally {
await pg.stop();
}
});
/**
* Verifies that a process exits cleanly (code 0) after explicitly
* closing the source pool. This guards against SIGPIPE (exit 13)
* caused by process.exit() killing I/O mid-flush.
*/
test("process exits with code 0 after explicit pool close", async () => {
const pg = await new PostgreSqlContainer("postgres:17").start();
const script = `
import { connectToSource } from "./src/sql/postgresjs.ts";
import { Connectable } from "./src/sync/connectable.ts";
const db = connectToSource(Connectable.fromString("${pg.getConnectionUri()}"));
await db.exec("SELECT 1");
await db.close();
`;
try {
const { exitCode, timedOut } = await runScript(script);
expect(timedOut, "process should not hang after pool.end()").toBe(false);
expect(exitCode, "process should exit 0, not SIGPIPE (13)").toBe(0);
} finally {
await pg.stop();
}
});