-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
413 lines (395 loc) · 10.2 KB
/
index.spec.ts
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import {PromiseQueue, TimeoutError} from '../index.js';
import * as ava from 'ava';
import delay from 'delay';
const test = ava.default;
test('TaskQueue addTask', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1});
const res = await queue.add(async () => {
await delay(10);
return result;
});
t.is(res, result);
});
test('TaskQueue addTask with signal', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1});
const ac = new AbortController();
const res = await queue.add(async () => {
await delay(10);
return result;
}, {
signal: ac.signal,
});
t.is(res, result);
ac.abort();
await t.throwsAsync(queue.add(async () => {
await delay(10);
return result;
}, {
signal: ac.signal,
}), {instanceOf: Error, message: 'This operation was aborted'});
});
test('TaskQueue addTask with signal and abort all', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 5});
const ac1 = new AbortController();
const ac2 = new AbortController();
const promise1 = queue.add(async () => {
await delay(10);
return result;
}, {
signal: ac1.signal,
});
const promise2 = queue.add(async () => {
await delay(10);
return result;
}, {
signal: ac2.signal,
});
ac1.abort();
ac2.abort();
await t.throwsAsync(promise1, {instanceOf: Error, message: 'This operation was aborted'});
await t.throwsAsync(promise2, {instanceOf: Error, message: 'This operation was aborted'});
t.is(queue.size, 0);
t.is(queue.pending, 0);
});
test('TaskQueue with timeout', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1, timeout: 20});
const res = await queue.add(async () => {
await delay(10);
return result;
});
t.is(res, result);
await t.throwsAsync(queue.add(async () => {
await delay(30);
return result;
}), {instanceOf: TimeoutError, message: 'Promise timeout'});
});
test('TaskQueue with timeout and has changed', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1, timeout: 20});
const res = await queue.add(async () => {
await delay(10);
return result;
});
t.is(res, result);
await t.throwsAsync(queue.add(async () => {
await delay(30);
return result;
}), {instanceOf: TimeoutError, message: 'Promise timeout'});
queue.timeout = 50;
t.is(await queue.add(async () => {
await delay(30);
return result;
}), result);
});
test('TaskQueue with timeout addTask with signal', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1, timeout: 20});
const ac = new AbortController();
const res = await queue.add(async () => {
await delay(10);
return result;
});
t.is(res, result);
await t.throwsAsync(queue.add(async () => {
await delay(30);
return result;
}, {signal: ac.signal}), {instanceOf: TimeoutError, message: 'Promise timeout'});
setTimeout(() => {
ac.abort();
}, 10);
await t.throwsAsync(queue.add(async () => {
await delay(30);
return result;
}, {signal: ac.signal}), {instanceOf: Error, message: 'This operation was aborted'});
});
test('TaskQueue addTask check size and pending', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1});
const promise = queue.add(async () => {
await delay(10);
return result;
});
t.is(queue.size, 0);
t.is(queue.pending, 1);
t.is(await promise, result);
});
test('TaskQueue addTask with concurrency, check size and pending', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 2});
const promise1 = queue.add(async () => {
await delay(10);
return 1;
});
const promise2 = queue.add(async () => {
await delay(20);
return 2;
});
const promise3 = queue.add(async () => {
await delay(20);
return 3;
});
const promise4 = queue.add(async () => {
await delay(20);
return 4;
});
const promise5 = queue.add(async () => {
await delay(20);
return 5;
});
t.is(queue.size, 3);
t.is(queue.pending, 2);
t.is(await promise1, 1);
t.is(await promise2, 2);
t.is(queue.size, 1);
t.is(queue.pending, 2);
t.is(await promise3, 3);
t.is(queue.size, 0);
t.is(queue.pending, 2);
t.is(await promise4, 4);
t.is(queue.size, 0);
t.is(queue.pending, 1);
t.is(await promise5, 5);
});
test('TaskQueue addTask with priority, check order', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 2});
const res: number[] = [];
const promise1 = queue.add(async () => {
await delay(10);
res.push(1);
}, {priority: 10});
const promise2 = queue.add(async () => {
await delay(20);
res.push(2);
}, {priority: 10});
const promise3 = queue.add(async () => {
await delay(20);
res.push(3);
}, {priority: -1});
const promise4 = queue.add(async () => {
await delay(20);
res.push(4);
}, {priority: 0});
const promise5 = queue.add(async () => {
await delay(20);
res.push(5);
}, {priority: 5});
await queue.onIdle();
t.deepEqual(res, [1, 2, 5, 4, 3]);
});
test('TaskQueue addTask with concurrency 1, check run time', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 1});
const times = [10, 20, 30, 40, 50];
const start = Date.now();
t.deepEqual(await Promise.all(times.map(async (time) => {
return queue.add(async () => {
await delay(time);
return time;
});
})), times);
const range = Date.now() - start;
t.true(range >= 130 && range <= 230);
});
test('TaskQueue addTask with concurrency 5, check run time', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 5});
const times = [20, 40, 60, 80, 100];
const start = Date.now();
t.deepEqual(await Promise.all(times.map(async (time) => {
return queue.add(async () => {
await delay(time);
return time;
});
})), times);
const range = Date.now() - start;
t.true(range >= 80 && range <= 120);
});
test('TaskQueue update concurrency', async (t) => {
const result = 'test';
let concurrency = 5;
const queue = new PromiseQueue({concurrency});
let running = 0;
for (const i of Array(100).keys()) {
void queue.add(async () => {
running++;
t.true(running <= concurrency);
t.true(queue.pending <= concurrency);
await delay(10 + Math.random() * 20);
running--;
if (i % 12 === 0) {
queue.concurrency = ++concurrency;
t.is(queue.concurrency, concurrency);
}
if (i % 15 === 0) {
queue.concurrency = --concurrency;
t.is(queue.concurrency, concurrency);
}
});
}
await queue.onIdle();
t.pass();
});
test('TaskQueue onSizeLessThan and onEmpty and onIdle', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 5});
for (const i of Array(10).keys()) {
void queue.add(async () => {
await delay(20 + Math.random() * 10);
});
}
await queue.onSizeLessThan(3);
t.true(queue.size < 3);
await queue.onEmpty();
t.is(queue.size, 0);
t.true(queue.pending > 0);
await queue.onIdle();
t.is(queue.size, 0);
t.is(queue.pending, 0);
});
test('TaskQueue addAll', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 2});
let promise = queue.addAll([
async () => {
await delay(20 + Math.random() * 10);
return 1;
},
async () => {
await delay(20 + Math.random() * 10);
throw new Error('test');
},
]);
t.is(queue.size, 0);
t.is(queue.pending, 2);
t.deepEqual(await promise, [{
status: 'fulfilled',
value: 1,
}, {
reason: new Error('test'),
status: 'rejected',
}]);
queue.concurrency = 1;
promise = queue.addAll([
async () => {
await delay(20 + Math.random() * 10);
return 1;
},
async () => {
await delay(20 + Math.random() * 10);
return 2;
},
]);
t.is(queue.size, 1);
t.is(queue.pending, 1);
t.deepEqual(await promise, [{
status: 'fulfilled',
value: 1,
}, {
status: 'fulfilled',
value: 2,
}]);
});
test('TaskQueue autoStart false', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 2, autoStart: false});
const promise = queue.addAll([
async () => {
await delay(20 + Math.random() * 10);
return 1;
},
async () => {
await delay(20 + Math.random() * 10);
return 2;
},
async () => {
await delay(20 + Math.random() * 10);
return 3;
},
async () => {
await delay(20 + Math.random() * 10);
return 4;
},
]);
t.is(queue.size, 4);
t.is(queue.pending, 0);
t.is(queue.isPaused, true);
queue.start();
t.is(queue.size, 2);
t.is(queue.pending, 2);
t.is(queue.isPaused, false);
t.deepEqual(await promise, [{
status: 'fulfilled',
value: 1,
}, {
status: 'fulfilled',
value: 2,
}, {
status: 'fulfilled',
value: 3,
}, {
status: 'fulfilled',
value: 4,
}]);
});
test('TaskQueue pause', async (t) => {
const result = 'test';
const queue = new PromiseQueue({concurrency: 2, autoStart: false});
const promise = queue.addAll([
async () => {
await delay(20 + Math.random() * 10);
return 1;
},
async () => {
await delay(20 + Math.random() * 10);
return 2;
},
async () => {
await delay(20 + Math.random() * 10);
return 3;
},
async () => {
await delay(20 + Math.random() * 10);
return 4;
},
]);
t.is(queue.size, 4);
t.is(queue.pending, 0);
t.is(queue.isPaused, true);
queue.start();
t.is(queue.size, 2);
t.is(queue.pending, 2);
t.is(queue.isPaused, false);
void queue.add(async () => {
await delay(20 + Math.random() * 10);
return 5;
});
queue.pause();
t.is(queue.size, 3);
t.is(queue.pending, 2);
t.is(queue.isPaused, true);
await delay(50);
t.is(queue.size, 3);
t.is(queue.pending, 0);
queue.start();
t.is(queue.size, 1);
t.is(queue.pending, 2);
t.is(queue.isPaused, false);
t.deepEqual(await promise, [{
status: 'fulfilled',
value: 1,
}, {
status: 'fulfilled',
value: 2,
}, {
status: 'fulfilled',
value: 3,
}, {
status: 'fulfilled',
value: 4,
}]);
});