Skip to content

IndexedDB recovery for handleUserChange #3087

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
May 19, 2020
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
2 changes: 1 addition & 1 deletion packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class FirestoreClient {
persistenceResult
).then(initializationDone.resolve, initializationDone.reject);
} else {
this.asyncQueue.enqueueAndForget(() => {
this.asyncQueue.enqueueRetryable(() => {
return this.handleCredentialChange(user);
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,15 @@ export class SyncEngine implements RemoteSyncer {

async handleCredentialChange(user: User): Promise<void> {
const userChanged = !this.currentUser.isEqual(user);
this.currentUser = user;

if (userChanged) {
const result = await this.localStore.handleUserChange(user);
this.currentUser = user;

// Fails tasks waiting for pending writes requested by previous user.
this.rejectOutstandingPendingWritesCallbacks(
"'waitForPendingWrites' promise is rejected due to a user change."
);

const result = await this.localStore.handleUserChange(user);
// TODO(b/114226417): Consider calling this only in the primary tab.
this.sharedClientState.handleUserChange(
user,
Expand Down
37 changes: 37 additions & 0 deletions packages/firestore/test/unit/specs/recovery_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,41 @@ describeSpec('Persistence Recovery', ['no-ios', 'no-android'], () => {
removed: [doc1]
});
});

specTest(
'User change handles transaction failures (with recovery)',
['durable-persistence'],
() => {
const query = Query.atPath(path('collection'));
const doc1 = doc(
'collection/key1',
0,
{ foo: 'a' },
{ hasLocalMutations: true }
);
return spec()
.changeUser('user1')
.userSets('collection/key1', { foo: 'a' })
.userListens(query)
.expectEvents(query, {
added: [doc1],
fromCache: true,
hasPendingWrites: true
})
.failDatabaseTransactions({ 'Handle user change': true })
.changeUser('user2')
.recoverDatabase()
.runTimer(TimerId.AsyncQueueRetry)
.expectEvents(query, { removed: [doc1], fromCache: true })
.failDatabaseTransactions({ 'Handle user change': true })
.changeUser('user1')
.recoverDatabase()
.runTimer(TimerId.AsyncQueueRetry)
.expectEvents(query, {
added: [doc1],
fromCache: true,
hasPendingWrites: true
});
}
);
});
16 changes: 12 additions & 4 deletions packages/firestore/test/unit/specs/spec_test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,19 @@ abstract class TestRunner {
return Promise.resolve();
}

private doChangeUser(user: string | null): Promise<void> {
private async doChangeUser(user: string | null): Promise<void> {
this.user = new User(user);
return this.queue.enqueue(() =>
this.syncEngine.handleCredentialChange(this.user)
);
const deferred = new Deferred<void>();
await this.queue.enqueueRetryable(async () => {
try {
await this.syncEngine.handleCredentialChange(this.user);
} finally {
// Resolve the deferred Promise even if the operation failed. This allows
// the spec tests to manually retry the failed user change.
deferred.resolve();
}
});
return deferred.promise;
}

private async doFailDatabase(
Expand Down