|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { RenderTooSlowError, checkRenderEta, startRenderEtaGuard } from "./renderEtaGuard.js"; |
| 3 | + |
| 4 | +const RENDER_TOO_SLOW = "RENDER_TOO_SLOW"; |
| 5 | + |
| 6 | +describe("checkRenderEta", () => { |
| 7 | + const base = { maxRenderMs: 100_000, elapsedMs: 0, framesCompleted: 0, totalFrames: 100 }; |
| 8 | + |
| 9 | + it("passes at the 1.2x margin and fails just over it", () => { |
| 10 | + expect(checkRenderEta("capture", { ...base, msPerFrame: 1_200 })).toBeUndefined(); |
| 11 | + const error = checkRenderEta("capture", { ...base, msPerFrame: 1_201 }); |
| 12 | + expect(error).toBeInstanceOf(RenderTooSlowError); |
| 13 | + expect(error?.message).toMatch(/^render too slow:/); |
| 14 | + expect(error?.publicMetadata).toMatchObject({ stage: "capture", etaMs: 120_100 }); |
| 15 | + // The producer server reads these off the error to build the wire envelope. |
| 16 | + expect(error?.code).toBe(RENDER_TOO_SLOW); |
| 17 | + expect(error?.owner).toBe("user"); |
| 18 | + expect(error?.retryable).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it("counts time already spent", () => { |
| 22 | + expect( |
| 23 | + checkRenderEta("capture", { ...base, elapsedMs: 30_000, msPerFrame: 1_000 }), |
| 24 | + ).toBeInstanceOf(RenderTooSlowError); |
| 25 | + }); |
| 26 | + |
| 27 | + it("never fires without a budget or without an estimate", () => { |
| 28 | + expect( |
| 29 | + checkRenderEta("capture", { ...base, maxRenderMs: undefined, msPerFrame: 1e9 }), |
| 30 | + ).toBeUndefined(); |
| 31 | + expect(checkRenderEta("capture", { ...base, maxRenderMs: 0, msPerFrame: 1e9 })).toBeUndefined(); |
| 32 | + expect(checkRenderEta("capture", { ...base, msPerFrame: 0 })).toBeUndefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("matches the two prod renders on the 90 minute budget", () => { |
| 36 | + const budget = 90 * 60_000; |
| 37 | + // 21540 frames, 4 workers, p95 784 ms: the calibration estimate (70 min) is under budget. |
| 38 | + expect( |
| 39 | + checkRenderEta("capture_calibration", { |
| 40 | + maxRenderMs: budget, |
| 41 | + elapsedMs: 0, |
| 42 | + framesCompleted: 0, |
| 43 | + totalFrames: 21_540, |
| 44 | + msPerFrame: 784 / 4, |
| 45 | + }), |
| 46 | + ).toBeUndefined(); |
| 47 | + // 570 frames, 1 worker, p95 15.8 s: 150 min, caught before capture. |
| 48 | + expect( |
| 49 | + checkRenderEta("capture_calibration", { |
| 50 | + maxRenderMs: budget, |
| 51 | + elapsedMs: 600_000, |
| 52 | + framesCompleted: 0, |
| 53 | + totalFrames: 570, |
| 54 | + msPerFrame: 15_800 / 1, |
| 55 | + }), |
| 56 | + ).toBeInstanceOf(RenderTooSlowError); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("startRenderEtaGuard", () => { |
| 61 | + beforeEach(() => vi.useFakeTimers()); |
| 62 | + afterEach(() => vi.useRealTimers()); |
| 63 | + |
| 64 | + /** Advances 5 s per step, adding `framesPerStep` frames, until tripped or `steps` run out. */ |
| 65 | + function run(opts: { |
| 66 | + maxRenderMs?: number; |
| 67 | + framesPerStep: number; |
| 68 | + steps: number; |
| 69 | + total: number; |
| 70 | + }) { |
| 71 | + const job = { framesRendered: 0 }; |
| 72 | + let tripped: RenderTooSlowError | undefined; |
| 73 | + const stop = startRenderEtaGuard({ |
| 74 | + maxRenderMs: opts.maxRenderMs, |
| 75 | + job, |
| 76 | + totalFrames: opts.total, |
| 77 | + pipelineStartMs: Date.now(), |
| 78 | + onTooSlow: (error) => (tripped = error), |
| 79 | + }); |
| 80 | + let at = 0; |
| 81 | + for (let i = 0; i < opts.steps && !tripped; i++) { |
| 82 | + job.framesRendered += opts.framesPerStep; |
| 83 | + vi.advanceTimersByTime(5_000); |
| 84 | + at += 5_000; |
| 85 | + } |
| 86 | + stop(); |
| 87 | + return { tripped, at, job }; |
| 88 | + } |
| 89 | + |
| 90 | + it("fails a crawling render within about two minutes of capture", () => { |
| 91 | + const { tripped, at } = run({ |
| 92 | + maxRenderMs: 120_000, |
| 93 | + framesPerStep: 1, |
| 94 | + steps: 100, |
| 95 | + total: 1_000, |
| 96 | + }); |
| 97 | + expect(tripped?.message).toMatch(/^render too slow:/); |
| 98 | + expect(at).toBeLessThanOrEqual(120_000); |
| 99 | + expect(tripped?.publicMetadata).toMatchObject({ |
| 100 | + stage: "capture", |
| 101 | + totalFrames: 1_000, |
| 102 | + maxRenderMs: 120_000, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("leaves a render that will finish inside the budget alone", () => { |
| 107 | + expect( |
| 108 | + run({ maxRenderMs: 10_000_000, framesPerStep: 100, steps: 100, total: 100_000 }).tripped, |
| 109 | + ).toBeUndefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("is never tripped by the encode and assemble time after capture completes", () => { |
| 113 | + const job = { framesRendered: 0 }; |
| 114 | + const onTooSlow = vi.fn(); |
| 115 | + const stop = startRenderEtaGuard({ |
| 116 | + maxRenderMs: 120_000, |
| 117 | + job, |
| 118 | + totalFrames: 100, |
| 119 | + pipelineStartMs: Date.now(), |
| 120 | + onTooSlow, |
| 121 | + }); |
| 122 | + // Capture finishes inside the budget... |
| 123 | + for (let i = 0; i < 10; i++) { |
| 124 | + job.framesRendered += 10; |
| 125 | + vi.advanceTimersByTime(5_000); |
| 126 | + } |
| 127 | + // ...then a mux that runs for an hour with the counter parked at the total. |
| 128 | + vi.advanceTimersByTime(60 * 60_000); |
| 129 | + stop(); |
| 130 | + expect(onTooSlow).not.toHaveBeenCalled(); |
| 131 | + expect(vi.getTimerCount()).toBe(0); |
| 132 | + }); |
| 133 | + |
| 134 | + it("is a no-op without a budget", () => { |
| 135 | + expect( |
| 136 | + run({ maxRenderMs: undefined, framesPerStep: 1, steps: 200, total: 1_000 }).tripped, |
| 137 | + ).toBeUndefined(); |
| 138 | + expect( |
| 139 | + run({ maxRenderMs: 0, framesPerStep: 1, steps: 200, total: 1_000 }).tripped, |
| 140 | + ).toBeUndefined(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("does not decide on zero frames (the stall watchdog owns that)", () => { |
| 144 | + expect( |
| 145 | + run({ maxRenderMs: 1_000, framesPerStep: 0, steps: 200, total: 1_000 }).tripped, |
| 146 | + ).toBeUndefined(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments