-
-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathshutdown.test.ts
More file actions
93 lines (78 loc) · 2.69 KB
/
shutdown.test.ts
File metadata and controls
93 lines (78 loc) · 2.69 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
81
82
83
84
85
86
87
88
89
90
91
92
93
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const callHook = vi.fn().mockResolvedValue(undefined);
vi.mock("../../src/runtime/internal/app.ts", () => ({
useNitroApp: () => ({
hooks: { callHook },
}),
}));
import {
resolveGracefulShutdownConfig,
setupShutdownHooks,
} from "../../src/runtime/internal/shutdown.ts";
import type { ProcessEventMap } from "node:process";
describe("resolveGracefulShutdownConfig", () => {
const env = process.env;
afterEach(() => {
process.env = env;
});
it("returns undefined by default", () => {
process.env = { ...env };
delete process.env.NITRO_SHUTDOWN_DISABLED;
delete process.env.NITRO_SHUTDOWN_TIMEOUT;
expect(resolveGracefulShutdownConfig()).toBeUndefined();
});
it.each([
{ value: "true", expected: false },
{ value: "false", expected: undefined },
{ value: "", expected: undefined },
{ value: "1", expected: undefined },
{ value: "yes", expected: undefined },
])("NITRO_SHUTDOWN_DISABLED=$value returns $expected", ({ value, expected }) => {
process.env = { ...env, NITRO_SHUTDOWN_DISABLED: value };
delete process.env.NITRO_SHUTDOWN_TIMEOUT;
expect(resolveGracefulShutdownConfig()).toBe(expected);
});
it("returns gracefulTimeout in seconds from NITRO_SHUTDOWN_TIMEOUT ms", () => {
process.env = { ...env, NITRO_SHUTDOWN_TIMEOUT: "10000" };
delete process.env.NITRO_SHUTDOWN_DISABLED;
expect(resolveGracefulShutdownConfig()).toEqual({ gracefulTimeout: 10 });
});
it("disabled takes priority over timeout", () => {
process.env = {
...env,
NITRO_SHUTDOWN_DISABLED: "true",
NITRO_SHUTDOWN_TIMEOUT: "10000",
};
expect(resolveGracefulShutdownConfig()).toBe(false);
});
it("ignores non-numeric timeout", () => {
process.env = { ...env, NITRO_SHUTDOWN_TIMEOUT: "abc" };
delete process.env.NITRO_SHUTDOWN_DISABLED;
expect(resolveGracefulShutdownConfig()).toBeUndefined();
});
});
describe("setupShutdownHooks", () => {
let signals: (keyof ProcessEventMap)[] = ["SIGTERM", "SIGINT"];
let priors: Record<string, ((...args: any) => void)[]> = Object.fromEntries(
signals.map((s) => [s, []])
);
beforeEach(() => {
callHook.mockClear();
for (const signal of signals) {
priors[signal] = process.listeners(signal).slice();
}
});
afterEach(() => {
for (const signal of signals) {
process.removeAllListeners(signal);
for (const fn of priors[signal]) {
process.on(signal, fn);
}
}
});
it.each(["SIGTERM", "SIGINT"])("calls close hook on %s", (signal) => {
setupShutdownHooks();
process.emit(signal, true);
expect(callHook).toHaveBeenCalledOnce();
});
});