-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
test.js
426 lines (336 loc) · 11 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
import test from 'ava';
import inRange from 'in-range';
import timeSpan from 'time-span';
import delay from 'delay';
import pThrottle, {AbortError} from './index.js';
const fixture = Symbol('fixture');
test('main', async t => {
const totalRuns = 100;
const limit = 5;
const interval = 100;
const end = timeSpan();
const throttled = pThrottle({limit, interval})(async () => {});
await Promise.all(Array.from({length: totalRuns}).fill(0).map(x => throttled(x)));
const totalTime = (totalRuns * interval) / limit;
t.true(inRange(end(), {
start: totalTime - 200,
end: totalTime + 200,
}));
});
test('queue size', async t => {
const limit = 10;
const interval = 100;
const delayedExecutions = 20;
const throttled = pThrottle({limit, interval})(() => Date.now());
const promises = [];
t.is(throttled.queueSize, 0);
for (let index = 0; index < limit; index++) {
promises.push(throttled());
}
t.is(throttled.queueSize, 0);
for (let index = 0; index < delayedExecutions; index++) {
promises.push(throttled());
}
t.is(throttled.queueSize, delayedExecutions);
await Promise.all(promises);
t.is(throttled.queueSize, 0);
});
test('strict mode', async t => {
const totalRuns = 100;
const limit = 5;
const interval = 100;
const strict = true;
const end = timeSpan();
const throttled = pThrottle({limit, interval, strict})(async () => {});
await Promise.all(Array.from({length: totalRuns}).fill(0).map(x => throttled(x)));
const totalTime = (totalRuns * interval) / limit;
t.true(inRange(end(), {
start: totalTime - 200,
end: totalTime + 200,
}));
});
test('limits after pause in strict mode', async t => {
const limit = 10;
const interval = 100;
const strict = true;
const throttled = pThrottle({limit, interval, strict})(() => Date.now());
const pause = 40;
const promises = [];
const start = Date.now();
await throttled();
await delay(pause);
for (let index = 0; index < limit + 1; index++) {
promises.push(throttled());
}
const results = await Promise.all(promises);
for (const [index, executed] of results.entries()) {
const elapsed = executed - start;
if (index < limit - 1) {
t.true(inRange(elapsed, {start: pause, end: pause + 50}), 'Executed immediately after the pause');
} else if (index === limit - 1) {
t.true(inRange(elapsed, {start: interval, end: interval + 50}), 'Executed after the interval');
} else {
const difference = executed - results[index - limit];
t.true(inRange(difference, {start: interval - 10, end: interval + 50}), 'Waited the interval');
}
}
});
test('limits after pause in windowed mode', async t => {
const limit = 10;
const interval = 100;
const strict = false;
const throttled = pThrottle({limit, interval, strict})(() => Date.now());
const pause = 40;
const promises = [];
const start = Date.now();
await throttled();
await delay(pause);
for (let index = 0; index < limit + 1; index++) {
promises.push(throttled());
}
const results = await Promise.all(promises);
for (const [index, executed] of results.entries()) {
const elapsed = executed - start;
if (index < limit - 1) {
t.true(inRange(elapsed, {start: pause, end: pause + 10}), 'Executed immediately after the pause');
} else {
t.true(inRange(elapsed, {start: interval - 10, end: interval + 10}), 'Executed immediately after the interval');
}
}
});
test('passes arguments through', async t => {
const throttled = pThrottle({limit: 1, interval: 100})(async x => x);
t.is(await throttled(fixture), fixture);
});
test('can be aborted', async t => {
const limit = 1;
const interval = 10_000; // 10 seconds
const end = timeSpan();
const throttled = pThrottle({limit, interval})(async () => {});
await throttled();
const promise = throttled();
throttled.abort();
let error;
try {
await promise;
} catch (error_) {
error = error_;
}
t.true(error instanceof AbortError);
t.true(end() < 100);
});
test('can be disabled', async t => {
let counter = 0;
const throttled = pThrottle({
limit: 1,
interval: 10_000,
})(async () => ++counter);
t.is(await throttled(), 1);
const end = timeSpan();
throttled.isEnabled = false;
t.is(await throttled(), 2);
t.true(end() < 200);
});
test('promise rejections are thrown', async t => {
const throttled = pThrottle({
limit: 1,
interval: 10_000,
})(() => Promise.reject(new Error('Catch me if you can!')));
await t.throwsAsync(throttled, {
instanceOf: Error,
message: 'Catch me if you can!',
});
});
test('`this` is preserved in throttled function', async t => {
class FixtureClass {
constructor() {
this._foo = fixture;
}
foo() {
// If `this` is not preserved by `pThrottle()`
// then `this` will be undefined and accesing `this._foo` will throw.
return this._foo;
}
getThis() {
// If `this` is not preserved by `pThrottle()`
// then `this` will be undefined.
return this;
}
}
FixtureClass.prototype.foo = pThrottle({limit: 1, interval: 100})(FixtureClass.prototype.foo);
FixtureClass.prototype.getThis = pThrottle({limit: 1, interval: 100})(FixtureClass.prototype.getThis);
const thisFixture = new FixtureClass();
t.is(await thisFixture.getThis(), thisFixture);
await t.notThrowsAsync(thisFixture.foo());
t.is(await thisFixture.foo(), fixture);
});
for (const limit of [1, 5, 10]) {
test(`respects limit of ${limit} calls`, async t => {
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const promises = [];
const start = Date.now();
for (let i = 0; i < limit; i++) {
promises.push(throttled());
}
const results = await Promise.all(promises);
for (const time of results) {
t.true(inRange(time - start, {start: 0, end: interval}));
}
});
}
test('handles multiple instances independently', async t => {
const throttledOne = pThrottle({limit: 1, interval: 100})(() => 'one');
const throttledTwo = pThrottle({limit: 1, interval: 200})(() => 'two');
const resultOne = await throttledOne();
const resultTwo = await throttledTwo();
t.is(resultOne, 'one');
t.is(resultTwo, 'two');
});
test('disable and re-enable functionality', async t => {
const throttled = pThrottle({limit: 1, interval: 1000})(() => Date.now());
const start = Date.now();
await throttled(); // First call, should pass immediately.
throttled.isEnabled = false;
const timeDisabled = await throttled(); // Should pass immediately.
throttled.isEnabled = true;
const timeReEnabled = await throttled(); // Should be throttled.
t.true(timeDisabled - start < 100);
t.true(timeReEnabled - start >= 1000);
});
test('stability under high load', async t => {
const limit = 5;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const promises = [];
for (let i = 0; i < 100; i++) {
promises.push(throttled());
}
const results = await Promise.all(promises);
t.is(results.length, 100);
});
test('handles zero interval', async t => {
const throttled = pThrottle({limit: 1, interval: 0})(() => Date.now());
const start = Date.now();
await throttled();
const end = Date.now();
t.true(end - start < 50); // Small buffer to account for execution time
});
test('handles simultaneous calls', async t => {
const limit = 5;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const times = await Promise.all(Array.from({length: limit}).map(() => throttled()));
// Ensure all calls are within the same interval
for (let i = 1; i < times.length; i++) {
t.true(times[i] - times[0] < interval);
}
});
test('clears queue after abort', async t => {
const limit = 2;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
try {
await throttled();
await throttled();
} catch {}
throttled.abort();
t.is(throttled.queueSize, 0);
});
test('allows immediate execution with high limit', async t => {
const limit = 10;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const start = Date.now();
const promises = Array.from({length: 5}, () => throttled());
const results = await Promise.all(promises);
const end = Date.now();
for (const time of results) {
t.true(time - start < 50);
}
t.true(end - start < 100);
});
test('queues calls beyond limit', async t => {
const limit = 2;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const start = Date.now();
const firstBatch = Promise.all([throttled(), throttled()]);
await delay(50); // Ensure the first batch is within the limit
const secondBatch = Promise.all([throttled(), throttled()]);
const results = await Promise.all([firstBatch, secondBatch]);
const end = Date.now();
// Check that the second batch was executed after the interval
for (const time of results[1]) {
t.true(time - start >= interval);
}
t.true(end - start >= interval);
});
test('resets interval after inactivity', async t => {
const limit = 1;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const firstCall = await throttled();
await delay(interval + 50); // Inactivity longer than the interval
const secondCall = await throttled();
t.true(secondCall - firstCall >= interval + 50);
});
test('maintains function execution order', async t => {
const limit = 2;
const interval = 100;
const throttled = pThrottle({limit, interval})(async value => value);
const results = await Promise.all([throttled(1), throttled(2), throttled(3)]);
t.deepEqual(results, [1, 2, 3]);
});
test('handles extremely short intervals', async t => {
const limit = 1;
const interval = 1; // Very short interval
const throttled = pThrottle({limit, interval})(() => {});
await throttled();
await delay(5); // Slight delay
await throttled();
t.pass(); // If it gets here without error, the test passes
});
test('executes immediately for limit greater than calls', async t => {
const limit = 10;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const start = Date.now();
const results = await Promise.all([throttled(), throttled()]);
const end = Date.now();
for (const time of results) {
t.true(time - start < 50);
}
t.true(end - start < 100);
});
test('manages rapid successive calls', async t => {
const limit = 3;
const interval = 50;
const throttled = pThrottle({limit, interval})(() => Date.now());
const results = [];
for (let i = 0; i < 10; i++) {
results.push(throttled());
// eslint-disable-next-line no-await-in-loop
await delay(10); // Small delay between calls
}
await Promise.all(results);
t.pass(); // Test passes if all promises resolve without error
});
test('onDelay', async t => {
let delayedCounter = 0;
const limit = 10;
const interval = 100;
const delayedExecutions = 20;
const onDelay = () => delayedCounter++;
const throttled = pThrottle({limit, interval, onDelay})(() => Date.now());
const promises = [];
for (let index = 0; index < limit; index++) {
promises.push(throttled());
}
t.is(delayedCounter, 0);
for (let index = 0; index < delayedExecutions; index++) {
promises.push(throttled());
}
t.is(delayedCounter, delayedExecutions);
await Promise.all(promises);
});