|
| 1 | +import { describe, it, expect, mock } from "bun:test"; |
| 2 | +import { |
| 3 | + shutdownGracefully, |
| 4 | + type ShutdownTarget, |
| 5 | +} from "../../apps/server-ts/src/shutdown"; |
| 6 | + |
| 7 | +function makeExecutor(runningIds: string[], stopImpl?: (id: string) => Promise<void>) { |
| 8 | + const stopWorkflow = mock(stopImpl ?? (async (_id: string) => {})); |
| 9 | + const target: ShutdownTarget = { |
| 10 | + getRunningWorkflowIds: () => runningIds, |
| 11 | + stopWorkflow, |
| 12 | + }; |
| 13 | + return { target, stopWorkflow }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("TestGracefulShutdown", () => { |
| 17 | + it("test_stops_all_running_workflows_then_closes_db", async () => { |
| 18 | + const { target, stopWorkflow } = makeExecutor(["wf-a", "wf-b", "wf-c"]); |
| 19 | + const order: string[] = []; |
| 20 | + const closeDatabase = mock(() => { |
| 21 | + order.push("close-db"); |
| 22 | + }); |
| 23 | + |
| 24 | + await shutdownGracefully(target, { closeDatabase }); |
| 25 | + |
| 26 | + expect(stopWorkflow).toHaveBeenCalledTimes(3); |
| 27 | + expect(stopWorkflow.mock.calls.map((c) => c[0]).sort()).toEqual(["wf-a", "wf-b", "wf-c"]); |
| 28 | + expect(closeDatabase).toHaveBeenCalledTimes(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it("test_closes_db_when_nothing_is_running", async () => { |
| 32 | + const { target, stopWorkflow } = makeExecutor([]); |
| 33 | + const closeDatabase = mock(() => {}); |
| 34 | + |
| 35 | + await shutdownGracefully(target, { closeDatabase }); |
| 36 | + |
| 37 | + expect(stopWorkflow).not.toHaveBeenCalled(); |
| 38 | + expect(closeDatabase).toHaveBeenCalledTimes(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it("test_one_failing_workflow_does_not_skip_others_or_db_close", async () => { |
| 42 | + const { target, stopWorkflow } = makeExecutor(["wf-bad", "wf-ok"], async (id) => { |
| 43 | + if (id === "wf-bad") throw new Error("stop failed"); |
| 44 | + }); |
| 45 | + const closeDatabase = mock(() => {}); |
| 46 | + |
| 47 | + await shutdownGracefully(target, { closeDatabase }); |
| 48 | + |
| 49 | + expect(stopWorkflow).toHaveBeenCalledTimes(2); |
| 50 | + expect(closeDatabase).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("test_db_close_failure_does_not_throw", async () => { |
| 54 | + const { target } = makeExecutor([]); |
| 55 | + const closeDatabase = mock(() => { |
| 56 | + throw new Error("db close failed"); |
| 57 | + }); |
| 58 | + |
| 59 | + await expect(shutdownGracefully(target, { closeDatabase })).resolves.toBeUndefined(); |
| 60 | + expect(closeDatabase).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("test_getRunningWorkflowIds_failure_still_closes_db", async () => { |
| 64 | + const closeDatabase = mock(() => {}); |
| 65 | + const target: ShutdownTarget = { |
| 66 | + getRunningWorkflowIds: () => { |
| 67 | + throw new Error("executor broken"); |
| 68 | + }, |
| 69 | + stopWorkflow: async () => {}, |
| 70 | + }; |
| 71 | + |
| 72 | + await expect(shutdownGracefully(target, { closeDatabase })).resolves.toBeUndefined(); |
| 73 | + expect(closeDatabase).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | +}); |
0 commit comments