Skip to content

Commit b427447

Browse files
author
Brian Chen
authored
Fix racy promises in SpecTestRunner (#2693)
1 parent 57c38ff commit b427447

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/firestore/src/util/async_queue.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,16 @@ export class AsyncQueue {
372372
* Waits until all currently queued tasks are finished executing. Delayed
373373
* operations are not run.
374374
*/
375-
drain(): Promise<void> {
376-
// It should still be possible to drain the queue after it is shutting down.
377-
return this.enqueueEvenAfterShutdown(() => Promise.resolve());
375+
async drain(): Promise<void> {
376+
// Operations in the queue prior to draining may have enqueued additional
377+
// operations. Keep draining the queue until the tail is no longer advanced,
378+
// which indicates that no more new operations were enqueued and that all
379+
// operations were executed.
380+
let currentTail: Promise<unknown>;
381+
do {
382+
currentTail = this.tail;
383+
await currentTail;
384+
} while (currentTail !== this.tail);
378385
}
379386

380387
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@ describeSpec('Limbo Documents:', [], () => {
476476
.expectLimboDocs(docB.key, docC.key)
477477
.client(1)
478478
.stealPrimaryLease()
479+
.expectListen(query, 'resume-token-1000000')
479480
.client(0)
480481
.runTimer(TimerId.ClientMetadataRefresh)
481482
.expectPrimaryState(false)
482483
.expectLimboDocs()
483484
.client(1)
484-
.expectListen(query, 'resume-token-1000000')
485485
.watchAcksFull(query, 3 * 1e6)
486486
.expectLimboDocs(docB.key, docC.key)
487487
.ackLimbo(3 * 1e6, deletedDocB)

0 commit comments

Comments
 (0)