|
| 1 | +import { delay, noop, range } from 'es-toolkit' |
| 2 | +import { expect, describe, it } from 'vitest' |
| 3 | +import { createLimitedVersionOfFn, pLimit } from '../src' |
| 4 | +import { approximateCostTime } from './_shared' |
| 5 | + |
| 6 | +describe('concurrency invalid check', () => { |
| 7 | + describe('pLimit(concurrency)', () => { |
| 8 | + it('should throws when concurrency is invalid', () => { |
| 9 | + expect(() => pLimit(0)).toThrowError('concurrency must be greater than 0') |
| 10 | + expect(() => pLimit(Infinity)).toThrowError('concurrency must be finite') |
| 11 | + }) |
| 12 | + }) |
| 13 | + |
| 14 | + describe('createLimitedVersionOfFn(fn, concurrency)', () => { |
| 15 | + it('should throws when concurrency is invalid', () => { |
| 16 | + expect(() => createLimitedVersionOfFn(noop, 0)).toThrowError( |
| 17 | + 'concurrency must be greater than 0', |
| 18 | + ) |
| 19 | + expect(() => createLimitedVersionOfFn(noop, Infinity)).toThrowError( |
| 20 | + 'concurrency must be finite', |
| 21 | + ) |
| 22 | + }) |
| 23 | + }) |
| 24 | +}) |
| 25 | + |
| 26 | +describe('concurrency control is correct', () => { |
| 27 | + it('pLimit', async function () { |
| 28 | + let arr = range(5) // [0 .. 4] |
| 29 | + async function measureCostTime(concurrency: number) { |
| 30 | + let start = performance.now() |
| 31 | + |
| 32 | + async function work(x: number) { |
| 33 | + await delay(x * 10) |
| 34 | + return x * 10 |
| 35 | + } |
| 36 | + |
| 37 | + const limit = pLimit(concurrency) |
| 38 | + const result = await Promise.all(arr.map((value) => limit(() => work(value)))) |
| 39 | + expect(result).toEqual(arr.map((value) => value * 10)) |
| 40 | + |
| 41 | + const cost = performance.now() - start |
| 42 | + return cost |
| 43 | + } |
| 44 | + |
| 45 | + function approximateCostTime(time: number, tolerance: number) { |
| 46 | + return function satisfy(val: number) { |
| 47 | + return Math.abs(val - time) <= Math.abs(tolerance) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const tolerance = 10 |
| 52 | + |
| 53 | + // Infinity |
| 54 | + expect(await measureCostTime(arr.length)).toSatisfy(approximateCostTime(40, tolerance)) |
| 55 | + |
| 56 | + // executor-0: 0ms 20ms 40ms |
| 57 | + // executor-1: 10ms 30ms |
| 58 | + expect(await measureCostTime(2)).toSatisfy(approximateCostTime(60, tolerance)) |
| 59 | + |
| 60 | + // executor-0: 0ms 30ms |
| 61 | + // executor-1: 10ms 40ms |
| 62 | + // executor-2: 20ms |
| 63 | + expect(await measureCostTime(3)).toSatisfy(approximateCostTime(50, tolerance)) |
| 64 | + }) |
| 65 | + |
| 66 | + it('createLimitedVersionOfFn', async function () { |
| 67 | + let arr = range(5) // [0 .. 4] |
| 68 | + async function measureCostTime(concurrency: number) { |
| 69 | + let start = performance.now() |
| 70 | + |
| 71 | + async function work(x: number) { |
| 72 | + await delay(x * 10) |
| 73 | + return x * 10 |
| 74 | + } |
| 75 | + |
| 76 | + const workWithLimit = createLimitedVersionOfFn(work, concurrency) |
| 77 | + const result = await Promise.all(arr.map((x) => workWithLimit(x))) |
| 78 | + expect(result).toEqual(arr.map((value) => value * 10)) |
| 79 | + |
| 80 | + const cost = performance.now() - start |
| 81 | + return cost |
| 82 | + } |
| 83 | + |
| 84 | + const tolerance = 10 |
| 85 | + |
| 86 | + // Infinity |
| 87 | + expect(await measureCostTime(arr.length)).toSatisfy(approximateCostTime(40, tolerance)) |
| 88 | + |
| 89 | + // executor-0: 0ms 20ms 40ms |
| 90 | + // executor-1: 10ms 30ms |
| 91 | + expect(await measureCostTime(2)).toSatisfy(approximateCostTime(60, tolerance)) |
| 92 | + |
| 93 | + // executor-0: 0ms 30ms |
| 94 | + // executor-1: 10ms 40ms |
| 95 | + // executor-2: 20ms |
| 96 | + expect(await measureCostTime(3)).toSatisfy(approximateCostTime(50, tolerance)) |
| 97 | + }) |
| 98 | +}) |
0 commit comments