-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
43 lines (36 loc) · 1002 Bytes
/
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
import test from 'ava';
import inRange from 'in-range';
import timeSpan from 'time-span';
import m from './';
const fixture = Symbol('fixture');
test('main', async t => {
const totalRuns = 100;
const limit = 5;
const interval = 100;
const end = timeSpan();
const throttled = m(async () => {}, limit, interval);
await Promise.all(Array(totalRuns).fill(0).map(throttled));
const totalTime = (totalRuns * interval) / limit;
t.true(inRange(end(), totalTime - 100, totalTime + 100));
});
test('passes arguments through', async t => {
const throttled = m(async x => x, 1, 100);
t.is(await throttled(fixture), fixture);
});
test('can be aborted', async t => {
const limit = 1;
const interval = 10000; // 10 seconds
const end = timeSpan();
const throttled = m(async () => {}, limit, interval);
await throttled();
const p = throttled();
throttled.abort();
let error;
try {
await p;
} catch (err) {
error = err;
}
t.true(error instanceof m.AbortError);
t.true(end() < 100);
});