This commit is contained in:
calzoneman 2013-09-06 15:53:23 -05:00
parent c2cd04f760
commit 6aecb32c89
3 changed files with 65 additions and 6 deletions

52
tests/rateLimiter.js Normal file
View file

@ -0,0 +1,52 @@
var $util = require('../lib/utilities.js');
function testBurst() {
var lim = $util.newRateLimiter();
var params = {
burst: 10,
sustained: 2
};
for (var i = 0; i < 10; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst: Unexpected throttle");
return;
}
}
if (!lim.throttle(params)) {
console.log("[FAIL] Burst: didn't throttle after exceeding burst amount");
return;
}
console.log("[PASS] Burst");
}
function testBurstAndWait() {
var lim = $util.newRateLimiter();
var params = {
burst: 10,
sustained: 2
};
for (var i = 0; i < 9; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst & Wait: Unexpected throttle");
return;
}
}
// Wait a while and try some more
setTimeout(function () {
for (var i = 9; i < 17; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst & Wait: Unexpected throttle");
return;
}
}
console.log("[PASS] Burst & Wait");
}, 6000);
}
testBurst();
testBurstAndWait();