Skip to content

Allow failing of individual transactions #3052

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 3 commits into from
May 13, 2020
Merged
Changes from 1 commit
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
43 changes: 22 additions & 21 deletions packages/firestore/test/unit/specs/spec_test_components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,7 @@ export class MockMemoryPersistence extends MemoryPersistence {
transaction: PersistenceTransaction
) => PersistencePromise<T>
): Promise<T> {
if (this.injectFailures) {
if (this.injectFailures[action as PersistenceAction] === undefined) {
throw fail('Failure mode not specified for action: ' + action);
}
if (this.injectFailures[action as PersistenceAction]) {
return Promise.reject(
new IndexedDbTransactionError(new Error('Simulated retryable error'))
);
}
}

failTransactionIfNeeded(this.injectFailures, action);
return super.runTransaction(action, mode, transactionOperation);
}
}
Expand All @@ -95,20 +85,31 @@ export class MockIndexedDbPersistence extends IndexedDbPersistence {
transaction: PersistenceTransaction
) => PersistencePromise<T>
): Promise<T> {
if (this.injectFailures) {
if (this.injectFailures[action as PersistenceAction] === undefined) {
throw fail('Failure mode not specified for action: ' + action);
} else if (this.injectFailures[action as PersistenceAction]) {
return Promise.reject(
new IndexedDbTransactionError(new Error('Simulated retryable error'))
);
}
}

failTransactionIfNeeded(this.injectFailures, action);
return super.runTransaction(action, mode, transactionOperation);
}
}

/**
* Shared failure handler between MockIndexedDbPersistence and
* MockMemoryPersistence that can inject transaction failures.
*/
function failTransactionIfNeeded(
config: SpecDatabaseFailures | undefined,
action: string
): void {
if (config) {
const shouldFail = config[action as PersistenceAction];
if (shouldFail === undefined) {
throw fail('Failure mode not specified for action: ' + action);
} else if (shouldFail) {
throw new IndexedDbTransactionError(
new Error('Simulated retryable error' + action)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add a space or colon after "error" so that the action text is not mashed into the word "error".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a nit but a crucial typo fix :) Done

);
}
}
}

export class MockIndexedDbComponentProvider extends IndexedDbComponentProvider {
persistence!: MockIndexedDbPersistence;

Expand Down