forked from iphands/event-horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
70 lines (58 loc) · 1.98 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
/*global require, describe, it, console, setTimeout*/
'use strict';
var assert = require('assert'),
horizon = require('./event-horizon');
function funcFactory(cb) {
return function () {
cb();
};
}
function loop(max, t, i, eaten, run) {
for (i = 0; i < max; i += 1) {
t.run(funcFactory(run), funcFactory(eaten));
}
}
describe('Event-Horizon', function () {
it('should eat things that happen too fast', function () {
var t = horizon.instance({ window: 1000, max: 5 }),
run = 0,
eaten = 0,
i = 0;
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(5, run);
assert.equal(95, eaten);
});
it('should use a sliding window', function (done) {
var t = horizon.instance({ window: 20, max: 10 }),
run = 0,
eaten = 0,
i = 0;
this.timeout(500);
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
}, 18);
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(10, run);
assert.equal(190, eaten);
done();
}, 21);
});
it('should reset when I go over window', function (done) {
var t = horizon.instance({ window: 10, max: 10 }),
run = 0,
eaten = 0,
i = 0;
this.timeout(500);
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(30, run);
assert.equal(270, eaten);
done();
}, 11);
}, 11);
});
});