-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
65 lines (53 loc) · 1.48 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
'use strict';
const test = require('tape');
const valvelet = require('.');
test('is exported as a function', (t) => {
t.equal(typeof valvelet, 'function');
t.end();
});
test('returns a function that returns a promise', (t) => {
const limit = valvelet(() => 'foo', 1, 10);
limit().then((value) => {
t.equal(value, 'foo');
t.end();
});
});
test('calls the original function with the same context and arguments', (t) => {
const limit = valvelet(function () {
t.deepEqual(arguments, (function () { return arguments; })(1, 2));
t.equal(this, 'foo');
}, 1, 10);
limit.call('foo', 1, 2).then(t.end);
});
test('allows to limit the queue size', (t) => {
const limit = valvelet(() => {}, 1, 10, 2);
limit();
limit();
limit();
limit().then(() => {
t.fail('Promise should not be fulfilled');
t.end();
}, (err) => {
t.equal(err instanceof Error, true);
t.equal(err.message, 'Queue is full');
t.end();
});
});
test('limits the execution rate of the original function', (t) => {
const values = [1, 2, 3, 4, 5];
const start = Date.now();
const times = [];
const limit = valvelet((arg) => {
times.push(Date.now());
return Promise.resolve(arg);
}, 2, 100);
Promise.all(values.map((i) => limit(i))).then((data) => {
t.deepEqual(data, values);
times.forEach((time, i) => {
const delay = Math.floor(i / 2) * 100;
const diff = time - start - delay;
t.ok(diff >= 0 && diff < 20);
});
t.end();
});
});