Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationRecovery.test.ts

306 lines8.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 createOrchestrationRecoveryCoordinator,
5 deriveReplayRetryDecision,
6} from "./orchestrationRecovery";
7​
8describe("createOrchestrationRecoveryCoordinator", () => {
9 it("defers live events until bootstrap completes and then requests replay", () => {
10 const coordinator = createOrchestrationRecoveryCoordinator();
11​
12 expect(coordinator.beginSnapshotRecovery("bootstrap")).toBe(true);
13 expect(coordinator.classifyDomainEvent(4)).toBe("defer");
14​
15 expect(coordinator.completeSnapshotRecovery(2)).toBe(true);
16 expect(coordinator.getState()).toMatchObject({
17 latestSequence: 2,
18 highestObservedSequence: 4,
19 bootstrapped: true,
20 pendingReplay: false,
21 inFlight: null,
22 });
23 });
24​
25 it("classifies sequence gaps as recovery-only replay work", () => {
26 const coordinator = createOrchestrationRecoveryCoordinator();
27​
28 coordinator.beginSnapshotRecovery("bootstrap");
29 coordinator.completeSnapshotRecovery(3);
30​
31 expect(coordinator.classifyDomainEvent(5)).toBe("recover");
32 expect(coordinator.beginReplayRecovery("sequence-gap")).toBe(true);
33 expect(coordinator.getState().inFlight).toEqual({
34 kind: "replay",
35 reason: "sequence-gap",
36 });
37 });
38​
39 it("tracks live event batches without entering recovery", () => {
40 const coordinator = createOrchestrationRecoveryCoordinator();
41​
42 coordinator.beginSnapshotRecovery("bootstrap");
43 coordinator.completeSnapshotRecovery(3);
44​
45 expect(coordinator.classifyDomainEvent(4)).toBe("apply");
46 expect(coordinator.markEventBatchApplied([{ sequence: 4 }])).toEqual([{ sequence: 4 }]);
47 expect(coordinator.getState()).toMatchObject({
48 latestSequence: 4,
49 highestObservedSequence: 4,
50 bootstrapped: true,
51 inFlight: null,
52 });
53 });
54​
55 it("requests another replay when deferred events arrive during replay recovery", () => {
56 const coordinator = createOrchestrationRecoveryCoordinator();
57​
58 coordinator.beginSnapshotRecovery("bootstrap");
59 coordinator.completeSnapshotRecovery(3);
60 coordinator.classifyDomainEvent(5);
61 coordinator.beginReplayRecovery("sequence-gap");
62 coordinator.classifyDomainEvent(7);
63 coordinator.markEventBatchApplied([{ sequence: 4 }, { sequence: 5 }, { sequence: 6 }]);
64​
65 expect(coordinator.completeReplayRecovery()).toEqual({
66 replayMadeProgress: true,
67 shouldReplay: true,
68 });
69 });
70​
71 it("retries replay when no progress was made but higher live sequences were observed", () => {
72 const coordinator = createOrchestrationRecoveryCoordinator();
73​
74 coordinator.beginSnapshotRecovery("bootstrap");
75 coordinator.completeSnapshotRecovery(3);
76 coordinator.classifyDomainEvent(5);
77 coordinator.beginReplayRecovery("sequence-gap");
78​
79 expect(coordinator.completeReplayRecovery()).toEqual({
80 replayMadeProgress: false,
81 shouldReplay: true,
82 });
83 expect(coordinator.getState()).toMatchObject({
84 latestSequence: 3,
85 highestObservedSequence: 5,
86 pendingReplay: false,
87 inFlight: null,
88 });
89 });
90​
91 it("does not request another replay when a replay made no progress and nothing newer was observed", () => {
92 const coordinator = createOrchestrationRecoveryCoordinator();
93​
94 coordinator.beginSnapshotRecovery("bootstrap");
95 coordinator.completeSnapshotRecovery(3);
96 coordinator.beginReplayRecovery("sequence-gap");
97​
98 expect(coordinator.completeReplayRecovery()).toEqual({
99 replayMadeProgress: false,
100 shouldReplay: false,
101 });
102 });
103​
104 it("marks replay failure as unbootstrapped so snapshot fallback is recovery-only", () => {
105 const coordinator = createOrchestrationRecoveryCoordinator();
106​
107 coordinator.beginSnapshotRecovery("bootstrap");
108 coordinator.completeSnapshotRecovery(3);
109 coordinator.beginReplayRecovery("sequence-gap");
110 coordinator.failReplayRecovery();
111​
112 expect(coordinator.getState()).toMatchObject({
113 bootstrapped: false,
114 inFlight: null,
115 });
116 expect(coordinator.beginSnapshotRecovery("replay-failed")).toBe(true);
117 expect(coordinator.getState().inFlight).toEqual({
118 kind: "snapshot",
119 reason: "replay-failed",
120 });
121 });
122​
123 it("keeps enough state to explain why bootstrap snapshot recovery requests replay", () => {
124 const coordinator = createOrchestrationRecoveryCoordinator();
125​
126 expect(coordinator.beginSnapshotRecovery("bootstrap")).toBe(true);
127 expect(coordinator.classifyDomainEvent(4)).toBe("defer");
128 expect(coordinator.completeSnapshotRecovery(2)).toBe(true);
129​
130 expect(coordinator.getState()).toMatchObject({
131 latestSequence: 2,
132 highestObservedSequence: 4,
133 bootstrapped: true,
134 pendingReplay: false,
135 inFlight: null,
136 });
137 });
138​
139 it("reports skip state when snapshot recovery is requested while replay is in flight", () => {
140 const coordinator = createOrchestrationRecoveryCoordinator();
141​
142 coordinator.beginSnapshotRecovery("bootstrap");
143 coordinator.completeSnapshotRecovery(3);
144 expect(coordinator.beginReplayRecovery("sequence-gap")).toBe(true);
145​
146 expect(coordinator.beginSnapshotRecovery("bootstrap")).toBe(false);
147 expect(coordinator.getState()).toMatchObject({
148 pendingReplay: true,
149 inFlight: {
150 kind: "replay",
151 reason: "sequence-gap",
152 },
153 });
154 });
155});
156​
157describe("deriveReplayRetryDecision", () => {
158 it("retries immediately when replay made progress", () => {
159 expect(
160 deriveReplayRetryDecision({
161 previousTracker: {
162 attempts: 2,
163 latestSequence: 3,
164 highestObservedSequence: 5,
165 },
166 completion: {
167 replayMadeProgress: true,
168 shouldReplay: true,
169 },
170 recoveryState: {
171 latestSequence: 5,
172 highestObservedSequence: 5,
173 },
174 baseDelayMs: 100,
175 maxNoProgressRetries: 3,
176 }),
177 ).toEqual({
178 shouldRetry: true,
179 delayMs: 0,
180 tracker: null,
181 });
182 });
183​
184 it("caps no-progress retries for the same frontier", () => {
185 const first = deriveReplayRetryDecision({
186 previousTracker: null,
187 completion: {
188 replayMadeProgress: false,
189 shouldReplay: true,
190 },
191 recoveryState: {
192 latestSequence: 3,
193 highestObservedSequence: 5,
194 },
195 baseDelayMs: 100,
196 maxNoProgressRetries: 3,
197 });
198​
199 const second = deriveReplayRetryDecision({
200 previousTracker: first.tracker,
201 completion: {
202 replayMadeProgress: false,
203 shouldReplay: true,
204 },
205 recoveryState: {
206 latestSequence: 3,
207 highestObservedSequence: 5,
208 },
209 baseDelayMs: 100,
210 maxNoProgressRetries: 3,
211 });
212​
213 const third = deriveReplayRetryDecision({
214 previousTracker: second.tracker,
215 completion: {
216 replayMadeProgress: false,
217 shouldReplay: true,
218 },
219 recoveryState: {
220 latestSequence: 3,
221 highestObservedSequence: 5,
222 },
223 baseDelayMs: 100,
224 maxNoProgressRetries: 3,
225 });
226​
227 const fourth = deriveReplayRetryDecision({
228 previousTracker: third.tracker,
229 completion: {
230 replayMadeProgress: false,
231 shouldReplay: true,
232 },
233 recoveryState: {
234 latestSequence: 3,
235 highestObservedSequence: 5,
236 },
237 baseDelayMs: 100,
238 maxNoProgressRetries: 3,
239 });
240​
241 expect(first).toEqual({
242 shouldRetry: true,
243 delayMs: 100,
244 tracker: {
245 attempts: 1,
246 latestSequence: 3,
247 highestObservedSequence: 5,
248 },
249 });
250 expect(second).toEqual({
251 shouldRetry: true,
252 delayMs: 200,
253 tracker: {
254 attempts: 2,
255 latestSequence: 3,
256 highestObservedSequence: 5,
257 },
258 });
259 expect(third).toEqual({
260 shouldRetry: true,
261 delayMs: 400,
262 tracker: {
263 attempts: 3,
264 latestSequence: 3,
265 highestObservedSequence: 5,
266 },
267 });
268 expect(fourth).toEqual({
269 shouldRetry: false,
270 delayMs: 0,
271 tracker: null,
272 });
273 });
274​
275 it("resets the retry budget when the replay frontier changes", () => {
276 const exhausted = {
277 attempts: 3,
278 latestSequence: 3,
279 highestObservedSequence: 5,
280 };
281​
282 expect(
283 deriveReplayRetryDecision({
284 previousTracker: exhausted,
285 completion: {
286 replayMadeProgress: false,
287 shouldReplay: true,
288 },
289 recoveryState: {
290 latestSequence: 3,
291 highestObservedSequence: 6,
292 },
293 baseDelayMs: 100,
294 maxNoProgressRetries: 3,
295 }),
296 ).toEqual({
297 shouldRetry: true,
298 delayMs: 100,
299 tracker: {
300 attempts: 1,
301 latestSequence: 3,
302 highestObservedSequence: 6,
303 },
304 });
305 });
306});