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

deduplication.suite.ts

397 lines12.7 KB

1/**
2 * Deduplication Test Suite
3 *
4 * Tests public results of concurrent consumers. These shared cases do not
5 * count provider calls or establish that a request is held in flight.
6 * The Query-specific held-demand boundary supplies that separate witness.
7 */
8​
9import { describe, expect, it } from 'vitest'
10import { createLiveQueryCollection, eq, gt, isNull, lt } from '@tanstack/db'
11import { waitForQueryData } from '../utils/helpers'
12import { withQueryCleanup } from './mutations.suite'
13import type { E2ETestConfig } from '../types'
14​
15export function createDeduplicationTestSuite(
16 getConfig: () => Promise<E2ETestConfig>,
17) {
18 describe(`Deduplication Suite`, () => {
19 describe(`Identical Predicates`, () => {
20 it(`should return equal nonempty counts for identical concurrent queries`, async () => {
21 await withQueryCleanup(async (own) => {
22 const config = await getConfig()
23 const usersCollection = config.collections.onDemand.users
24​
25 // Create two identical queries
26 const query1 = own(
27 createLiveQueryCollection((q) =>
28 q
29 .from({ user: usersCollection })
30 .where(({ user }) => eq(user.age, 25)),
31 ),
32 )
33​
34 const query2 = own(
35 createLiveQueryCollection((q) =>
36 q
37 .from({ user: usersCollection })
38 .where(({ user }) => eq(user.age, 25)),
39 ),
40 )
41​
42 // Execute concurrently
43 await Promise.all([query1.preload(), query2.preload()])
44 await Promise.all([
45 waitForQueryData(query1, { minSize: 1 }),
46 waitForQueryData(query2, { minSize: 1 }),
47 ])
48​
49 // Both public result counts should agree.
50 expect(query1.size).toBe(query2.size)
51 expect(query1.size).toBeGreaterThan(0)
52 })
53 })
54​
55 it(`should return equal counts for five identical queries`, async () => {
56 await withQueryCleanup(async (own) => {
57 const config = await getConfig()
58 const usersCollection = config.collections.onDemand.users
59​
60 // Create 5 identical queries
61 const queries = Array.from({ length: 5 }, () =>
62 own(
63 createLiveQueryCollection((q) =>
64 q
65 .from({ user: usersCollection })
66 .where(({ user }) => gt(user.age, 30)),
67 ),
68 ),
69 )
70​
71 // Execute all concurrently
72 await Promise.all(queries.map((q) => q.preload()))
73​
74 // All public result counts should agree.
75 const firstSize = queries[0]?.size
76 expect(firstSize).toBeDefined()
77 queries.forEach((q) => {
78 expect(q.size).toBe(firstSize!)
79 })
80 })
81 })
82 })
83​
84 describe(`Overlapping Predicates`, () => {
85 it(`should handle subset predicates correctly`, async () => {
86 await withQueryCleanup(async (own) => {
87 const config = await getConfig()
88 const usersCollection = config.collections.onDemand.users
89​
90 // Query 1: age > 25 (superset)
91 const query1 = own(
92 createLiveQueryCollection((q) =>
93 q
94 .from({ user: usersCollection })
95 .where(({ user }) => gt(user.age, 25)),
96 ),
97 )
98​
99 // Query 2: age > 30 (subset of Query 1)
100 const query2 = own(
101 createLiveQueryCollection((q) =>
102 q
103 .from({ user: usersCollection })
104 .where(({ user }) => gt(user.age, 30)),
105 ),
106 )
107​
108 // Execute concurrently
109 await Promise.all([query1.preload(), query2.preload()])
110​
111 // Query 2 results should be subset of Query 1
112 expect(query2.size).toBeLessThanOrEqual(query1.size)
113 })
114 })
115​
116 it(`should handle non-overlapping predicates correctly`, async () => {
117 await withQueryCleanup(async (own) => {
118 const config = await getConfig()
119 const usersCollection = config.collections.onDemand.users
120​
121 // Query 1: age > 50
122 const query1 = own(
123 createLiveQueryCollection((q) =>
124 q
125 .from({ user: usersCollection })
126 .where(({ user }) => gt(user.age, 50)),
127 ),
128 )
129​
130 // Query 2: age < 30
131 const query2 = own(
132 createLiveQueryCollection((q) =>
133 q
134 .from({ user: usersCollection })
135 .where(({ user }) => lt(user.age, 30)),
136 ),
137 )
138​
139 // Execute concurrently
140 await Promise.all([query1.preload(), query2.preload()])
141 await Promise.all([
142 waitForQueryData(query1, { minSize: 1 }),
143 waitForQueryData(query2, { minSize: 1 }),
144 ])
145​
146 // Both public results should be nonempty; provider work is not counted.
147 expect(query1.size).toBeGreaterThan(0)
148 expect(query2.size).toBeGreaterThan(0)
149 })
150 })
151 })
152​
153 describe(`Queries During Loading`, () => {
154 it(`should settle identical queries preloaded in the same turn`, async () => {
155 await withQueryCleanup(async (own) => {
156 const config = await getConfig()
157 const usersCollection = config.collections.onDemand.users
158​
159 // Start first query
160 const query1 = own(
161 createLiveQueryCollection((q) =>
162 q
163 .from({ user: usersCollection })
164 .where(({ user }) => eq(user.isActive, true)),
165 ),
166 )
167​
168 // Immediately start identical second query
169 const query2 = own(
170 createLiveQueryCollection((q) =>
171 q
172 .from({ user: usersCollection })
173 .where(({ user }) => eq(user.isActive, true)),
174 ),
175 )
176​
177 // Both should complete successfully
178 await Promise.all([query1.preload(), query2.preload()])
179 })
180 })
181​
182 it(`should preserve count inclusion for different predicates preloaded together`, async () => {
183 await withQueryCleanup(async (own) => {
184 const config = await getConfig()
185 const usersCollection = config.collections.onDemand.users
186​
187 const query1 = own(
188 createLiveQueryCollection((q) =>
189 q
190 .from({ user: usersCollection })
191 .where(({ user }) => gt(user.age, 25)),
192 ),
193 )
194​
195 const query2 = own(
196 createLiveQueryCollection((q) =>
197 q
198 .from({ user: usersCollection })
199 .where(({ user }) => gt(user.age, 50)),
200 ),
201 )
202​
203 await Promise.all([query1.preload(), query2.preload()])
204​
205 // Both execute correctly
206 expect(query1.size).toBeGreaterThanOrEqual(query2.size)
207 })
208 })
209 })
210​
211 describe(`Deduplication with Limit/Offset`, () => {
212 it(`should handle queries with same limit/offset`, async () => {
213 await withQueryCleanup(async (own) => {
214 const config = await getConfig()
215 const usersCollection = config.collections.onDemand.users
216​
217 const query1 = own(
218 createLiveQueryCollection((q) =>
219 q
220 .from({ user: usersCollection })
221 .orderBy(({ user }) => user.id, `asc`)
222 .limit(10)
223 .offset(0),
224 ),
225 )
226​
227 const query2 = own(
228 createLiveQueryCollection((q) =>
229 q
230 .from({ user: usersCollection })
231 .orderBy(({ user }) => user.id, `asc`)
232 .limit(10)
233 .offset(0),
234 ),
235 )
236​
237 await Promise.all([query1.preload(), query2.preload()])
238 await waitForQueryData(query1, { minSize: 10 })
239 await waitForQueryData(query2, { minSize: 10 })
240​
241 expect(query1.size).toBe(query2.size)
242 expect(query1.size).toBe(10)
243 })
244 })
245​
246 it(`should handle queries with different limits`, async () => {
247 await withQueryCleanup(async (own) => {
248 const config = await getConfig()
249 const usersCollection = config.collections.onDemand.users
250​
251 const query1 = own(
252 createLiveQueryCollection((q) =>
253 q
254 .from({ user: usersCollection })
255 .orderBy(({ user }) => user.id, `asc`)
256 .limit(10),
257 ),
258 )
259​
260 const query2 = own(
261 createLiveQueryCollection((q) =>
262 q
263 .from({ user: usersCollection })
264 .orderBy(({ user }) => user.id, `asc`)
265 .limit(20),
266 ),
267 )
268​
269 await Promise.all([query1.preload(), query2.preload()])
270 await waitForQueryData(query1, { minSize: 10 })
271 await waitForQueryData(query2, { minSize: 20 })
272​
273 expect(query1.size).toBe(10)
274 expect(query2.size).toBe(20)
275 })
276 })
277​
278 it(`should handle queries with different offsets`, async () => {
279 await withQueryCleanup(async (own) => {
280 const config = await getConfig()
281 const usersCollection = config.collections.onDemand.users
282​
283 const query1 = own(
284 createLiveQueryCollection((q) =>
285 q
286 .from({ user: usersCollection })
287 .orderBy(({ user }) => user.id, `asc`)
288 .limit(10)
289 .offset(0),
290 ),
291 )
292​
293 const query2 = own(
294 createLiveQueryCollection((q) =>
295 q
296 .from({ user: usersCollection })
297 .orderBy(({ user }) => user.id, `asc`)
298 .limit(10)
299 .offset(10),
300 ),
301 )
302​
303 await Promise.all([query1.preload(), query2.preload()])
304 await waitForQueryData(query1, { minSize: 10 })
305 await waitForQueryData(query2, { minSize: 10 })
306​
307 expect(query1.size).toBe(10)
308 expect(query2.size).toBe(10)
309​
310 // Results should be different
311 const ids1 = Array.from(query1.state.values()).map((u) => u.id)
312 const ids2 = Array.from(query2.state.values()).map((u) => u.id)
313 expect(ids1).not.toEqual(ids2)
314 })
315 })
316 })
317​
318 describe(`Race Conditions`, () => {
319 it(`should handle rapid concurrent query bursts`, async () => {
320 await withQueryCleanup(async (own) => {
321 const config = await getConfig()
322 const usersCollection = config.collections.onDemand.users
323​
324 // Create 20 identical queries
325 const queries = Array.from({ length: 20 }, () =>
326 own(
327 createLiveQueryCollection((q) =>
328 q
329 .from({ user: usersCollection })
330 .where(({ user }) => eq(user.isActive, true)),
331 ),
332 ),
333 )
334​
335 // Execute all simultaneously
336 await Promise.all(queries.map((q) => q.preload()))
337​
338 // All public result counts should agree.
339 const firstSize = queries[0]?.size
340 expect(firstSize).toBeDefined()
341 queries.forEach((q) => {
342 expect(q.size).toBe(firstSize!)
343 })
344 })
345 })
346​
347 it(`should return nonempty results for four concurrent predicates`, async () => {
348 await withQueryCleanup(async (own) => {
349 const config = await getConfig()
350 const usersCollection = config.collections.onDemand.users
351​
352 // Execute many concurrent queries with different predicates
353 const queries = [
354 own(
355 createLiveQueryCollection((q) =>
356 q
357 .from({ user: usersCollection })
358 .where(({ user }) => eq(user.age, 25)),
359 ),
360 ),
361 own(
362 createLiveQueryCollection((q) =>
363 q
364 .from({ user: usersCollection })
365 .where(({ user }) => gt(user.age, 50)),
366 ),
367 ),
368 own(
369 createLiveQueryCollection((q) =>
370 q
371 .from({ user: usersCollection })
372 .where(({ user }) => eq(user.isActive, true)),
373 ),
374 ),
375 own(
376 createLiveQueryCollection((q) =>
377 q
378 .from({ user: usersCollection })
379 .where(({ user }) => isNull(user.email)),
380 ),
381 ),
382 ]
383​
384 await Promise.all(queries.map((q) => q.preload()))
385 await Promise.all(
386 queries.map((q) => waitForQueryData(q, { minSize: 1 })),
387 )
388​
389 // Each query should have a nonempty public result.
390 queries.forEach((q) => {
391 expect(q.size).toBeGreaterThan(0)
392 })
393 })
394 })
395 })
396 })