-
Notifications
You must be signed in to change notification settings - Fork 86
/
mapParallelAsyncWithLimit.spec.js
53 lines (43 loc) · 1.4 KB
/
mapParallelAsyncWithLimit.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import isCI from 'is-ci'
import { composeAsync } from './composeAsync.js'
import { delay } from './delay.js'
import { mapAsync } from './mapAsync.js'
import { mapParallelAsyncWithLimit } from './mapParallelAsyncWithLimit.js'
import { toDecimal } from './toDecimal.js'
jest.setTimeout(30000)
test('happy', async () => {
const limit = 3
const startTime = new Date().getTime()
const list = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
const iterable = async x => {
await delay(500)
return x + 1
}
const result = await mapParallelAsyncWithLimit(
iterable, limit, list
)
const endTime = new Date().getTime()
const diffTime = endTime - startTime
const startTime2 = new Date().getTime()
await mapAsync(iterable, list)
const endTime2 = new Date().getTime()
const diffTime2 = endTime2 - startTime2
const methodScale = toDecimal((diffTime2 - diffTime) / 1000, 0)
expect(result).toEqual([ 2, 3, 4, 5, 6, 7, 8, 9, 10 ])
if (!isCI) expect(methodScale).toBe(limit)
})
const fn = async x => {
await delay(100)
return x + 1
}
test('with R.composeAsync', async () => {
const result = await composeAsync(mapParallelAsyncWithLimit(fn, 2), x =>
x.map(xx => xx + 1))([ 1, 2, 3, 4, 5, 6 ])
expect(result).toEqual([ 3, 4, 5, 6, 7, 8 ])
})
test('fallback to R.mapFastAsync', async () => {
const result = await mapParallelAsyncWithLimit(
fn, 4, [ 1, 2, 3 ]
)
expect(result).toEqual([ 2, 3, 4 ])
})