-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
95 lines (85 loc) · 2.66 KB
/
test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const test = require('tape')
const sinon = require('sinon')
const parallelFn = require('./index')
function setup (parallelism = 2) {
const taskStub = sinon.stub()
taskStub.resolves()
return {
taskStub,
scheduler: parallelFn(taskStub, parallelism)
}
}
test('parallelFn creates a task queue up to a max parallelism limit', async (t) => {
const { taskStub, scheduler } = setup()
scheduler()
scheduler()
scheduler()
t.ok(taskStub.calledTwice, 'task function only called up to max parallelism')
t.end()
})
test('parallelFn creates a task queue up to a max parallelism limit and starts functions when possible', async (t) => {
const { taskStub, scheduler } = setup()
taskStub.onCall(0).resolves()
scheduler()
scheduler()
await scheduler()
t.ok(taskStub.calledThrice, 'task function called 3 times')
t.end()
})
test('parallelFn calls taskFn with original arguments', (t) => {
const { taskStub, scheduler } = setup()
const a = 1
const b = { c: 2 }
scheduler(a, b)
t.ok(taskStub.calledWithExactly(a, b), 'task function called with arguments')
t.ok(taskStub.calledOnce, 'task function called once')
t.end()
})
test('parallelFn calls taskFn with original context', (t) => {
const { taskStub, scheduler } = setup()
const a = 1
const b = { c: 2 }
const context = {}
scheduler.call(context, a, b)
t.equal(taskStub.thisValues[0], context, 'task function called with correct context')
t.ok(taskStub.calledOnce, 'task function called once')
t.end()
})
test('parallelFn returns a promise whose resolution is the return resolution of the task function', async (t) => {
const { taskStub, scheduler } = setup()
const expected = 'orange'
taskStub.onCall(0).resolves(expected)
const value = await scheduler()
t.equal(value, expected, 'task function resolution is as expected')
t.end()
})
test('parallelFn returns the promise rejection when the task function rejects', async (t) => {
const { taskStub, scheduler } = setup()
const err = Error('snap!')
taskStub.onCall(0).rejects(err)
try {
await scheduler()
} catch (e) {
t.equal(e, err, 'task function rejection throws error')
}
t.end()
})
test('parallelFn starts other tasks on task rejection', async (t) => {
const { taskStub, scheduler } = setup()
taskStub.onCall(0).resolves()
try {
await scheduler()
} catch (e) {}
scheduler()
scheduler()
t.ok(taskStub.calledThrice, 'task function called 3 times')
t.end()
})
test('parallelFn has Infinity parallelism when not specified', (t) => {
const { taskStub, scheduler } = setup(null)
for (let x = 0; x < 1000; ++x) {
scheduler()
}
t.equal(1000, taskStub.getCalls().length, 'called 1000 times')
t.end()
})