diff --git a/packages/firestore/src/core/transaction_runner.ts b/packages/firestore/src/core/transaction_runner.ts index d586abeeedf..a9e17327aa7 100644 --- a/packages/firestore/src/core/transaction_runner.ts +++ b/packages/firestore/src/core/transaction_runner.ts @@ -25,14 +25,14 @@ import { isNullOrUndefined } from '../util/types'; import { Transaction } from './transaction'; -const RETRY_COUNT = 5; +export const DEFAULT_MAX_ATTEMPTS_COUNT = 5; /** * TransactionRunner encapsulates the logic needed to run and retry transactions * with backoff. */ export class TransactionRunner { - private retries = RETRY_COUNT; + private attemptsRemaining = DEFAULT_MAX_ATTEMPTS_COUNT; private backoff: ExponentialBackoff; constructor( @@ -49,6 +49,7 @@ export class TransactionRunner { /** Runs the transaction and sets the result on deferred. */ run(): void { + this.attemptsRemaining -= 1; this.runWithBackOff(); } @@ -99,8 +100,8 @@ export class TransactionRunner { } private handleTransactionError(error: Error): void { - if (this.retries > 0 && this.isRetryableTransactionError(error)) { - this.retries -= 1; + if (this.attemptsRemaining > 0 && this.isRetryableTransactionError(error)) { + this.attemptsRemaining -= 1; this.asyncQueue.enqueueAndForget(() => { this.runWithBackOff(); return Promise.resolve(); diff --git a/packages/firestore/test/integration/api_internal/transaction.test.ts b/packages/firestore/test/integration/api_internal/transaction.test.ts index 41a0f53b213..1b636adbd98 100644 --- a/packages/firestore/test/integration/api_internal/transaction.test.ts +++ b/packages/firestore/test/integration/api_internal/transaction.test.ts @@ -18,6 +18,7 @@ import * as firestore from '@firebase/firestore-types'; import { expect } from 'chai'; +import { DEFAULT_MAX_ATTEMPTS_COUNT } from '../../../src/core/transaction_runner'; import { TimerId } from '../../../src/util/async_queue'; import { Deferred } from '../../util/promise'; import * as integrationHelpers from '../util/helpers'; @@ -183,6 +184,7 @@ apiDescribe( .then(() => doc.get()) .then(snapshot => { expect(snapshot.data()!['count']).to.equal(1234 + counter); + expect(counter).to.equal(DEFAULT_MAX_ATTEMPTS_COUNT); }); }); }); diff --git a/packages/firestore/test/integration/util/internal_helpers.ts b/packages/firestore/test/integration/util/internal_helpers.ts index 4ac848d02b5..fb2dd862bf9 100644 --- a/packages/firestore/test/integration/util/internal_helpers.ts +++ b/packages/firestore/test/integration/util/internal_helpers.ts @@ -49,7 +49,8 @@ export function getDefaultDatabaseInfo(): DatabaseInfo { DEFAULT_SETTINGS.host!, !!DEFAULT_SETTINGS.ssl, !!DEFAULT_SETTINGS.experimentalForceLongPolling, - !!DEFAULT_SETTINGS.experimentalAutoDetectLongPolling + !!DEFAULT_SETTINGS.experimentalAutoDetectLongPolling, + /*use FetchStreams= */ false ); }