Skip to content

Firestore: query.test.ts: add a test that resumes a query with existence filter #7134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
Query,
query,
QuerySnapshot,
runTransaction,
setDoc,
startAfter,
startAt,
Expand Down Expand Up @@ -2059,6 +2060,62 @@ apiDescribe('Queries', (persistence: boolean) => {
});
});
});

it('resuming a query should use existence filter to detect deletes', async () => {
// Prepare the names and contents of the 100 documents to create.
const testDocs: { [key: string]: object } = {};
for (let i = 0; i < 100; i++) {
testDocs['doc' + (1000 + i)] = { key: 42 };
}

return withTestCollection(persistence, testDocs, async (coll, db) => {
// Run a query to populate the local cache with the 100 documents and a
// resume token.
const snapshot1 = await getDocs(coll);
expect(snapshot1.size, 'snapshot1.size').to.equal(100);
const createdDocuments = snapshot1.docs.map(snapshot => snapshot.ref);

// Delete 50 of the 100 documents. Do this in a transaction, rather than
// deleteDoc(), to avoid affecting the local cache.
const deletedDocumentIds = new Set<string>();
await runTransaction(db, async txn => {
for (let i = 0; i < createdDocuments.length; i += 2) {
const documentToDelete = createdDocuments[i];
txn.delete(documentToDelete);
deletedDocumentIds.add(documentToDelete.id);
}
});

// Wait for 10 seconds, during which Watch will stop tracking the query
// and will send an existence filter rather than "delete" events when the
// query is resumed.
await new Promise(resolve => setTimeout(resolve, 10000));

// Resume the query and save the resulting snapshot for verification.
const snapshot2 = await getDocs(coll);

// Verify that the snapshot from the resumed query contains the expected
// documents; that is, that it contains the 50 documents that were _not_
// deleted.
// TODO(b/270731363): Remove the "if" condition below once the
// Firestore Emulator is fixed to send an existence filter. At the time of
// writing, the Firestore emulator fails to send an existence filter,
// resulting in the client including the deleted documents in the snapshot
// of the resumed query.
if (!(USE_EMULATOR && snapshot2.size === 100)) {
const actualDocumentIds = snapshot2.docs
.map(documentSnapshot => documentSnapshot.ref.id)
.sort();
const expectedDocumentIds = createdDocuments
.filter(documentRef => !deletedDocumentIds.has(documentRef.id))
.map(documentRef => documentRef.id)
.sort();
expect(actualDocumentIds, 'snapshot2.docs').to.deep.equal(
expectedDocumentIds
);
}
});
}).timeout('90s');
});

function verifyDocumentChange<T>(
Expand Down
37 changes: 31 additions & 6 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import {
PrivateSettings,
SnapshotListenOptions,
newTestFirestore,
newTestApp
newTestApp,
writeBatch,
WriteBatch
} from './firebase_export';
import {
ALT_PROJECT_ID,
Expand Down Expand Up @@ -317,11 +319,34 @@ export function withTestCollectionSettings(
const collectionId = 'test-collection-' + doc(collection(testDb, 'x')).id;
const testCollection = collection(testDb, collectionId);
const setupCollection = collection(setupDb, collectionId);
const sets: Array<Promise<void>> = [];
Object.keys(docs).forEach(key => {
sets.push(setDoc(doc(setupCollection, key), docs[key]));
});
return Promise.all(sets).then(() => fn(testCollection, testDb));

const writeBatchCommits: Array<Promise<void>> = [];
let writeBatch_: WriteBatch | null = null;
let writeBatchSize = 0;

for (const key of Object.keys(docs)) {
if (writeBatch_ === null) {
writeBatch_ = writeBatch(setupDb);
}

writeBatch_.set(doc(setupCollection, key), docs[key]);
writeBatchSize++;

// Write batches are capped at 500 writes. Use 400 just to be safe.
if (writeBatchSize === 400) {
writeBatchCommits.push(writeBatch_.commit());
writeBatch_ = null;
writeBatchSize = 0;
}
}

if (writeBatch_ !== null) {
writeBatchCommits.push(writeBatch_.commit());
}

return Promise.all(writeBatchCommits).then(() =>
fn(testCollection, testDb)
);
}
);
}