Skip to content

Commit 5c3b0b3

Browse files
author
Brian Chen
committed
add iterative drain on AQ
1 parent b739763 commit 5c3b0b3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

packages/firestore/src/util/async_queue.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ export class AsyncQueue {
206206
// visible for testing
207207
failure: Error | null = null;
208208

209+
// Number of unexecuted operations remaining on the AsyncQueue.
210+
private operationsCount = 0;
211+
209212
// Flag set while there's an outstanding AsyncQueue operation, used for
210213
// assertion sanity-checks.
211214
private operationInProgress = false;
@@ -219,6 +222,11 @@ export class AsyncQueue {
219222
return this._isShuttingDown;
220223
}
221224

225+
// Visible for testing
226+
isEmpty(): boolean {
227+
return this.operationsCount === 0;
228+
}
229+
222230
/**
223231
* Adds a new operation to the queue without waiting for it to complete (i.e.
224232
* we ignore the Promise result).
@@ -280,6 +288,7 @@ export class AsyncQueue {
280288
}
281289

282290
private enqueueInternal<T extends unknown>(op: () => Promise<T>): Promise<T> {
291+
this.operationsCount += 1;
283292
const newTail = this.tail.then(() => {
284293
this.operationInProgress = true;
285294
return op()
@@ -305,6 +314,7 @@ export class AsyncQueue {
305314
})
306315
.then(result => {
307316
this.operationInProgress = false;
317+
this.operationsCount -= 1;
308318
return result;
309319
});
310320
});

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,16 @@ abstract class TestRunner {
549549
async run(step: SpecStep): Promise<void> {
550550
await this.doStep(step);
551551
await this.queue.drain();
552+
// Wait for any additional operations that the initial operations
553+
// scheduled. We skip this iterative drain for steps that involve changing
554+
// the primary tab since tabs that become the primary can schedule
555+
// multiple operations onto the queue that subsequent steps require.
556+
while (
557+
step.applyClientState?.primary === undefined &&
558+
!this.queue.isEmpty()
559+
) {
560+
await this.queue.drain();
561+
}
552562
this.validateExpectedSnapshotEvents(step.expectedSnapshotEvents!);
553563
await this.validateExpectedState(step.expectedState!);
554564
this.validateSnapshotsInSyncEvents(step.expectedSnapshotsInSyncEvents);

0 commit comments

Comments
 (0)