Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

KeyScheduler.test.ts

291 lines7.0 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { KeyScheduler } from '../src/executor/KeyScheduler'
3import type { OfflineTransaction } from '../src/types'
4​
5function createTransaction({
6 id,
7 createdAt,
8 nextAttemptAt,
9 retryCount = 0,
10}: {
11 id: string
12 createdAt: Date
13 nextAttemptAt: number
14 retryCount?: number
15}): OfflineTransaction {
16 return {
17 id,
18 mutationFnName: `syncData`,
19 mutations: [],
20 keys: [],
21 idempotencyKey: `idempotency-${id}`,
22 createdAt,
23 retryCount,
24 nextAttemptAt,
25 metadata: {},
26 version: 1,
27 }
29​
30describe(`KeyScheduler`, () => {
31 afterEach(() => {
32 vi.useRealTimers()
33 })
34​
35 it(`does not execute a later ready transaction while an earlier retry is pending`, () => {
36 vi.useFakeTimers()
37​
38 const now = new Date(`2026-01-01T00:00:00.000Z`)
39 vi.setSystemTime(now)
40​
41 const scheduler = new KeyScheduler()
42 const first = createTransaction({
43 id: `first`,
44 createdAt: new Date(now.getTime()),
45 nextAttemptAt: now.getTime(),
46 })
47 const second = createTransaction({
48 id: `second`,
49 createdAt: new Date(now.getTime() + 1),
50 nextAttemptAt: now.getTime(),
51 })
52​
53 scheduler.schedule(first)
54 scheduler.schedule(second)
55​
56 const firstTx = scheduler.getNext()
57 expect(firstTx?.id).toBe(`first`)
58​
59 scheduler.markStarted(first)
60 scheduler.markFailed(first)
61 scheduler.updateTransaction({
62 ...first,
63 retryCount: 1,
64 nextAttemptAt: now.getTime() + 5000,
65 lastError: {
66 name: `Error`,
67 message: `network timeout`,
68 },
69 })
70​
71 const secondTx = scheduler.getNext()
72 expect(secondTx).toBeUndefined()
73 })
74​
75 it(`executes the first transaction once its retry delay has elapsed`, () => {
76 vi.useFakeTimers()
77​
78 const now = new Date(`2026-01-01T00:00:00.000Z`)
79 vi.setSystemTime(now)
80​
81 const scheduler = new KeyScheduler()
82 const first = createTransaction({
83 id: `first`,
84 createdAt: new Date(now.getTime()),
85 nextAttemptAt: now.getTime() + 5000,
86 retryCount: 1,
87 })
88 const second = createTransaction({
89 id: `second`,
90 createdAt: new Date(now.getTime() + 1),
91 nextAttemptAt: now.getTime(),
92 })
93​
94 scheduler.schedule(first)
95 scheduler.schedule(second)
96​
97 expect(scheduler.getNext()).toBeUndefined()
98​
99 vi.advanceTimersByTime(5000)
100​
101 const result = scheduler.getNext()
102 expect(result?.id).toBe(`first`)
103 })
104​
105 it(`processes the second transaction after the first completes`, () => {
106 vi.useFakeTimers()
107​
108 const now = new Date(`2026-01-01T00:00:00.000Z`)
109 vi.setSystemTime(now)
110​
111 const scheduler = new KeyScheduler()
112 const first = createTransaction({
113 id: `first`,
114 createdAt: new Date(now.getTime()),
115 nextAttemptAt: now.getTime(),
116 })
117 const second = createTransaction({
118 id: `second`,
119 createdAt: new Date(now.getTime() + 1),
120 nextAttemptAt: now.getTime(),
121 })
122​
123 scheduler.schedule(first)
124 scheduler.schedule(second)
125​
126 const tx1 = scheduler.getNext()
127 expect(tx1?.id).toBe(`first`)
128​
129 scheduler.markStarted(first)
130 scheduler.markCompleted(first)
131​
132 const tx2 = scheduler.getNext()
133 expect(tx2?.id).toBe(`second`)
134 })
135​
136 it(`returns nothing while a transaction is running`, () => {
137 vi.useFakeTimers()
138​
139 const now = new Date(`2026-01-01T00:00:00.000Z`)
140 vi.setSystemTime(now)
141​
142 const scheduler = new KeyScheduler()
143 const tx = createTransaction({
144 id: `tx1`,
145 createdAt: new Date(now.getTime()),
146 nextAttemptAt: now.getTime(),
147 })
148​
149 scheduler.schedule(tx)
150 scheduler.markStarted(tx)
151​
152 expect(scheduler.getNext()).toBeUndefined()
153 })
154​
155 it(`processes second transaction after first retries and succeeds`, () => {
156 vi.useFakeTimers()
157​
158 const now = new Date(`2026-01-01T00:00:00.000Z`)
159 vi.setSystemTime(now)
160​
161 const scheduler = new KeyScheduler()
162 const first = createTransaction({
163 id: `first`,
164 createdAt: new Date(now.getTime()),
165 nextAttemptAt: now.getTime(),
166 })
167 const second = createTransaction({
168 id: `second`,
169 createdAt: new Date(now.getTime() + 1),
170 nextAttemptAt: now.getTime(),
171 })
172​
173 scheduler.schedule(first)
174 scheduler.schedule(second)
175​
176 scheduler.markStarted(first)
177 scheduler.markFailed(first)
178 scheduler.updateTransaction({
179 ...first,
180 retryCount: 1,
181 nextAttemptAt: now.getTime() + 5000,
182 })
183​
184 expect(scheduler.getNext()).toBeUndefined()
185​
186 vi.advanceTimersByTime(5000)
187​
188 const retryTx = scheduler.getNext()
189 expect(retryTx?.id).toBe(`first`)
190​
191 scheduler.markStarted(retryTx!)
192 scheduler.markCompleted(retryTx!)
193​
194 const finalTx = scheduler.getNext()
195 expect(finalTx?.id).toBe(`second`)
196 })
197​
198 it(`maintains FIFO order regardless of scheduling order`, () => {
199 vi.useFakeTimers()
200​
201 const now = new Date(`2026-01-01T00:00:00.000Z`)
202 vi.setSystemTime(now)
203​
204 const scheduler = new KeyScheduler()
205 const older = createTransaction({
206 id: `older`,
207 createdAt: new Date(now.getTime()),
208 nextAttemptAt: now.getTime(),
209 })
210 const newer = createTransaction({
211 id: `newer`,
212 createdAt: new Date(now.getTime() + 1),
213 nextAttemptAt: now.getTime(),
214 })
215​
216 scheduler.schedule(newer)
217 scheduler.schedule(older)
218​
219 const next = scheduler.getNext()
220 expect(next?.id).toBe(`older`)
221 })
222​
223 it(`preserves FIFO order after updateTransaction`, () => {
224 vi.useFakeTimers()
225​
226 const now = new Date(`2026-01-01T00:00:00.000Z`)
227 vi.setSystemTime(now)
228​
229 const scheduler = new KeyScheduler()
230 const first = createTransaction({
231 id: `first`,
232 createdAt: new Date(now.getTime()),
233 nextAttemptAt: now.getTime(),
234 })
235 const second = createTransaction({
236 id: `second`,
237 createdAt: new Date(now.getTime() + 1),
238 nextAttemptAt: now.getTime(),
239 })
240​
241 scheduler.schedule(first)
242 scheduler.schedule(second)
243​
244 scheduler.updateTransaction({
245 ...first,
246 retryCount: 1,
247 nextAttemptAt: now.getTime() + 5000,
248 })
249​
250 vi.advanceTimersByTime(5000)
251​
252 const next = scheduler.getNext()
253 expect(next?.id).toBe(`first`)
254 })
255​
256 it(`returns undefined when no transactions are scheduled`, () => {
257 const scheduler = new KeyScheduler()
258 expect(scheduler.getNext()).toBeUndefined()
259 })
260​
261 it(`processes transactions with identical createdAt in scheduling order`, () => {
262 vi.useFakeTimers()
263​
264 const now = new Date(`2026-01-01T00:00:00.000Z`)
265 vi.setSystemTime(now)
266​
267 const scheduler = new KeyScheduler()
268 const first = createTransaction({
269 id: `first`,
270 createdAt: new Date(now.getTime()),
271 nextAttemptAt: now.getTime(),
272 })
273 const second = createTransaction({
274 id: `second`,
275 createdAt: new Date(now.getTime()),
276 nextAttemptAt: now.getTime(),
277 })
278​
279 scheduler.schedule(first)
280 scheduler.schedule(second)
281​
282 const tx1 = scheduler.getNext()
283 expect(tx1?.id).toBe(`first`)
284​
285 scheduler.markStarted(first)
286 scheduler.markCompleted(first)
287​
288 const tx2 = scheduler.getNext()
289 expect(tx2?.id).toBe(`second`)
290 })
291})