Repository navigation
Expand file tree
/
Copy pathdispatch-loop.test.ts
More file actions
294 lines (256 loc) · 11 KB
/
Copy pathdispatch-loop.test.ts
File metadata and controls
294 lines (256 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* #29919 — `DispatchLoop` wakes at the next due time, and a sole writer's loop
* does not re-run the idle ramp after a cold start.
*
* Before #29919 every `start()` re-ran the backoff's ramp from `intervalMs`
* against stores that were empty: with the dispatchers' 500 ms base, six ticks
* in a fresh loop's first minute (0, 1, 3, 7, 15, 31 s), every one idle, while the only thing the ramp ever bought — early
* notice of a row another process wrote — cannot happen on a sole writer. And
* a deferred row was noticed only at the next backed-off tick, up to the
* ceiling late.
*
* Every leg runs on vitest's fake timers, so "when did a tick start" is an
* exact reading rather than a sleep-and-hope.
*/
import { describe, it, expect, afterEach, vi } from 'vitest';
import { DispatchLoop, type DispatchLoopOptions } from './dispatch-loop.js';
const BASE = 500;
const MINUTE = 60_000;
const FIVE_MINUTES = 5 * MINUTE;
afterEach(() => { vi.useRealTimers(); });
/** Fake timers with the clock at 0, so a tick's start reads as ms since `start()`. */
function useClockAtZero(): void {
vi.useFakeTimers();
vi.setSystemTime(0);
}
/**
* A store of deferred rows: `runTick` claims every row due by the fake clock,
* `nextDueAt` answers the earliest row still in the future. Both are recorded.
*/
function harness(options: Partial<DispatchLoopOptions> & { dueRows?: number[] } = {}) {
const dueRows = [...(options.dueRows ?? [])];
const tickStarts: number[] = [];
const claimedAt: number[] = [];
let dueReads = 0;
const errors: unknown[] = [];
const loop = new DispatchLoop({
intervalMs: BASE,
runTick: async () => {
const now = Date.now();
tickStarts.push(now);
let claimed = 0;
for (let i = dueRows.length - 1; i >= 0; i--) {
if (dueRows[i]! <= now) {
dueRows.splice(i, 1);
claimedAt.push(now);
claimed++;
}
}
return claimed;
},
nextDueAt: async () => {
dueReads++;
const future = dueRows.filter((t) => t > Date.now());
return future.length > 0 ? Math.min(...future) : null;
},
onTickError: (err) => { errors.push(err); },
...options,
});
return {
loop,
tickStarts,
claimedAt,
errors,
dueRows,
get dueReads() { return dueReads; },
};
}
describe('#29919 DispatchLoop — a sole writer does not ramp from a cold start', () => {
it('a fresh loop on an empty store ticks ONCE in its first minute', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES });
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.tickStarts).toEqual([0]);
// The one tick's one due read — nothing deferred, so the ceiling.
expect(h.dueReads).toBe(1);
});
it('CONTROL: the same loop without soleWriter still runs the ramp it always had', async () => {
useClockAtZero();
const h = harness({ maxIdleIntervalMs: FIVE_MINUTES });
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
// 1, 2, 4, 8, 16 s apart — six ticks. The harness can see a ramp, so the
// single tick above is a reading about soleWriter, not a dead clock.
expect(h.tickStarts).toEqual([0, 1_000, 3_000, 7_000, 15_000, 31_000]);
});
it('without a nextDueAt reader, soleWriter alone sleeps straight to the ceiling', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: 30_000, nextDueAt: undefined });
h.loop.start();
await vi.advanceTimersByTimeAsync(2 * MINUTE);
await h.loop.stop();
expect(h.tickStarts).toEqual([0, 30_000, 60_000, 90_000, 120_000]);
});
it('steady state costs no extra read: an idle hour at the ceiling reads the due time once', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: 30_000 });
h.loop.start();
await vi.advanceTimersByTimeAsync(60 * MINUTE);
await h.loop.stop();
// 3600 s / 30 s, plus the tick start() runs.
expect(h.tickStarts).toHaveLength(121);
expect(h.dueReads).toBe(1);
});
});
describe('#29919 DispatchLoop — wakes at the next due time', () => {
it('a row due at T+20 s is claimed at T+20 s, under a five-minute ceiling', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES, dueRows: [20_000] });
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.claimedAt).toEqual([20_000]);
// The cold-start tick, the due tick, and the tick after a claim
// (`intervalMs` later) — then nothing is deferred and the ceiling wins.
expect(h.tickStarts).toEqual([0, 20_000, 20_500]);
});
it('the ramp keeps its shape, cut short only by the due time (no soleWriter)', async () => {
useClockAtZero();
const h = harness({ maxIdleIntervalMs: FIVE_MINUTES, dueRows: [2_000] });
h.loop.start();
await vi.advanceTimersByTimeAsync(4_000);
await h.loop.stop();
expect(h.claimedAt).toEqual([2_000]);
expect(h.tickStarts).toEqual([0, 1_000, 2_000, 2_500, 3_500]);
});
it('consecutive deferred rows are each claimed on time — the read is re-asked after each due time', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES, dueRows: [10_000, 40_000] });
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.claimedAt).toEqual([10_000, 40_000]);
});
it('wakeAt(): a deferred row written later in the streak is claimed on time with no read', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES });
h.loop.start();
await vi.advanceTimersByTimeAsync(10_000);
const readsBefore = h.dueReads;
const ticksBefore = h.tickStarts.length;
h.dueRows.push(35_000);
h.loop.wakeAt(35_000);
// No tick now: the hint only moves the timer.
await vi.advanceTimersByTimeAsync(1);
expect(h.tickStarts).toHaveLength(ticksBefore);
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.claimedAt).toEqual([35_000]);
// The hint cost no read; the read after the claim is the next streak's.
expect(h.dueReads).toBe(readsBefore + 1);
});
it('wakeAt() keeps a timer that is already due sooner', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES, dueRows: [20_000] });
h.loop.start();
await vi.advanceTimersByTimeAsync(1);
h.dueRows.push(50_000);
h.loop.wakeAt(50_000);
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.claimedAt).toEqual([20_000, 50_000]);
});
});
describe('#29919 DispatchLoop — wake() still delivers at once', () => {
it('a wake on a sole writer sleeping towards its ceiling ticks immediately', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES });
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
h.dueRows.push(MINUTE);
h.loop.wake();
await vi.advanceTimersByTimeAsync(0);
await h.loop.stop();
expect(h.claimedAt).toEqual([MINUTE]);
});
});
describe('#29919 DispatchLoop — nothing it reads can make it spin', () => {
it('a rejected tick counts as idle: a failing store is ticked once per ceiling, not hammered', async () => {
useClockAtZero();
const h = harness({
soleWriter: true,
maxIdleIntervalMs: 30_000,
runTick: async () => { h.tickStarts.push(Date.now()); throw new Error('store down'); },
});
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.tickStarts).toEqual([0, 30_000, 60_000]);
expect(h.errors).toHaveLength(3);
// A rejected tick asks for no due time.
expect(h.dueReads).toBe(0);
});
it('a rejected tick after a due time was learned still sleeps until that due time, not 0 ms', async () => {
useClockAtZero();
const tickStarts: number[] = [];
const loop = new DispatchLoop({
intervalMs: BASE,
maxIdleIntervalMs: FIVE_MINUTES,
soleWriter: true,
// The cold-start tick finds nothing; every later tick rejects.
runTick: async () => {
tickStarts.push(Date.now());
if (tickStarts.length > 1) throw new Error('store down');
return 0;
},
nextDueAt: async () => 20_000,
onTickError: () => {},
});
loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await loop.stop();
// The due tick at 20 s rejects; the next is a ceiling away.
expect(tickStarts).toEqual([0, 20_000]);
});
it('a due time at or before the tick that found nothing is not chased (visible, but not this loop\'s to claim)', async () => {
useClockAtZero();
const h = harness({
soleWriter: true,
maxIdleIntervalMs: 30_000,
// Another node holds the row's partition: it is due, and never claimable here.
runTick: async () => { h.tickStarts.push(Date.now()); return 0; },
nextDueAt: async () => 0,
});
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.tickStarts).toEqual([0, 30_000, 60_000]);
});
it('a rejected due read is reported, and the loop sleeps by its backoff alone', async () => {
useClockAtZero();
const h = harness({
soleWriter: true,
maxIdleIntervalMs: 30_000,
nextDueAt: async () => { throw new Error('read failed'); },
});
h.loop.start();
await vi.advanceTimersByTimeAsync(MINUTE);
await h.loop.stop();
expect(h.tickStarts).toEqual([0, 30_000, 60_000]);
expect(h.errors).toHaveLength(3);
});
it('stop() is final: a wakeAt() after it is a no-op', async () => {
useClockAtZero();
const h = harness({ soleWriter: true, maxIdleIntervalMs: FIVE_MINUTES });
h.loop.start();
await vi.advanceTimersByTimeAsync(1);
await h.loop.stop();
h.loop.wakeAt(10_000);
await vi.advanceTimersByTimeAsync(MINUTE);
expect(h.tickStarts).toEqual([0]);
});
});