|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { chunkArray, mapInBatches, runInBatches } from '../../../src/utils/concurrent-batch'; |
| 4 | + |
| 5 | +describe('concurrent-batch', () => { |
| 6 | + describe('chunkArray', () => { |
| 7 | + it('should split an array into chunks of at most `size`', () => { |
| 8 | + expect(chunkArray([1, 2, 3, 4, 5], 2)).to.deep.equal([[1, 2], [3, 4], [5]]); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should return a single chunk when size >= length', () => { |
| 12 | + expect(chunkArray([1, 2, 3], 10)).to.deep.equal([[1, 2, 3]]); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return the whole array as one chunk when size <= 0', () => { |
| 16 | + expect(chunkArray([1, 2, 3], 0)).to.deep.equal([[1, 2, 3]]); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return [] for an empty array', () => { |
| 20 | + expect(chunkArray([], 3)).to.deep.equal([]); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('mapInBatches', () => { |
| 25 | + it('should collect results in input order', async () => { |
| 26 | + const results = await mapInBatches([1, 2, 3, 4, 5], 2, async (n) => n * 10); |
| 27 | + expect(results).to.deep.equal([10, 20, 30, 40, 50]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should pass the correct absolute index across batches', async () => { |
| 31 | + const indexes: number[] = []; |
| 32 | + await mapInBatches(['a', 'b', 'c', 'd', 'e'], 2, async (_item, index) => { |
| 33 | + indexes.push(index); |
| 34 | + return index; |
| 35 | + }); |
| 36 | + expect([...indexes].sort((a, b) => a - b)).to.deep.equal([0, 1, 2, 3, 4]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should never run more than `concurrency` tasks at once', async () => { |
| 40 | + let inFlight = 0; |
| 41 | + let maxInFlight = 0; |
| 42 | + await mapInBatches(Array.from({ length: 10 }, (_, i) => i), 3, async (n) => { |
| 43 | + inFlight += 1; |
| 44 | + maxInFlight = Math.max(maxInFlight, inFlight); |
| 45 | + await new Promise((resolve) => setImmediate(resolve)); |
| 46 | + inFlight -= 1; |
| 47 | + return n; |
| 48 | + }); |
| 49 | + expect(maxInFlight).to.be.at.most(3); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return [] for an empty array without invoking fn', async () => { |
| 53 | + let called = false; |
| 54 | + const results = await mapInBatches([], 5, async (n) => { |
| 55 | + called = true; |
| 56 | + return n; |
| 57 | + }); |
| 58 | + expect(results).to.deep.equal([]); |
| 59 | + expect(called).to.equal(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should fail fast when a task rejects', async () => { |
| 63 | + let error: Error | undefined; |
| 64 | + try { |
| 65 | + await mapInBatches([1, 2, 3], 2, async (n) => { |
| 66 | + if (n === 2) throw new Error('boom'); |
| 67 | + return n; |
| 68 | + }); |
| 69 | + } catch (e) { |
| 70 | + error = e as Error; |
| 71 | + } |
| 72 | + expect(error).to.be.instanceOf(Error); |
| 73 | + expect(error?.message).to.equal('boom'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should treat concurrency < 1 as 1', async () => { |
| 77 | + const results = await mapInBatches([1, 2, 3], 0, async (n) => n); |
| 78 | + expect(results).to.deep.equal([1, 2, 3]); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('runInBatches', () => { |
| 83 | + it('should invoke fn for every item with the correct absolute index', async () => { |
| 84 | + const seen: Array<{ item: string; index: number }> = []; |
| 85 | + await runInBatches(['a', 'b', 'c'], 2, async (item, index) => { |
| 86 | + seen.push({ item, index }); |
| 87 | + }); |
| 88 | + expect(seen.sort((a, b) => a.index - b.index)).to.deep.equal([ |
| 89 | + { item: 'a', index: 0 }, |
| 90 | + { item: 'b', index: 1 }, |
| 91 | + { item: 'c', index: 2 }, |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not abort the batch when one task rejects (fault-tolerant)', async () => { |
| 96 | + const completed: number[] = []; |
| 97 | + await runInBatches([1, 2, 3, 4], 2, async (n) => { |
| 98 | + if (n === 2) throw new Error('boom'); |
| 99 | + completed.push(n); |
| 100 | + }); |
| 101 | + expect(completed.sort((a, b) => a - b)).to.deep.equal([1, 3, 4]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should be a no-op for an empty array', async () => { |
| 105 | + let called = false; |
| 106 | + await runInBatches([], 5, async () => { |
| 107 | + called = true; |
| 108 | + }); |
| 109 | + expect(called).to.equal(false); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments