|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { BackpressureMonitor, type BackpressureSignalSource } from "./backpressureMonitor.js"; |
| 3 | + |
| 4 | +function countingSource(verdict: { engaged: boolean } | null): { |
| 5 | + source: BackpressureSignalSource; |
| 6 | + reads: () => number; |
| 7 | +} { |
| 8 | + let reads = 0; |
| 9 | + return { |
| 10 | + source: { |
| 11 | + read: async () => { |
| 12 | + reads++; |
| 13 | + return verdict; |
| 14 | + }, |
| 15 | + }, |
| 16 | + reads: () => reads, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe("BackpressureMonitor", () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.useFakeTimers(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + vi.useRealTimers(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("when disabled, never skips dequeue and never reads the signal source", () => { |
| 30 | + // Even though the source would report "engaged", a disabled monitor must be |
| 31 | + // a complete no-op: this is the backwards-compatibility guarantee. |
| 32 | + const { source, reads } = countingSource({ engaged: true }); |
| 33 | + const monitor = new BackpressureMonitor({ enabled: false, source }); |
| 34 | + |
| 35 | + monitor.start(); |
| 36 | + |
| 37 | + expect(monitor.shouldSkipDequeue()).toBe(false); |
| 38 | + expect(reads()).toBe(0); |
| 39 | + |
| 40 | + monitor.stop(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("when enabled and the source reports engaged, skips dequeue after a refresh", async () => { |
| 44 | + const { source } = countingSource({ engaged: true }); |
| 45 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 46 | + |
| 47 | + monitor.start(); |
| 48 | + await vi.advanceTimersByTimeAsync(0); // flush the initial async read |
| 49 | + |
| 50 | + expect(monitor.shouldSkipDequeue()).toBe(true); |
| 51 | + |
| 52 | + monitor.stop(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("when enabled and the source reports clear, does not skip dequeue", async () => { |
| 56 | + const { source } = countingSource({ engaged: false }); |
| 57 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 58 | + |
| 59 | + monitor.start(); |
| 60 | + await vi.advanceTimersByTimeAsync(0); |
| 61 | + |
| 62 | + expect(monitor.shouldSkipDequeue()).toBe(false); |
| 63 | + |
| 64 | + monitor.stop(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("fails open (stops skipping) when the source throws", async () => { |
| 68 | + let call = 0; |
| 69 | + const source: BackpressureSignalSource = { |
| 70 | + read: async () => { |
| 71 | + call++; |
| 72 | + if (call === 1) { |
| 73 | + return { engaged: true }; |
| 74 | + } |
| 75 | + throw new Error("signal source unreachable"); |
| 76 | + }, |
| 77 | + }; |
| 78 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 79 | + |
| 80 | + monitor.start(); |
| 81 | + await vi.advanceTimersByTimeAsync(0); |
| 82 | + expect(monitor.shouldSkipDequeue()).toBe(true); // engaged from the first read |
| 83 | + |
| 84 | + await vi.advanceTimersByTimeAsync(1000); // next refresh throws |
| 85 | + expect(monitor.shouldSkipDequeue()).toBe(false); // fail-open: a dead source must not pin the brake |
| 86 | + |
| 87 | + monitor.stop(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("fails open when the source reports unknown (null)", async () => { |
| 91 | + const { source } = countingSource(null); |
| 92 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 93 | + |
| 94 | + monitor.start(); |
| 95 | + await vi.advanceTimersByTimeAsync(0); |
| 96 | + |
| 97 | + expect(monitor.shouldSkipDequeue()).toBe(false); |
| 98 | + |
| 99 | + monitor.stop(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("fails open when the cached verdict goes stale (older than max age)", async () => { |
| 103 | + // Source stops updating (e.g. hangs) after the first read; the verdict ages out. |
| 104 | + const source: BackpressureSignalSource = { |
| 105 | + read: async () => ({ engaged: true, ts: Date.now() }), |
| 106 | + }; |
| 107 | + const monitor = new BackpressureMonitor({ |
| 108 | + enabled: true, |
| 109 | + source, |
| 110 | + refreshIntervalMs: 1_000_000, // effectively only the initial read fires |
| 111 | + maxVerdictAgeMs: 15_000, |
| 112 | + }); |
| 113 | + |
| 114 | + monitor.start(); |
| 115 | + await vi.advanceTimersByTimeAsync(0); |
| 116 | + expect(monitor.shouldSkipDequeue()).toBe(true); |
| 117 | + |
| 118 | + await vi.advanceTimersByTimeAsync(15_001); // verdict now older than max age |
| 119 | + expect(monitor.shouldSkipDequeue()).toBe(false); |
| 120 | + |
| 121 | + monitor.stop(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("does not read the source on the hot path (reads are driven by the refresh tick)", async () => { |
| 125 | + const { source, reads } = countingSource({ engaged: true }); |
| 126 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 127 | + |
| 128 | + monitor.start(); |
| 129 | + await vi.advanceTimersByTimeAsync(0); |
| 130 | + expect(reads()).toBe(1); // just the initial refresh |
| 131 | + |
| 132 | + for (let i = 0; i < 1000; i++) { |
| 133 | + monitor.shouldSkipDequeue(); |
| 134 | + } |
| 135 | + |
| 136 | + expect(reads()).toBe(1); // hot-path calls performed zero I/O |
| 137 | + |
| 138 | + monitor.stop(); |
| 139 | + }); |
| 140 | + |
| 141 | + it("stops refreshing after stop()", async () => { |
| 142 | + const { source, reads } = countingSource({ engaged: true }); |
| 143 | + const monitor = new BackpressureMonitor({ enabled: true, source, refreshIntervalMs: 1000 }); |
| 144 | + |
| 145 | + monitor.start(); |
| 146 | + await vi.advanceTimersByTimeAsync(0); |
| 147 | + const readsAtStop = reads(); |
| 148 | + |
| 149 | + monitor.stop(); |
| 150 | + await vi.advanceTimersByTimeAsync(5000); |
| 151 | + |
| 152 | + expect(reads()).toBe(readsAtStop); |
| 153 | + }); |
| 154 | +}); |
0 commit comments