-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
test.js
260 lines (231 loc) · 7.29 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
import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import AggregateError from 'aggregate-error';
import pMap, {pMapSkip} from './index.js';
const sharedInput = [
Promise.resolve([10, 300]),
[20, 200],
[30, 100]
];
const errorInput1 = [
[20, 200],
[30, 100],
[() => Promise.reject(new Error('foo')), 10],
[() => {
throw new Error('bar');
}, 10]
];
const errorInput2 = [
[20, 200],
[() => Promise.reject(new Error('bar')), 10],
[30, 100],
[() => {
throw new Error('foo');
}, 10]
];
const mapper = async ([value, ms]) => {
await delay(ms);
if (typeof value === 'function') {
value = await value();
}
return value;
};
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.
t.true(inRange(end(), {start: 290, end: 430}));
});
test('concurrency: 1', async t => {
const end = timeSpan();
t.deepEqual(await pMap(sharedInput, mapper, {concurrency: 1}), [10, 20, 30]);
t.true(inRange(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: /foo(.|\n)*bar/});
await t.throwsAsync(pMap(errorInput2, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: /bar(.|\n)*foo/});
});
test('pMapSkip', async t => {
t.deepEqual(await pMap([
1,
pMapSkip,
2
], async value => value), [1, 2]);
});
test('do not run mapping after stop-on-error happened', async t => {
const input = [1, delay(300, {value: 2}), 3];
const mappedValues = [];
await t.throwsAsync(
pMap(input, async value => {
mappedValues.push(value);
if (value === 1) {
await delay(100);
throw new Error('Oops!');
}
},
{concurrency: 1})
);
await delay(500);
t.deepEqual(mappedValues, [1]);
});
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('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: 'Some big AggregateError message'}
);
// Note: all 3 mappers get invoked, all 3 throw, even with stop on error 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]);
});