forked from sindresorhus/p-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
646 lines (548 loc) · 17.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import test from 'ava';
import delay from 'delay';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import assertInRange from './assert-in-range.js';
import pMap, {pMapIterable, pMapSkip} from './index.js';
const sharedInput = [
[async () => 10, 300],
[20, 200],
Promise.resolve([30, 100]),
];
const longerSharedInput = [
[10, 300],
[20, 200],
[30, 100],
[40, 50],
[50, 25],
];
const errorInput1 = [
[20, 200],
[30, 100],
[async () => {
throw new Error('foo');
}, 10],
[() => {
throw new Error('bar');
}, 10],
];
const errorInput2 = [
[20, 200],
[async () => {
throw new Error('bar');
}, 10],
[30, 100],
[() => {
throw new Error('foo');
}, 10],
];
const errorInput3 = [
[20, 10],
[async () => {
throw new Error('bar');
}, 100],
[30, 100],
];
const mapper = async ([value, ms]) => {
await delay(ms);
if (typeof value === 'function') {
value = await value();
}
return value;
};
const mapperWithIndex = async ([value, ms], index) => {
await delay(ms);
if (typeof value === 'function') {
value = await value();
}
return {value, index};
};
class ThrowingIterator {
constructor(max, throwOnIndex) {
this._max = max;
this._throwOnIndex = throwOnIndex;
this.index = 0;
this[Symbol.iterator] = this[Symbol.iterator].bind(this);
}
[Symbol.iterator]() {
let index = 0;
const max = this._max;
const throwOnIndex = this._throwOnIndex;
return {
next: (() => {
try {
if (index === throwOnIndex) {
throw new Error(`throwing on index ${index}`);
}
const item = {value: index, done: index === max};
return item;
} finally {
index++;
this.index = index;
}
// eslint is wrong - bind is needed else the next() call cannot update
// this.index, which we need to track how many times the iterator was called
// eslint-disable-next-line no-extra-bind
}).bind(this),
};
}
}
test('main', async t => {
const end = timeSpan();
t.deepEqual(await pMap(sharedInput, mapper), [10, 20, 30]);
// We give it some leeway on both sides of the expected 300ms as the exact value depends on the machine and workload.
assertInRange(t, end(), {start: 290, end: 430});
});
test('concurrency: 1', async t => {
const end = timeSpan();
t.deepEqual(await pMap(sharedInput, mapper, {concurrency: 1}), [10, 20, 30]);
assertInRange(t, end(), {start: 590, end: 760});
});
test('concurrency: 4', async t => {
const concurrency = 4;
let running = 0;
await pMap(Array.from({length: 100}).fill(0), async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
running--;
}, {concurrency});
});
test('handles empty iterable', async t => {
t.deepEqual(await pMap([], mapper), []);
});
test('async with concurrency: 2 (random time sequence)', async t => {
const input = Array.from({length: 10}).map(() => randomInt(0, 100));
const mapper = value => delay(value, {value});
const result = await pMap(input, mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('async with concurrency: 2 (problematic time sequence)', async t => {
const input = [100, 200, 10, 36, 13, 45];
const mapper = value => delay(value, {value});
const result = await pMap(input, mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('async with concurrency: 2 (out of order time sequence)', async t => {
const input = [200, 100, 50];
const mapper = value => delay(value, {value});
const result = await pMap(input, mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('enforce number in options.concurrency', async t => {
await t.throwsAsync(pMap([], () => {}, {concurrency: 0}), {instanceOf: TypeError});
await t.throwsAsync(pMap([], () => {}, {concurrency: 1.5}), {instanceOf: TypeError});
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 1}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 10}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: Number.POSITIVE_INFINITY}));
});
test('immediately rejects when stopOnError is true', async t => {
await t.throwsAsync(pMap(errorInput1, mapper, {concurrency: 1}), {message: 'foo'});
await t.throwsAsync(pMap(errorInput2, mapper, {concurrency: 1}), {message: 'bar'});
});
test('aggregate errors when stopOnError is false', async t => {
await t.notThrowsAsync(pMap(sharedInput, mapper, {concurrency: 1, stopOnError: false}));
await t.throwsAsync(pMap(errorInput1, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: ''});
await t.throwsAsync(pMap(errorInput2, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: ''});
});
test('pMapSkip', async t => {
t.deepEqual(await pMap([
1,
pMapSkip,
2,
], async value => value), [1, 2]);
});
test('multiple pMapSkips', async t => {
t.deepEqual(await pMap([
1,
pMapSkip,
2,
pMapSkip,
3,
pMapSkip,
pMapSkip,
4,
], async value => value), [1, 2, 3, 4]);
});
test('all pMapSkips', async t => {
t.deepEqual(await pMap([
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip,
], async value => value), []);
});
test('all mappers should run when concurrency is infinite, even after stop-on-error happened', async t => {
const input = [1, async () => delay(300, {value: 2}), 3];
const mappedValues = [];
await t.throwsAsync(
pMap(input, async value => {
value = typeof value === 'function' ? await value() : value;
mappedValues.push(value);
if (value === 1) {
await delay(100);
throw new Error('Oops!');
}
}),
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
});
class AsyncTestData {
constructor(data) {
this.data = data;
}
async * [Symbol.asyncIterator]() {
for (let index = 0; index < this.data.length; index++) {
// Add a delay between each iterated item
// eslint-disable-next-line no-await-in-loop
await delay(10);
yield this.data[index];
}
}
}
//
// Async Iterator tests
//
test('asyncIterator - main', async t => {
const end = timeSpan();
t.deepEqual(await pMap(new AsyncTestData(sharedInput), mapper), [10, 20, 30]);
// We give it some leeway on both sides of the expected 300ms as the exact value depends on the machine and workload.
assertInRange(t, end(), {start: 290, end: 430});
});
test('asyncIterator - concurrency: 1', async t => {
const end = timeSpan();
t.deepEqual(await pMap(new AsyncTestData(sharedInput), mapper, {concurrency: 1}), [10, 20, 30]);
assertInRange(t, end(), {start: 590, end: 760});
});
test('asyncIterator - concurrency: 4', async t => {
const concurrency = 4;
let running = 0;
await pMap(new AsyncTestData(Array.from({length: 100}).fill(0)), async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
running--;
}, {concurrency});
});
test('asyncIterator - handles empty iterable', async t => {
t.deepEqual(await pMap(new AsyncTestData([]), mapper), []);
});
test('asyncIterator - async with concurrency: 2 (random time sequence)', async t => {
const input = Array.from({length: 10}).map(() => randomInt(0, 100));
const mapper = value => delay(value, {value});
const result = await pMap(new AsyncTestData(input), mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('asyncIterator - async with concurrency: 2 (problematic time sequence)', async t => {
const input = [100, 200, 10, 36, 13, 45];
const mapper = value => delay(value, {value});
const result = await pMap(new AsyncTestData(input), mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('asyncIterator - async with concurrency: 2 (out of order time sequence)', async t => {
const input = [200, 100, 50];
const mapper = value => delay(value, {value});
const result = await pMap(new AsyncTestData(input), mapper, {concurrency: 2});
t.deepEqual(result, input);
});
test('asyncIterator - enforce number in options.concurrency', async t => {
await t.throwsAsync(pMap(new AsyncTestData([]), () => {}, {concurrency: 0}), {instanceOf: TypeError});
await t.throwsAsync(pMap(new AsyncTestData([]), () => {}, {concurrency: 1.5}), {instanceOf: TypeError});
await t.notThrowsAsync(pMap(new AsyncTestData([]), () => {}, {concurrency: 1}));
await t.notThrowsAsync(pMap(new AsyncTestData([]), () => {}, {concurrency: 10}));
await t.notThrowsAsync(pMap(new AsyncTestData([]), () => {}, {concurrency: Number.POSITIVE_INFINITY}));
});
test('asyncIterator - immediately rejects when stopOnError is true', async t => {
await t.throwsAsync(pMap(new AsyncTestData(errorInput1), mapper, {concurrency: 1}), {message: 'foo'});
await t.throwsAsync(pMap(new AsyncTestData(errorInput2), mapper, {concurrency: 1}), {message: 'bar'});
});
test('asyncIterator - aggregate errors when stopOnError is false', async t => {
await t.notThrowsAsync(pMap(new AsyncTestData(sharedInput), mapper, {concurrency: 1, stopOnError: false}));
await t.throwsAsync(pMap(new AsyncTestData(errorInput1), mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: ''});
await t.throwsAsync(pMap(new AsyncTestData(errorInput2), mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: ''});
});
test('asyncIterator - pMapSkip', async t => {
t.deepEqual(await pMap(new AsyncTestData([
1,
pMapSkip,
2,
]), async value => value), [1, 2]);
});
test('asyncIterator - multiple pMapSkips', async t => {
t.deepEqual(await pMap(new AsyncTestData([
1,
pMapSkip,
2,
pMapSkip,
3,
pMapSkip,
pMapSkip,
4,
]), async value => value), [1, 2, 3, 4]);
});
test('asyncIterator - all pMapSkips', async t => {
t.deepEqual(await pMap(new AsyncTestData([
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip,
]), async value => value), []);
});
test('asyncIterator - all mappers should run when concurrency is infinite, even after stop-on-error happened', async t => {
const input = [1, async () => delay(300, {value: 2}), 3];
const mappedValues = [];
await t.throwsAsync(
pMap(new AsyncTestData(input), async value => {
if (typeof value === 'function') {
value = await value();
}
mappedValues.push(value);
if (value === 1) {
await delay(100);
throw new Error(`Oops! ${value}`);
}
}),
{message: 'Oops! 1'},
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
});
test('catches exception from source iterator - 1st item', async t => {
const input = new ThrowingIterator(100, 0);
const mappedValues = [];
const error = await t.throwsAsync(pMap(
input,
async value => {
mappedValues.push(value);
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true},
));
t.is(error.message, 'throwing on index 0');
t.is(input.index, 1);
await delay(300);
t.deepEqual(mappedValues, []);
});
// The 2nd iterable item throwing is distinct from the 1st when concurrency is 1 because
// it means that the source next() is invoked from next() and not from
// the constructor
test('catches exception from source iterator - 2nd item', async t => {
const input = new ThrowingIterator(100, 1);
const mappedValues = [];
await t.throwsAsync(pMap(
input,
async value => {
mappedValues.push(value);
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true},
));
await delay(300);
t.is(input.index, 2);
t.deepEqual(mappedValues, [0]);
});
// The 2nd iterable item throwing after a 1st item mapper exception, with stopOnError false,
// is distinct from other cases because our next() is called from a catch block
test('catches exception from source iterator - 2nd item after 1st item mapper throw', async t => {
const input = new ThrowingIterator(100, 1);
const mappedValues = [];
const error = await t.throwsAsync(pMap(
input,
async value => {
mappedValues.push(value);
await delay(100);
throw new Error('mapper threw error');
},
{concurrency: 1, stopOnError: false},
));
await delay(300);
t.is(error.message, 'throwing on index 1');
t.is(input.index, 2);
t.deepEqual(mappedValues, [0]);
});
test('asyncIterator - get the correct exception after stop-on-error', async t => {
const input = [1, async () => delay(200, {value: 2}), async () => delay(300, {value: 3})];
const mappedValues = [];
const task = pMap(new AsyncTestData(input), async value => {
if (typeof value === 'function') {
value = await value();
}
mappedValues.push(value);
// Throw for each item - all should fail and we should get only the first
await delay(100);
throw new Error(`Oops! ${value}`);
});
await delay(500);
await t.throwsAsync(task, {message: 'Oops! 1'});
t.deepEqual(mappedValues, [1, 2, 3]);
});
test('incorrect input type', async t => {
let mapperCalled = false;
const task = pMap(123_456, async () => {
mapperCalled = true;
await delay(100);
});
await delay(500);
await t.throwsAsync(task, {message: 'Expected `input` to be either an `Iterable` or `AsyncIterable`, got (number)'});
t.false(mapperCalled);
});
test('no unhandled rejected promises from mapper throws - infinite concurrency', async t => {
const input = [1, 2, 3];
const mappedValues = [];
await t.throwsAsync(
pMap(input, async value => {
mappedValues.push(value);
await delay(100);
throw new Error(`Oops! ${value}`);
}),
{message: 'Oops! 1'},
);
// Note: All 3 mappers get invoked, all 3 throw, even with `{stopOnError: true}` this
// should raise an AggregateError with all 3 exceptions instead of throwing 1
// exception and hiding the other 2.
t.deepEqual(mappedValues, [1, 2, 3]);
});
test('no unhandled rejected promises from mapper throws - concurrency 1', async t => {
const input = [1, 2, 3];
const mappedValues = [];
await t.throwsAsync(
pMap(input, async value => {
mappedValues.push(value);
await delay(100);
throw new Error(`Oops! ${value}`);
},
{concurrency: 1}),
{message: 'Oops! 1'},
);
t.deepEqual(mappedValues, [1]);
});
test('invalid mapper', async t => {
await t.throwsAsync(pMap([], 'invalid mapper', {concurrency: 2}), {instanceOf: TypeError});
});
if (globalThis.AbortController !== undefined) {
test('abort by AbortController', async t => {
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 100);
const mapper = async value => value;
await t.throwsAsync(pMap([delay(1000), new AsyncTestData(100), 100], mapper, {signal: abortController.signal}), {
name: 'AbortError',
});
});
test('already aborted signal', async t => {
const abortController = new AbortController();
abortController.abort();
const mapper = async value => value;
await t.throwsAsync(pMap([delay(1000), new AsyncTestData(100), 100], mapper, {signal: abortController.signal}), {
name: 'AbortError',
});
});
}
async function collectAsyncIterable(asyncIterable) {
const values = [];
for await (const value of asyncIterable) {
values.push(value);
}
return values;
}
test('pMapIterable', async t => {
t.deepEqual(await collectAsyncIterable(pMapIterable(sharedInput, mapper)), [10, 20, 30]);
});
test('pMapIterable - index in mapper', async t => {
t.deepEqual(await collectAsyncIterable(pMapIterable(sharedInput, mapperWithIndex)), [
{value: 10, index: 0},
{value: 20, index: 1},
{value: 30, index: 2},
]);
t.deepEqual(await collectAsyncIterable(pMapIterable(longerSharedInput, mapperWithIndex)), [
{value: 10, index: 0},
{value: 20, index: 1},
{value: 30, index: 2},
{value: 40, index: 3},
{value: 50, index: 4},
]);
});
test('pMapIterable - empty', async t => {
t.deepEqual(await collectAsyncIterable(pMapIterable([], mapper)), []);
});
test('pMapIterable - iterable that throws', async t => {
let isFirstNextCall = true;
const iterable = {
[Symbol.asyncIterator]() {
return {
async next() {
if (!isFirstNextCall) {
return {done: true};
}
isFirstNextCall = false;
throw new Error('foo');
},
};
},
};
const iterator = pMapIterable(iterable, mapper)[Symbol.asyncIterator]();
await t.throwsAsync(iterator.next(), {message: 'foo'});
});
test('pMapIterable - mapper that throws', async t => {
await t.throwsAsync(collectAsyncIterable(pMapIterable(sharedInput, async () => {
throw new Error('foo');
})), {message: 'foo'});
});
test('pMapIterable - stop on error', async t => {
const output = [];
try {
for await (const value of pMapIterable(errorInput3, mapper)) {
output.push(value);
}
} catch (error) {
t.is(error.message, 'bar');
}
t.deepEqual(output, [20]);
});
test('pMapIterable - concurrency: 1', async t => {
const end = timeSpan();
t.deepEqual(await collectAsyncIterable(pMapIterable(sharedInput, mapper, {concurrency: 1, backpressure: Number.POSITIVE_INFINITY})), [10, 20, 30]);
// It could've only taken this much time if each were run in series
assertInRange(t, end(), {start: 590, end: 760});
});
test('pMapIterable - concurrency: 2', async t => {
const times = new Map();
const end = timeSpan();
t.deepEqual(await collectAsyncIterable(pMapIterable(longerSharedInput, value => {
times.set(value[0], end());
return mapper(value);
}, {concurrency: 2, backpressure: Number.POSITIVE_INFINITY})), [10, 20, 30, 40, 50]);
assertInRange(t, times.get(10), {start: 0, end: 50});
assertInRange(t, times.get(20), {start: 0, end: 50});
assertInRange(t, times.get(30), {start: 200, end: 250});
assertInRange(t, times.get(40), {start: 300, end: 350});
assertInRange(t, times.get(50), {start: 300, end: 350});
});
test('pMapIterable - backpressure', async t => {
let currentValue;
// Concurrency option is forced by an early check
const asyncIterator = pMapIterable(longerSharedInput, async value => {
currentValue = await mapper(value);
return currentValue;
}, {backpressure: 2, concurrency: 2})[Symbol.asyncIterator]();
const {value: value1} = await asyncIterator.next();
t.is(value1, 10);
// If backpressure is not respected, than all items will be evaluated in this time
await delay(600);
t.is(currentValue, 30);
const {value: value2} = await asyncIterator.next();
t.is(value2, 20);
await delay(100);
t.is(currentValue, 40);
});
test('pMapIterable - pMapSkip', async t => {
t.deepEqual(await collectAsyncIterable(pMapIterable([
1,
pMapSkip,
2,
], async value => value)), [1, 2]);
});