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

deduplication.suite.ts

350 lines11.1 KB

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