-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
test.js
235 lines (188 loc) · 4.8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import {AsyncLocalStorage} from 'node:async_hooks';
import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import pLimit, {limitFunction} from './index.js';
test('concurrency: 1', async t => {
const input = [
[10, 300],
[20, 200],
[30, 100],
];
const end = timeSpan();
const limit = pLimit(1);
const mapper = ([value, ms]) => limit(async () => {
await delay(ms);
return value;
});
t.deepEqual(await Promise.all(input.map(x => mapper(x))), [10, 20, 30]);
t.true(inRange(end(), {start: 590, end: 650}));
});
test('concurrency: 4', async t => {
const concurrency = 5;
let running = 0;
const limit = pLimit(concurrency);
const input = Array.from({length: 100}, () => limit(async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
running--;
}));
await Promise.all(input);
});
test('propagates async execution context properly', async t => {
const concurrency = 2;
const limit = pLimit(concurrency);
const store = new AsyncLocalStorage();
const checkId = async id => {
await Promise.resolve();
t.is(id, store.getStore()?.id);
};
const startContext = async id => store.run({id}, () => limit(checkId, id));
await Promise.all(
Array.from({length: 100}, (_, id) => startContext(id)),
);
});
test('non-promise returning function', async t => {
await t.notThrowsAsync(async () => {
const limit = pLimit(1);
await limit(() => null);
});
});
test('continues after sync throw', async t => {
const limit = pLimit(1);
let ran = false;
const promises = [
limit(() => {
throw new Error('err');
}),
limit(() => {
ran = true;
}),
];
await Promise.all(promises).catch(() => {});
t.is(ran, true);
});
test('accepts additional arguments', async t => {
const limit = pLimit(1);
const symbol = Symbol('test');
await limit(a => t.is(a, symbol), symbol);
});
test('does not ignore errors', async t => {
const limit = pLimit(1);
const error = new Error('🦄');
const promises = [
limit(async () => {
await delay(30);
}),
limit(async () => {
await delay(80);
throw error;
}),
limit(async () => {
await delay(50);
}),
];
await t.throwsAsync(Promise.all(promises), {is: error});
});
test('activeCount and pendingCount properties', async t => {
const limit = pLimit(5);
t.is(limit.activeCount, 0);
t.is(limit.pendingCount, 0);
const runningPromise1 = limit(() => delay(1000));
t.is(limit.activeCount, 0);
t.is(limit.pendingCount, 1);
await Promise.resolve();
t.is(limit.activeCount, 1);
t.is(limit.pendingCount, 0);
await runningPromise1;
t.is(limit.activeCount, 0);
t.is(limit.pendingCount, 0);
const immediatePromises = Array.from({length: 5}, () => limit(() => delay(1000)));
const delayedPromises = Array.from({length: 3}, () => limit(() => delay(1000)));
await Promise.resolve();
t.is(limit.activeCount, 5);
t.is(limit.pendingCount, 3);
await Promise.all(immediatePromises);
t.is(limit.activeCount, 3);
t.is(limit.pendingCount, 0);
await Promise.all(delayedPromises);
t.is(limit.activeCount, 0);
t.is(limit.pendingCount, 0);
});
test('clearQueue', async t => {
const limit = pLimit(1);
Array.from({length: 1}, () => limit(() => delay(1000)));
Array.from({length: 3}, () => limit(() => delay(1000)));
await Promise.resolve();
t.is(limit.pendingCount, 3);
limit.clearQueue();
t.is(limit.pendingCount, 0);
});
test('throws on invalid concurrency argument', t => {
t.throws(() => {
pLimit(0);
});
t.throws(() => {
pLimit(-1);
});
t.throws(() => {
pLimit(1.2);
});
t.throws(() => {
pLimit(undefined);
});
t.throws(() => {
pLimit(true);
});
});
test('change concurrency to smaller value', async t => {
const limit = pLimit(4);
let running = 0;
const log = [];
const promises = Array.from({length: 10}).map(() =>
limit(async () => {
++running;
log.push(running);
await delay(50);
--running;
}),
);
await delay(0);
t.is(running, 4);
limit.concurrency = 2;
await Promise.all(promises);
t.deepEqual(log, [1, 2, 3, 4, 2, 2, 2, 2, 2, 2]);
});
test('change concurrency to bigger value', async t => {
const limit = pLimit(2);
let running = 0;
const log = [];
const promises = Array.from({length: 10}).map(() =>
limit(async () => {
++running;
log.push(running);
await delay(50);
--running;
}),
);
await delay(0);
t.is(running, 2);
limit.concurrency = 4;
await Promise.all(promises);
t.deepEqual(log, [1, 2, 3, 4, 4, 4, 4, 4, 4, 4]);
});
test('limitFunction()', async t => {
const concurrency = 5;
let running = 0;
const limitedFunction = limitFunction(async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
running--;
}, {concurrency});
const input = Array.from({length: 100}, limitedFunction);
await Promise.all(input);
});