Skip to content

Commit aaa6943

Browse files
author
Brian Chen
committed
update to override backoff
1 parent 500c9e1 commit aaa6943

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

packages/firestore/src/api/database.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
307307
// are already set to synchronize on the async queue.
308308
private _firestoreClient: FirestoreClient | undefined;
309309

310+
// For tests only: Whether to skip all backoffs scheduled by transactions.
311+
private skipTransactionBackoffs = false;
312+
310313
// Public for use in tests.
311314
// TODO(mikelehen): Use modularized initialization instead.
312315
readonly _queue = new AsyncQueue();
@@ -613,6 +616,14 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
613616
);
614617
}
615618

619+
/**
620+
* For tests: Skip all transaction backoffs to make integration tests
621+
* complete faster.
622+
*/
623+
removeTransactionBackoffs(): void {
624+
this.skipTransactionBackoffs = true;
625+
}
626+
616627
runTransaction<T>(
617628
updateFunction: (transaction: firestore.Transaction) => Promise<T>
618629
): Promise<T> {
@@ -621,7 +632,8 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
621632
return this.ensureClientConfigured().transaction(
622633
(transaction: InternalTransaction) => {
623634
return updateFunction(new Transaction(this, transaction));
624-
}
635+
},
636+
this.skipTransactionBackoffs
625637
);
626638
}
627639

packages/firestore/src/core/firestore_client.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,29 @@ export class FirestoreClient {
607607
}
608608

609609
transaction<T>(
610-
updateFunction: (transaction: Transaction) => Promise<T>
610+
updateFunction: (transaction: Transaction) => Promise<T>,
611+
skipTransactionBackoffs: boolean = false
611612
): Promise<T> {
612613
this.verifyNotShutdown();
613614
// We have to wait for the async queue to be sure syncEngine is initialized.
614615
return this.asyncQueue
615616
.enqueue(async () => {})
616617
.then(() => {
617-
const backoff = new ExponentialBackoff(
618-
this.asyncQueue,
619-
TimerId.RetryTransaction
620-
);
618+
let backoff;
619+
if (skipTransactionBackoffs) {
620+
backoff = new ExponentialBackoff(
621+
this.asyncQueue,
622+
TimerId.RetryTransaction,
623+
1,
624+
2,
625+
10
626+
);
627+
} else {
628+
backoff = new ExponentialBackoff(
629+
this.asyncQueue,
630+
TimerId.RetryTransaction
631+
);
632+
}
621633
return this.syncEngine.runTransaction(updateFunction, backoff);
622634
});
623635
}

packages/firestore/src/util/async_queue.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ export class AsyncQueue {
215215
return this._isShuttingDown;
216216
}
217217

218-
219218
/**
220219
* Adds a new operation to the queue without waiting for it to complete (i.e.
221220
* we ignore the Promise result).
@@ -331,7 +330,7 @@ export class AsyncQueue {
331330
timerId,
332331
delayMs,
333332
op,
334-
removedOp =>
333+
removedOp =>
335334
this.removeDelayedOperation(removedOp as DelayedOperation<unknown>)
336335
);
337336
this.delayedOperations.push(delayedOp as DelayedOperation<unknown>);

packages/firestore/test/integration/api/transactions.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import { expect } from 'chai';
2020
import { Deferred } from '../../util/promise';
2121
import firebase from '../util/firebase_export';
2222
import * as integrationHelpers from '../util/helpers';
23-
import { asyncQueue } from '../util/internal_helpers';
24-
import { TimerId } from '../../../src/util/async_queue';
23+
import { removeTransactionBackoffs } from '../util/internal_helpers';
2524

2625
const apiDescribe = integrationHelpers.apiDescribe;
2726
apiDescribe('Database transactions', (persistence: boolean) => {
@@ -461,7 +460,7 @@ apiDescribe('Database transactions', (persistence: boolean) => {
461460
let started = 0;
462461

463462
return integrationHelpers.withTestDb(persistence, db => {
464-
asyncQueue(db).skipDelaysForTimerId(TimerId.RetryTransaction);
463+
removeTransactionBackoffs(db);
465464
const doc = db.collection('counters').doc();
466465
return doc
467466
.set({
@@ -518,7 +517,7 @@ apiDescribe('Database transactions', (persistence: boolean) => {
518517
let counter = 0;
519518

520519
return integrationHelpers.withTestDb(persistence, db => {
521-
asyncQueue(db).skipDelaysForTimerId(TimerId.RetryTransaction);
520+
removeTransactionBackoffs(db);
522521
const doc = db.collection('counters').doc();
523522
return doc
524523
.set({
@@ -657,7 +656,7 @@ apiDescribe('Database transactions', (persistence: boolean) => {
657656

658657
it('handle reading a doc twice with different versions', () => {
659658
return integrationHelpers.withTestDb(persistence, db => {
660-
asyncQueue(db).skipDelaysForTimerId(TimerId.RetryTransaction);
659+
removeTransactionBackoffs(db);
661660
const doc = db.collection('counters').doc();
662661
let counter = 0;
663662
return doc

packages/firestore/test/integration/util/internal_helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export function asyncQueue(db: firestore.FirebaseFirestore): AsyncQueue {
3434
return (db as Firestore)._queue;
3535
}
3636

37+
export function removeTransactionBackoffs(
38+
db: firestore.FirebaseFirestore
39+
): void {
40+
(db as Firestore).removeTransactionBackoffs();
41+
}
42+
3743
export function getDefaultDatabaseInfo(): DatabaseInfo {
3844
return new DatabaseInfo(
3945
new DatabaseId(DEFAULT_PROJECT_ID),

0 commit comments

Comments
 (0)