|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { expect } from 'chai'; |
| 8 | +import { ThrottledPromiseAll } from '../src/throttledPromiseAll'; |
| 9 | +import { Duration } from '../src/duration'; |
| 10 | + |
| 11 | +describe('throttledPromiseAll', () => { |
| 12 | + const numberProducer = ( |
| 13 | + source: number, |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 15 | + throttledPromise: ThrottledPromiseAll<number, number | undefined> |
| 16 | + ): Promise<number> => Promise.resolve(source + 1); |
| 17 | + |
| 18 | + it('should execute promises in order', async () => { |
| 19 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ concurrency: 1 }); |
| 20 | + for (const i of [1, 2, 3, 4, 5]) { |
| 21 | + // eslint-disable-next-line no-await-in-loop |
| 22 | + throttledPromiseAll.add(i, numberProducer); |
| 23 | + } |
| 24 | + await throttledPromiseAll.all(); |
| 25 | + const results = throttledPromiseAll.getResults() as number[]; |
| 26 | + expect(results).to.deep.equal([1, 2, 3, 4, 5].map((i) => i + 1)); |
| 27 | + }); |
| 28 | + it('should execute promises in groups of 2 - auto start', async () => { |
| 29 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ concurrency: 2 }); |
| 30 | + for (const i of [1, 2, 3, 4, 5]) { |
| 31 | + // eslint-disable-next-line no-await-in-loop |
| 32 | + throttledPromiseAll.add(i, numberProducer); |
| 33 | + } |
| 34 | + await throttledPromiseAll.all(); |
| 35 | + const results = throttledPromiseAll.getResults() as number[]; |
| 36 | + expect(results).to.deep.equal([1, 2, 3, 4, 5].map((i) => i + 1)); |
| 37 | + }); |
| 38 | + it('should execute promises in groups of 10 - auto start', async () => { |
| 39 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ concurrency: 10 }); |
| 40 | + for (const i of [1, 2, 3, 4, 5]) { |
| 41 | + // eslint-disable-next-line no-await-in-loop |
| 42 | + throttledPromiseAll.add(i, numberProducer); |
| 43 | + } |
| 44 | + await throttledPromiseAll.all(); |
| 45 | + const results = throttledPromiseAll.getResults() as number[]; |
| 46 | + expect(results).to.deep.equal([1, 2, 3, 4, 5].map((i) => i + 1)); |
| 47 | + }); |
| 48 | + it('should should reject', async () => { |
| 49 | + try { |
| 50 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ |
| 51 | + concurrency: 1, |
| 52 | + timeout: Duration.milliseconds(5000), |
| 53 | + }); |
| 54 | + throttledPromiseAll.add([1], (source) => Promise.reject(new Error(`Promise ${source} rejected`))); |
| 55 | + await throttledPromiseAll.all(); |
| 56 | + } catch (e) { |
| 57 | + expect((e as Error).message).to.equal('Promise 1 rejected'); |
| 58 | + } |
| 59 | + }); |
| 60 | + it('should should timeout', async () => { |
| 61 | + try { |
| 62 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ |
| 63 | + concurrency: 1, |
| 64 | + timeout: Duration.milliseconds(100), |
| 65 | + }); |
| 66 | + throttledPromiseAll.add( |
| 67 | + [1, 2, 3, 4, 5], |
| 68 | + (source) => new Promise((resolve) => setTimeout(() => resolve(source + 1), 10000)) |
| 69 | + ); |
| 70 | + await throttledPromiseAll.all(); |
| 71 | + } catch (e) { |
| 72 | + expect((e as Error).message).to.equal('PromiseQueue timed out after 100 milliseconds'); |
| 73 | + } |
| 74 | + }); |
| 75 | + it('should add more to the queue', async () => { |
| 76 | + const throttledPromiseAll: ThrottledPromiseAll<number, number> = new ThrottledPromiseAll({ concurrency: 1 }); |
| 77 | + throttledPromiseAll.add( |
| 78 | + 1, |
| 79 | + (source: number, throttledPromise: ThrottledPromiseAll<number, number | undefined>): Promise<number> => { |
| 80 | + if (source === 1) { |
| 81 | + throttledPromise.add([2, 3, 4, 5], numberProducer); |
| 82 | + } |
| 83 | + return Promise.resolve(source + 1); |
| 84 | + } |
| 85 | + ); |
| 86 | + await throttledPromiseAll.all(); |
| 87 | + const results = throttledPromiseAll.getResults() as number[]; |
| 88 | + expect(results).to.deep.equal([1, 2, 3, 4, 5].map((i) => i + 1)); |
| 89 | + }); |
| 90 | +}); |
0 commit comments