|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { setTimeout as sleep } from "node:timers/promises"; |
| 3 | +import { ComputeSnapshotService } from "./computeSnapshotService.js"; |
| 4 | +import type { ComputeWorkloadManager } from "../workloadManager/compute.js"; |
| 5 | +import type { SupervisorHttpClient } from "@trigger.dev/core/v3/workers"; |
| 6 | + |
| 7 | +// The TimerWheel ticks every 100ms, so a 200ms delay dispatches within ~300ms. |
| 8 | +const DELAY_MS = 200; |
| 9 | +// Long enough that a pending snapshot would certainly have dispatched. |
| 10 | +const SETTLE_MS = 600; |
| 11 | + |
| 12 | +function createService() { |
| 13 | + const snapshot = vi.fn(async (_opts: { runnerId: string; metadata: Record<string, string> }) => true); |
| 14 | + |
| 15 | + const computeManager = { |
| 16 | + snapshotDelayMs: DELAY_MS, |
| 17 | + snapshotDispatchLimit: 1, |
| 18 | + snapshot, |
| 19 | + } as unknown as ComputeWorkloadManager; |
| 20 | + |
| 21 | + const service = new ComputeSnapshotService({ |
| 22 | + computeManager, |
| 23 | + workerClient: {} as SupervisorHttpClient, |
| 24 | + wideEventOpts: { service: "supervisor-test", env: {}, enabled: false }, |
| 25 | + }); |
| 26 | + |
| 27 | + return { service, snapshot }; |
| 28 | +} |
| 29 | + |
| 30 | +function delayedSnapshot(runnerId = "runner-1") { |
| 31 | + return { |
| 32 | + runnerId, |
| 33 | + runFriendlyId: "run_1", |
| 34 | + snapshotFriendlyId: "snapshot_1", |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe("ComputeSnapshotService", () => { |
| 39 | + it("dispatches a scheduled snapshot after the delay", async () => { |
| 40 | + const { service, snapshot } = createService(); |
| 41 | + try { |
| 42 | + service.schedule("run_1", delayedSnapshot()); |
| 43 | + |
| 44 | + await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); |
| 45 | + expect(snapshot).toHaveBeenCalledWith({ |
| 46 | + runnerId: "runner-1", |
| 47 | + metadata: { runId: "run_1", snapshotFriendlyId: "snapshot_1" }, |
| 48 | + }); |
| 49 | + } finally { |
| 50 | + service.stop(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it("cancel before the delay expires prevents the dispatch", async () => { |
| 55 | + const { service, snapshot } = createService(); |
| 56 | + try { |
| 57 | + service.schedule("run_1", delayedSnapshot()); |
| 58 | + |
| 59 | + expect(service.cancel("run_1")).toBe(true); |
| 60 | + |
| 61 | + await sleep(SETTLE_MS); |
| 62 | + expect(snapshot).not.toHaveBeenCalled(); |
| 63 | + } finally { |
| 64 | + service.stop(); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it("cancel returns false when nothing is pending", () => { |
| 69 | + const { service } = createService(); |
| 70 | + try { |
| 71 | + expect(service.cancel("run_1")).toBe(false); |
| 72 | + } finally { |
| 73 | + service.stop(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it("cancel with a matching runnerId cancels the pending snapshot", async () => { |
| 78 | + const { service, snapshot } = createService(); |
| 79 | + try { |
| 80 | + service.schedule("run_1", delayedSnapshot("runner-a")); |
| 81 | + |
| 82 | + expect(service.cancel("run_1", "runner-a")).toBe(true); |
| 83 | + |
| 84 | + await sleep(SETTLE_MS); |
| 85 | + expect(snapshot).not.toHaveBeenCalled(); |
| 86 | + } finally { |
| 87 | + service.stop(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("cancel with a different runnerId leaves the pending snapshot alone", async () => { |
| 92 | + const { service, snapshot } = createService(); |
| 93 | + try { |
| 94 | + service.schedule("run_1", delayedSnapshot("runner-a")); |
| 95 | + |
| 96 | + // A stale runner for a reassigned run must not cancel the new runner's snapshot. |
| 97 | + expect(service.cancel("run_1", "runner-b")).toBe(false); |
| 98 | + |
| 99 | + await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); |
| 100 | + expect(snapshot).toHaveBeenCalledWith( |
| 101 | + expect.objectContaining({ runnerId: "runner-a" }) |
| 102 | + ); |
| 103 | + } finally { |
| 104 | + service.stop(); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it("re-scheduling the same run replaces the pending snapshot", async () => { |
| 109 | + const { service, snapshot } = createService(); |
| 110 | + try { |
| 111 | + service.schedule("run_1", delayedSnapshot()); |
| 112 | + service.schedule("run_1", { |
| 113 | + runnerId: "runner-1", |
| 114 | + runFriendlyId: "run_1", |
| 115 | + snapshotFriendlyId: "snapshot_2", |
| 116 | + }); |
| 117 | + |
| 118 | + await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); |
| 119 | + await sleep(SETTLE_MS); |
| 120 | + |
| 121 | + expect(snapshot).toHaveBeenCalledTimes(1); |
| 122 | + expect(snapshot).toHaveBeenCalledWith({ |
| 123 | + runnerId: "runner-1", |
| 124 | + metadata: { runId: "run_1", snapshotFriendlyId: "snapshot_2" }, |
| 125 | + }); |
| 126 | + } finally { |
| 127 | + service.stop(); |
| 128 | + } |
| 129 | + }); |
| 130 | +}); |
0 commit comments