Skip to content

Commit 7f009e2

Browse files
committed
query.test.ts: use LRUGC in bloom filter tests so they can run even when persistence=false
1 parent 114bc6e commit 7f009e2

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

packages/firestore/test/integration/api/query.test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
enableNetwork,
3737
endAt,
3838
endBefore,
39+
memoryLocalCache,
40+
memoryLruGarbageCollector,
3941
Firestore,
4042
GeoPoint,
4143
getDocs,
@@ -46,6 +48,7 @@ import {
4648
onSnapshot,
4749
or,
4850
orderBy,
51+
persistentLocalCache,
4952
Query,
5053
query,
5154
QuerySnapshot,
@@ -2128,16 +2131,6 @@ apiDescribe('Queries', (persistence: boolean) => {
21282131
);
21292132
}
21302133

2131-
// Skip the verification of the existence filter mismatch when persistence
2132-
// is disabled because without persistence there is no resume token
2133-
// specified in the subsequent call to getDocs(), and, therefore, Watch
2134-
// will _not_ send an existence filter.
2135-
// TODO(b/272754156) Re-write this test using a snapshot listener instead
2136-
// of calls to getDocs() and remove this check for disabled persistence.
2137-
if (!persistence) {
2138-
return 'passed';
2139-
}
2140-
21412134
// Skip the verification of the existence filter mismatch when testing
21422135
// against the Firestore emulator because the Firestore emulator fails to
21432136
// to send an existence filter at all.
@@ -2193,12 +2186,18 @@ apiDescribe('Queries', (persistence: boolean) => {
21932186
return 'passed';
21942187
};
21952188

2189+
// Use LRU memory cache so that the resume token will not be deleted between
2190+
// calls to `getDocs()`.
2191+
const localCache = persistence
2192+
? persistentLocalCache()
2193+
: memoryLocalCache({ garbageCollector: memoryLruGarbageCollector() });
2194+
21962195
// Run the test
21972196
let attemptNumber = 0;
21982197
while (true) {
21992198
attemptNumber++;
22002199
const iterationResult = await withTestCollection(
2201-
persistence,
2200+
localCache,
22022201
testDocs,
22032202
runTestIteration
22042203
);

packages/firestore/test/integration/util/helpers.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import {
2525
DocumentData,
2626
DocumentReference,
2727
Firestore,
28+
MemoryLocalCache,
2829
memoryLocalCache,
2930
memoryLruGarbageCollector,
3031
newTestApp,
3132
newTestFirestore,
33+
PersistentLocalCache,
3234
persistentLocalCache,
3335
PrivateSettings,
3436
QuerySnapshot,
@@ -137,7 +139,7 @@ export function toIds(docSet: QuerySnapshot): string[] {
137139
}
138140

139141
export function withTestDb(
140-
persistence: boolean,
142+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
141143
fn: (db: Firestore) => Promise<void>
142144
): Promise<void> {
143145
return withTestDbs(persistence, 1, ([db]) => {
@@ -160,7 +162,7 @@ export function withEnsuredEagerGcTestDb(
160162
}
161163

162164
export function withEnsuredLruGcTestDb(
163-
persistence: boolean,
165+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
164166
fn: (db: Firestore) => Promise<void>
165167
): Promise<void> {
166168
const newSettings = { ...DEFAULT_SETTINGS };
@@ -188,7 +190,7 @@ export function withEnsuredLruGcTestDb(
188190

189191
/** Runs provided fn with a db for an alternate project id. */
190192
export function withAlternateTestDb(
191-
persistence: boolean,
193+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
192194
fn: (db: Firestore) => Promise<void>
193195
): Promise<void> {
194196
return withTestDbsSettings(
@@ -203,7 +205,7 @@ export function withAlternateTestDb(
203205
}
204206

205207
export function withTestDbs(
206-
persistence: boolean,
208+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
207209
numDbs: number,
208210
fn: (db: Firestore[]) => Promise<void>
209211
): Promise<void> {
@@ -216,7 +218,7 @@ export function withTestDbs(
216218
);
217219
}
218220
export async function withTestDbsSettings<T>(
219-
persistence: boolean,
221+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
220222
projectId: string,
221223
settings: PrivateSettings,
222224
numDbs: number,
@@ -250,7 +252,7 @@ export async function withTestDbsSettings<T>(
250252
}
251253

252254
export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
253-
persistence: boolean,
255+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
254256
dbNames: string[],
255257
fn: (db: Firestore[]) => Promise<void>
256258
): Promise<void> {
@@ -265,8 +267,12 @@ export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
265267
const dbs: Firestore[] = [];
266268
for (const dbName of dbNames) {
267269
const newSettings = { ...DEFAULT_SETTINGS };
268-
if (persistence) {
269-
newSettings.localCache = persistentLocalCache();
270+
if (typeof persistence === 'boolean') {
271+
if (persistence) {
272+
newSettings.localCache = persistentLocalCache();
273+
}
274+
} else {
275+
newSettings.localCache = persistence;
270276
}
271277
const db = newTestFirestore(app, newSettings, dbName);
272278
dbs.push(db);
@@ -285,7 +291,7 @@ export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
285291
}
286292

287293
export function withTestDoc(
288-
persistence: boolean,
294+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
289295
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
290296
): Promise<void> {
291297
return withTestDb(persistence, db => {
@@ -294,7 +300,7 @@ export function withTestDoc(
294300
}
295301

296302
export function withTestDocAndSettings(
297-
persistence: boolean,
303+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
298304
settings: PrivateSettings,
299305
fn: (doc: DocumentReference) => Promise<void>
300306
): Promise<void> {
@@ -315,7 +321,7 @@ export function withTestDocAndSettings(
315321
// `withTestDoc(..., docRef => { setDoc(docRef, initialData) ...});` that
316322
// otherwise is quite common.
317323
export function withTestDocAndInitialData(
318-
persistence: boolean,
324+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
319325
initialData: DocumentData | null,
320326
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
321327
): Promise<void> {
@@ -330,15 +336,15 @@ export function withTestDocAndInitialData(
330336
}
331337

332338
export function withTestCollection<T>(
333-
persistence: boolean,
339+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
334340
docs: { [key: string]: DocumentData },
335341
fn: (collection: CollectionReference, db: Firestore) => Promise<T>
336342
): Promise<T> {
337343
return withTestCollectionSettings(persistence, DEFAULT_SETTINGS, docs, fn);
338344
}
339345

340346
export function withEmptyTestCollection(
341-
persistence: boolean,
347+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
342348
fn: (collection: CollectionReference, db: Firestore) => Promise<void>
343349
): Promise<void> {
344350
return withTestCollection(persistence, {}, fn);
@@ -347,7 +353,7 @@ export function withEmptyTestCollection(
347353
// TODO(mikelehen): Once we wipe the database between tests, we can probably
348354
// return the same collection every time.
349355
export function withTestCollectionSettings<T>(
350-
persistence: boolean,
356+
persistence: boolean | PersistentLocalCache | MemoryLocalCache,
351357
settings: PrivateSettings,
352358
docs: { [key: string]: DocumentData },
353359
fn: (collection: CollectionReference, db: Firestore) => Promise<T>

0 commit comments

Comments
 (0)