forked from sindresorhus/p-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
77 lines (65 loc) · 1.83 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
import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import PQueue from './';
const fixture = Symbol('fixture');
test('.add()', async t => {
const queue = new PQueue();
const p = queue.add(async () => fixture);
t.is(queue.size, 0);
t.is(queue.pending, 1);
t.is(await p, fixture);
});
test('.add() - limited concurrency', async t => {
const queue = new PQueue({concurrency: 2});
const p = queue.add(async () => fixture);
const p2 = queue.add(async () => delay(100).then(() => fixture));
const p3 = queue.add(async () => fixture);
t.is(queue.size, 1);
t.is(queue.pending, 2);
t.is(await p, fixture);
t.is(await p2, fixture);
t.is(await p3, fixture);
});
test('.add() - concurrency: 1', async t => {
const input = [
[10, 300],
[20, 200],
[30, 100]
];
const end = timeSpan();
const queue = new PQueue({concurrency: 1});
const mapper = ([val, ms]) => queue.add(() => delay(ms).then(() => val));
t.deepEqual(await Promise.all(input.map(mapper)), [10, 20, 30]);
t.true(inRange(end(), 590, 650));
});
test('.add() - concurrency: 5', async t => {
const concurrency = 5;
const queue = new PQueue({concurrency});
let running = 0;
const input = Array(100).fill(0).map(() => queue.add(async () => {
running++;
t.true(running <= concurrency);
t.true(queue.pending <= concurrency);
await delay(randomInt(30, 200));
running--;
}));
await Promise.all(input);
});
test('.onEmpty()', async t => {
const queue = new PQueue({concurrency: 1});
queue.add(async () => 0);
queue.add(async () => 0);
t.is(queue.size, 1);
t.is(queue.pending, 1);
await queue.onEmpty();
t.is(queue.size, 0);
queue.add(async () => 0);
queue.add(async () => 0);
t.is(queue.size, 1);
t.is(queue.pending, 1);
await queue.onEmpty();
t.is(queue.size, 0);
});