-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathconcurrentFlushScheduler.test.ts
More file actions
161 lines (126 loc) · 5.26 KB
/
concurrentFlushScheduler.test.ts
File metadata and controls
161 lines (126 loc) · 5.26 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
import { ConcurrentFlushScheduler } from "~/services/runsReplicationService.server";
vi.setConfig({ testTimeout: 10_000 });
type TestItem = {
id: string;
event: "insert" | "update";
version: number;
};
describe("ConcurrentFlushScheduler", () => {
it("should deduplicate items by key, keeping the latest version", async () => {
const flushedBatches: TestItem[][] = [];
const scheduler = new ConcurrentFlushScheduler<TestItem>({
batchSize: 100,
flushInterval: 50,
maxConcurrency: 1,
callback: async (_flushId, batch) => {
flushedBatches.push([...batch]);
},
getKey: (item) => `${item.event}_${item.id}`,
shouldReplace: (existing, incoming) => incoming.version >= existing.version,
});
scheduler.start();
// Add items with duplicate keys but different versions
scheduler.addToBatch([
{ id: "run_1", event: "insert", version: 1 },
{ id: "run_1", event: "update", version: 2 },
{ id: "run_2", event: "insert", version: 1 },
]);
// Add more items - should merge with existing
scheduler.addToBatch([
{ id: "run_1", event: "insert", version: 3 }, // Higher version, should replace
{ id: "run_1", event: "update", version: 1 }, // Lower version, should NOT replace
{ id: "run_2", event: "update", version: 4 },
]);
// Wait for flush
await new Promise((resolve) => setTimeout(resolve, 100));
scheduler.shutdown();
// Should have flushed once with deduplicated items
expect(flushedBatches.length).toBeGreaterThanOrEqual(1);
const allFlushed = flushedBatches.flat();
// Find items by their key
const insertRun1 = allFlushed.find((i) => i.id === "run_1" && i.event === "insert");
const updateRun1 = allFlushed.find((i) => i.id === "run_1" && i.event === "update");
const insertRun2 = allFlushed.find((i) => i.id === "run_2" && i.event === "insert");
const updateRun2 = allFlushed.find((i) => i.id === "run_2" && i.event === "update");
// Verify correct versions were kept
expect(insertRun1?.version).toBe(3); // Latest version for insert_run_1
expect(updateRun1?.version).toBe(2); // Original update_run_1 (v1 didn't replace v2)
expect(insertRun2?.version).toBe(1); // Only version for insert_run_2
expect(updateRun2?.version).toBe(4); // Only version for update_run_2
});
it("should skip items where getKey returns null", async () => {
const flushedBatches: TestItem[][] = [];
const scheduler = new ConcurrentFlushScheduler<TestItem>({
batchSize: 100,
flushInterval: 50,
maxConcurrency: 1,
callback: async (_flushId, batch) => {
flushedBatches.push([...batch]);
},
getKey: (item) => {
if (!item.id) {
return null;
}
return `${item.event}_${item.id}`;
},
shouldReplace: (existing, incoming) => incoming.version >= existing.version,
});
scheduler.start();
scheduler.addToBatch([
{ id: "run_1", event: "insert", version: 1 },
{ id: "", event: "insert", version: 2 }, // Should be skipped (null key)
{ id: "run_2", event: "insert", version: 1 },
]);
await new Promise((resolve) => setTimeout(resolve, 100));
scheduler.shutdown();
const allFlushed = flushedBatches.flat();
expect(allFlushed).toHaveLength(2);
expect(allFlushed.map((i) => i.id).sort()).toEqual(["run_1", "run_2"]);
});
it("should flush when batch size threshold is reached", async () => {
const flushedBatches: TestItem[][] = [];
const scheduler = new ConcurrentFlushScheduler<TestItem>({
batchSize: 3,
flushInterval: 10000, // Long interval so timer doesn't trigger
maxConcurrency: 1,
callback: async (_flushId, batch) => {
flushedBatches.push([...batch]);
},
getKey: (item) => `${item.event}_${item.id}`,
shouldReplace: (existing, incoming) => incoming.version >= existing.version,
});
scheduler.start();
// Add 3 unique items - should trigger flush
scheduler.addToBatch([
{ id: "run_1", event: "insert", version: 1 },
{ id: "run_2", event: "insert", version: 1 },
{ id: "run_3", event: "insert", version: 1 },
]);
await new Promise((resolve) => setTimeout(resolve, 50));
expect(flushedBatches.length).toBe(1);
expect(flushedBatches[0]).toHaveLength(3);
scheduler.shutdown();
});
it("should respect shouldReplace returning false", async () => {
const flushedBatches: TestItem[][] = [];
const scheduler = new ConcurrentFlushScheduler<TestItem>({
batchSize: 100,
flushInterval: 50,
maxConcurrency: 1,
callback: async (_flushId, batch) => {
flushedBatches.push([...batch]);
},
getKey: (item) => `${item.event}_${item.id}`,
// Never replace - first item wins
shouldReplace: () => false,
});
scheduler.start();
scheduler.addToBatch([{ id: "run_1", event: "insert", version: 10 }]);
scheduler.addToBatch([{ id: "run_1", event: "insert", version: 999 }]);
await new Promise((resolve) => setTimeout(resolve, 100));
scheduler.shutdown();
const allFlushed = flushedBatches.flat();
const insertRun1 = allFlushed.find((i) => i.id === "run_1" && i.event === "insert");
expect(insertRun1?.version).toBe(10); // First one wins
});
});