Skip to content

Fix b/72502745: OnlineState changes cause limbo document crash. #470

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 1 commit into from
Jan 26, 2018
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
27 changes: 15 additions & 12 deletions packages/firestore/src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ export class View {
);
});

const limboChanges = this.applyTargetChange(targetChange);
this.applyTargetChange(targetChange);
const limboChanges = this.updateLimboDocuments();
const synced = this.limboDocuments.size === 0 && this.current;
const newSyncState = synced ? SyncState.Synced : SyncState.Local;
const syncStateChanged = newSyncState !== this.syncState;
Expand Down Expand Up @@ -320,9 +321,7 @@ export class View {
* Updates syncedDocuments, current, and limbo docs based on the given change.
* Returns the list of changes to which docs are in limbo.
*/
private applyTargetChange(
targetChange?: TargetChange
): LimboDocumentChange[] {
private applyTargetChange(targetChange?: TargetChange): void {
if (targetChange) {
const targetMapping = targetChange.mapping;
if (targetMapping instanceof ResetMapping) {
Expand All @@ -348,19 +347,23 @@ export class View {
);
}
}
}

private updateLimboDocuments(): LimboDocumentChange[] {
// We can only determine limbo documents when we're in-sync with the server.
if (!this.current) {
return [];
}

// Recompute the set of limbo docs.
// TODO(klimt): Do this incrementally so that it's not quadratic when
// updating many documents.
const oldLimboDocuments = this.limboDocuments;
this.limboDocuments = documentKeySet();
if (this.current) {
this.documentSet.forEach(doc => {
if (this.shouldBeInLimbo(doc.key)) {
this.limboDocuments = this.limboDocuments.add(doc.key);
}
});
}
this.documentSet.forEach(doc => {
if (this.shouldBeInLimbo(doc.key)) {
this.limboDocuments = this.limboDocuments.add(doc.key);
}
});

// Diff the new limbo docs with the old limbo docs.
const changes: LimboDocumentChange[] = [];
Expand Down
31 changes: 31 additions & 0 deletions packages/firestore/test/unit/specs/offline_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,35 @@ describeSpec('Offline:', [], () => {
.expectEvents(query, { fromCache: false })
);
});

specTest('Queries with limbo documents handle going offline.', [], () => {
const query = Query.atPath(path('collection'));
const docA = doc('collection/a', 1000, { key: 'a' });
const docB = doc('collection/b', 1005, { key: 'b' });
const limboQuery = Query.atPath(docA.key.path);
return (
spec()
.userListens(query)
.watchAcksFull(query, 1000, docA)
.expectEvents(query, { added: [docA] })
.watchResets(query)
// No more documents
.watchCurrents(query, 'resume-token-1001')
.watchSnapshots(1001)
// docA will now be in limbo (triggering fromCache=true)
.expectLimboDocs(docA.key)
.expectEvents(query, { fromCache: true })
// first error triggers unknown state
.watchStreamCloses(Code.UNAVAILABLE)
.restoreListen(query, 'resume-token-1001')
// getting two more errors triggers offline state.
.watchStreamCloses(Code.UNAVAILABLE)
.watchStreamCloses(Code.UNAVAILABLE)
.watchAcksFull(query, 1001)
.watchAcksFull(limboQuery, 1001)
// Limbo document is resolved. No longer from cache.
.expectEvents(query, { removed: [docA], fromCache: false })
.expectLimboDocs()
);
});
});