|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { installWorkerPlacementReconcileGuard } from "./server-worker-placement-reconcile-guard.js"; |
| 3 | + |
| 4 | +const localClaim = { |
| 5 | + owner: "local" as const, |
| 6 | + claimId: "claim-cleanup", |
| 7 | + runId: "run-cleanup", |
| 8 | + generation: 1, |
| 9 | + ownerEpoch: null, |
| 10 | +}; |
| 11 | + |
| 12 | +function createFailedPlacementGuard(params: { |
| 13 | + turnClaim: typeof localClaim | null; |
| 14 | + activeOwnerEpoch: number | null; |
| 15 | + destroyRequestedAtMs: number | null; |
| 16 | +}) { |
| 17 | + let guard: |
| 18 | + | ((environmentId: string, reconcileCore: () => Promise<void>) => Promise<void>) |
| 19 | + | undefined; |
| 20 | + const resumeProvisioning = vi.fn(); |
| 21 | + installWorkerPlacementReconcileGuard({ |
| 22 | + placements: { |
| 23 | + list: () => [ |
| 24 | + { |
| 25 | + sessionId: "session-cleanup", |
| 26 | + state: "failed", |
| 27 | + environmentId: "worker-cleanup", |
| 28 | + turnClaim: params.turnClaim, |
| 29 | + activeOwnerEpoch: params.activeOwnerEpoch, |
| 30 | + }, |
| 31 | + ], |
| 32 | + } as never, |
| 33 | + environments: { |
| 34 | + get: (environmentId: string) => ({ |
| 35 | + environmentId, |
| 36 | + state: "provisioning", |
| 37 | + destroyRequestedAtMs: params.destroyRequestedAtMs, |
| 38 | + }), |
| 39 | + installReconcileEnvironmentGuard: (installed: typeof guard) => { |
| 40 | + guard = installed; |
| 41 | + return async () => {}; |
| 42 | + }, |
| 43 | + } as never, |
| 44 | + dispatch: { resumeProvisioning } as never, |
| 45 | + isStopping: () => false, |
| 46 | + }); |
| 47 | + if (!guard) { |
| 48 | + throw new Error("worker placement reconciliation guard was not installed"); |
| 49 | + } |
| 50 | + return { guard, resumeProvisioning }; |
| 51 | +} |
| 52 | + |
| 53 | +describe("worker placement reconciliation teardown authority", () => { |
| 54 | + it("allows destruction only after its exact failed owner has released all authority", async () => { |
| 55 | + const { guard, resumeProvisioning } = createFailedPlacementGuard({ |
| 56 | + turnClaim: null, |
| 57 | + activeOwnerEpoch: null, |
| 58 | + destroyRequestedAtMs: 1, |
| 59 | + }); |
| 60 | + const reconcileCore = vi.fn(async () => {}); |
| 61 | + |
| 62 | + await guard("worker-cleanup", reconcileCore); |
| 63 | + |
| 64 | + expect(reconcileCore).toHaveBeenCalledOnce(); |
| 65 | + expect(resumeProvisioning).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each([ |
| 69 | + { |
| 70 | + reason: "a retained local turn claim", |
| 71 | + turnClaim: localClaim, |
| 72 | + activeOwnerEpoch: null, |
| 73 | + destroyRequestedAtMs: 1, |
| 74 | + }, |
| 75 | + { |
| 76 | + reason: "an active owner epoch", |
| 77 | + turnClaim: null, |
| 78 | + activeOwnerEpoch: 2, |
| 79 | + destroyRequestedAtMs: 1, |
| 80 | + }, |
| 81 | + { |
| 82 | + reason: "no durable destruction request", |
| 83 | + turnClaim: null, |
| 84 | + activeOwnerEpoch: null, |
| 85 | + destroyRequestedAtMs: null, |
| 86 | + }, |
| 87 | + ])("keeps failed-placement cleanup fenced with $reason", async ({ reason: _, ...params }) => { |
| 88 | + const { guard, resumeProvisioning } = createFailedPlacementGuard(params); |
| 89 | + const reconcileCore = vi.fn(async () => {}); |
| 90 | + |
| 91 | + await expect(guard("worker-cleanup", reconcileCore)).rejects.toThrow( |
| 92 | + "provisioning owner is failed", |
| 93 | + ); |
| 94 | + |
| 95 | + expect(reconcileCore).not.toHaveBeenCalled(); |
| 96 | + expect(resumeProvisioning).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments