|
| 1 | +import { describe, test, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { shutdownManager } from "./shutdownManager.js"; |
| 3 | + |
| 4 | +// Type assertion to access private members for testing |
| 5 | +type PrivateShutdownManager = { |
| 6 | + handlers: Map<string, { handler: NodeJS.SignalsListener; signals: Array<"SIGTERM" | "SIGINT"> }>; |
| 7 | + shutdown: (signal: "SIGTERM" | "SIGINT") => Promise<void>; |
| 8 | + _reset: () => void; |
| 9 | +}; |
| 10 | + |
| 11 | +describe("ShutdownManager", { concurrent: false }, () => { |
| 12 | + const manager = shutdownManager as unknown as PrivateShutdownManager; |
| 13 | + // Mock process.exit to prevent actual exit |
| 14 | + const mockExit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + // Clear all mocks and reset the manager before each test |
| 18 | + vi.clearAllMocks(); |
| 19 | + manager._reset(); |
| 20 | + }); |
| 21 | + |
| 22 | + test("should successfully register a new handler", () => { |
| 23 | + const handler = vi.fn(); |
| 24 | + shutdownManager.register("test-handler", handler); |
| 25 | + |
| 26 | + expect(manager.handlers.has("test-handler")).toBe(true); |
| 27 | + const registeredHandler = manager.handlers.get("test-handler"); |
| 28 | + expect(registeredHandler?.handler).toBe(handler); |
| 29 | + expect(registeredHandler?.signals).toEqual(["SIGTERM", "SIGINT"]); |
| 30 | + }); |
| 31 | + |
| 32 | + test("should throw error when registering duplicate handler name", () => { |
| 33 | + const handler = vi.fn(); |
| 34 | + shutdownManager.register("duplicate-handler", handler); |
| 35 | + |
| 36 | + expect(() => { |
| 37 | + shutdownManager.register("duplicate-handler", handler); |
| 38 | + }).toThrow('Shutdown handler "duplicate-handler" already registered'); |
| 39 | + }); |
| 40 | + |
| 41 | + test("should register handler with custom signals", () => { |
| 42 | + const handler = vi.fn(); |
| 43 | + shutdownManager.register("custom-signals", handler, ["SIGTERM"]); |
| 44 | + |
| 45 | + const registeredHandler = manager.handlers.get("custom-signals"); |
| 46 | + expect(registeredHandler?.signals).toEqual(["SIGTERM"]); |
| 47 | + }); |
| 48 | + |
| 49 | + test("should call registered handlers when shutdown is triggered", async () => { |
| 50 | + const handler1 = vi.fn(); |
| 51 | + const handler2 = vi.fn(); |
| 52 | + |
| 53 | + shutdownManager.register("handler1", handler1); |
| 54 | + shutdownManager.register("handler2", handler2); |
| 55 | + |
| 56 | + await manager.shutdown("SIGTERM"); |
| 57 | + |
| 58 | + expect(handler1).toHaveBeenCalledWith("SIGTERM"); |
| 59 | + expect(handler2).toHaveBeenCalledWith("SIGTERM"); |
| 60 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); // SIGTERM number |
| 61 | + }); |
| 62 | + |
| 63 | + test("should only call handlers registered for specific signal", async () => { |
| 64 | + const handler1 = vi.fn(); |
| 65 | + const handler2 = vi.fn(); |
| 66 | + |
| 67 | + shutdownManager.register("handler1", handler1, ["SIGTERM"]); |
| 68 | + shutdownManager.register("handler2", handler2, ["SIGINT"]); |
| 69 | + |
| 70 | + await manager.shutdown("SIGTERM"); |
| 71 | + |
| 72 | + expect(handler1).toHaveBeenCalledWith("SIGTERM"); |
| 73 | + expect(handler2).not.toHaveBeenCalled(); |
| 74 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); |
| 75 | + }); |
| 76 | + |
| 77 | + test("should handle errors in shutdown handlers gracefully", async () => { |
| 78 | + const successHandler = vi.fn(); |
| 79 | + const errorHandler = vi.fn().mockRejectedValue(new Error("Handler failed")); |
| 80 | + |
| 81 | + shutdownManager.register("success-handler", successHandler); |
| 82 | + shutdownManager.register("error-handler", errorHandler); |
| 83 | + |
| 84 | + await manager.shutdown("SIGTERM"); |
| 85 | + |
| 86 | + expect(successHandler).toHaveBeenCalledWith("SIGTERM"); |
| 87 | + expect(errorHandler).toHaveBeenCalledWith("SIGTERM"); |
| 88 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); |
| 89 | + }); |
| 90 | + |
| 91 | + test("should only run shutdown sequence once even if called multiple times", async () => { |
| 92 | + const handler = vi.fn(); |
| 93 | + shutdownManager.register("test-handler", handler); |
| 94 | + |
| 95 | + await Promise.all([manager.shutdown("SIGTERM"), manager.shutdown("SIGTERM")]); |
| 96 | + |
| 97 | + expect(handler).toHaveBeenCalledTimes(1); |
| 98 | + expect(mockExit).toHaveBeenCalledTimes(1); |
| 99 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); |
| 100 | + }); |
| 101 | + |
| 102 | + test("should exit with correct signal number", async () => { |
| 103 | + const handler = vi.fn(); |
| 104 | + shutdownManager.register("test-handler", handler); |
| 105 | + |
| 106 | + await manager.shutdown("SIGINT"); |
| 107 | + expect(mockExit).toHaveBeenCalledWith(128 + 2); // SIGINT number |
| 108 | + |
| 109 | + vi.clearAllMocks(); |
| 110 | + manager._reset(); |
| 111 | + shutdownManager.register("test-handler", handler); |
| 112 | + |
| 113 | + await manager.shutdown("SIGTERM"); |
| 114 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); // SIGTERM number |
| 115 | + }); |
| 116 | + |
| 117 | + test("should only exit after all handlers have finished", async () => { |
| 118 | + const sequence: string[] = []; |
| 119 | + |
| 120 | + const handler1 = vi.fn().mockImplementation(async () => { |
| 121 | + sequence.push("handler1 start"); |
| 122 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 123 | + sequence.push("handler1 end"); |
| 124 | + }); |
| 125 | + |
| 126 | + const handler2 = vi.fn().mockImplementation(async () => { |
| 127 | + sequence.push("handler2 start"); |
| 128 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 129 | + sequence.push("handler2 end"); |
| 130 | + }); |
| 131 | + |
| 132 | + const handler3 = vi.fn().mockImplementation(async () => { |
| 133 | + sequence.push("handler3 start"); |
| 134 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 135 | + sequence.push("handler3 end"); |
| 136 | + }); |
| 137 | + |
| 138 | + // Store the current mock implementation |
| 139 | + const currentExit = mockExit.getMockImplementation(); |
| 140 | + |
| 141 | + // Override with our sequence-tracking implementation |
| 142 | + mockExit.mockImplementation((code?: number | string | null) => { |
| 143 | + sequence.push("exit"); |
| 144 | + return undefined as never; |
| 145 | + }); |
| 146 | + |
| 147 | + shutdownManager.register("handler1", handler1); |
| 148 | + shutdownManager.register("handler2", handler2); |
| 149 | + shutdownManager.register("handler3", handler3); |
| 150 | + |
| 151 | + await manager.shutdown("SIGTERM"); |
| 152 | + |
| 153 | + // Verify the execution order |
| 154 | + expect(sequence).toEqual([ |
| 155 | + "handler1 start", |
| 156 | + "handler2 start", |
| 157 | + "handler3 start", |
| 158 | + "handler3 end", |
| 159 | + "handler1 end", |
| 160 | + "handler2 end", |
| 161 | + "exit", |
| 162 | + ]); |
| 163 | + |
| 164 | + // Verify the handlers were called with correct arguments |
| 165 | + expect(handler1).toHaveBeenCalledWith("SIGTERM"); |
| 166 | + expect(handler2).toHaveBeenCalledWith("SIGTERM"); |
| 167 | + expect(handler3).toHaveBeenCalledWith("SIGTERM"); |
| 168 | + expect(mockExit).toHaveBeenCalledWith(128 + 15); |
| 169 | + |
| 170 | + // Restore original mock implementation |
| 171 | + if (currentExit) { |
| 172 | + mockExit.mockImplementation(currentExit); |
| 173 | + } |
| 174 | + }); |
| 175 | +}); |
0 commit comments