Skip to content

Commit 9a24cc0

Browse files
author
Brian Chen
authored
fix: fix flaky BulkWriter test (#1115)
1 parent 8ffe256 commit 9a24cc0

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

dev/test/bulk-writer.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ describe('BulkWriter', () => {
550550
describe('500/50/5 support', () => {
551551
afterEach(() => setTimeoutHandler(setTimeout));
552552

553-
it('does not send batches if doing so exceeds the rate limit', done => {
553+
it('does not send batches if doing so exceeds the rate limit', async () => {
554554
// The test is considered a success if BulkWriter tries to send the second
555555
// batch again after a timeout.
556556

@@ -561,7 +561,7 @@ describe('BulkWriter', () => {
561561
const requests2 = arrayRange2.map(i => setOp('doc' + i, 'bar'));
562562
const responses2 = arrayRange2.map(i => successResponse(i));
563563

564-
instantiateInstance([
564+
const bulkWriter = await instantiateInstance([
565565
{
566566
request: createRequest(requests1),
567567
response: mergeResponses(responses1),
@@ -570,33 +570,34 @@ describe('BulkWriter', () => {
570570
request: createRequest(requests2),
571571
response: mergeResponses(responses2),
572572
},
573-
]).then(bulkWriter => {
574-
setTimeoutHandler(() =>
575-
done(new Error('This batch should not have a timeout'))
576-
);
577-
for (let i = 0; i < 500; i++) {
578-
bulkWriter
579-
.set(firestore.doc('collectionId/doc' + i), {foo: 'bar'})
580-
.then(incrementOpCount);
581-
}
582-
bulkWriter.flush();
583-
584-
// Sending this next batch would go over the 500/50/5 capacity, so
585-
// check that BulkWriter doesn't send this batch until the first batch
586-
// is resolved.
587-
setTimeoutHandler((_, timeout) => {
588-
// Check that BulkWriter has not yet sent the 2nd batch.
589-
expect(requestCounter).to.equal(0);
590-
expect(timeout).to.be.greaterThan(0);
591-
done();
592-
});
593-
for (let i = 500; i < 505; i++) {
594-
bulkWriter
595-
.set(firestore.doc('collectionId/doc' + i), {foo: 'bar'})
596-
.then(incrementOpCount);
597-
}
598-
return bulkWriter.flush();
573+
]);
574+
for (let i = 0; i < 500; i++) {
575+
bulkWriter
576+
.set(firestore.doc('collectionId/doc' + i), {foo: 'bar'})
577+
.then(incrementOpCount);
578+
}
579+
const flush1 = bulkWriter.flush();
580+
581+
// Sending this next batch would go over the 500/50/5 capacity, so
582+
// check that BulkWriter doesn't send this batch until the first batch
583+
// is resolved.
584+
let timeoutCalled = false;
585+
setTimeoutHandler((_, timeout) => {
586+
// Check that BulkWriter has not yet sent the 2nd batch.
587+
timeoutCalled = true;
588+
expect(requestCounter).to.equal(0);
589+
expect(timeout).to.be.greaterThan(0);
599590
});
591+
for (let i = 500; i < 505; i++) {
592+
bulkWriter
593+
.set(firestore.doc('collectionId/doc' + i), {foo: 'bar'})
594+
.then(incrementOpCount);
595+
}
596+
const flush2 = bulkWriter.flush();
597+
await flush1;
598+
await flush2;
599+
expect(timeoutCalled).to.be.true;
600+
return bulkWriter.close();
600601
});
601602
});
602603

0 commit comments

Comments
 (0)