|
| 1 | +import { EventEmitter } from "events"; |
| 2 | +import { ServerProcess } from "../../../src/query-server/server-process"; |
| 3 | + |
| 4 | +function createFakeChild(): any { |
| 5 | + const child = new EventEmitter() as any; |
| 6 | + child.exitCode = null; |
| 7 | + child.signalCode = null; |
| 8 | + child.kill = jest.fn((signal?: string) => { |
| 9 | + child.exitCode = 1; |
| 10 | + child.emit("exit", null, signal ?? "SIGKILL"); |
| 11 | + return true; |
| 12 | + }); |
| 13 | + return child; |
| 14 | +} |
| 15 | + |
| 16 | +const fakeConnection = { dispose: jest.fn(), end: jest.fn() } as any; |
| 17 | +const fakeLogger = { log: jest.fn() } as any; |
| 18 | + |
| 19 | +function createServerProcess(child: any): ServerProcess { |
| 20 | + return new ServerProcess( |
| 21 | + child, |
| 22 | + fakeConnection, |
| 23 | + "test query server", |
| 24 | + fakeLogger, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +describe("ServerProcess.waitForExit", () => { |
| 29 | + it("resolves when the process exits", async () => { |
| 30 | + const child = createFakeChild(); |
| 31 | + const serverProcess = createServerProcess(child); |
| 32 | + |
| 33 | + const promise = serverProcess.waitForExit(); |
| 34 | + child.exitCode = 0; |
| 35 | + child.emit("exit", 0, null); |
| 36 | + |
| 37 | + await expect(promise).resolves.toBeUndefined(); |
| 38 | + expect(child.kill).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("resolves immediately if the process has already exited", async () => { |
| 42 | + const child = createFakeChild(); |
| 43 | + child.exitCode = 0; |
| 44 | + const serverProcess = createServerProcess(child); |
| 45 | + |
| 46 | + await expect(serverProcess.waitForExit()).resolves.toBeUndefined(); |
| 47 | + expect(child.kill).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("force-kills the process if it does not exit within the timeout", async () => { |
| 51 | + jest.useFakeTimers(); |
| 52 | + try { |
| 53 | + const child = createFakeChild(); |
| 54 | + const serverProcess = createServerProcess(child); |
| 55 | + |
| 56 | + const promise = serverProcess.waitForExit(1000); |
| 57 | + jest.advanceTimersByTime(1000); |
| 58 | + |
| 59 | + await expect(promise).resolves.toBeUndefined(); |
| 60 | + expect(child.kill).toHaveBeenCalledWith("SIGKILL"); |
| 61 | + } finally { |
| 62 | + jest.useRealTimers(); |
| 63 | + } |
| 64 | + }); |
| 65 | +}); |
0 commit comments