Improved naming to rate limiter tests

This commit is contained in:
Anubhav Jain
2015-07-20 19:59:58 -07:00
parent b83427e369
commit a957e613ed

View File

@@ -7,16 +7,16 @@ Tinytest.add(
'Check single rule with multiple invocations, only 1 that matches',
function (test) {
r = new RateLimiter();
var myUserId = 1;
var rule1 = {
var userIdOne = 1;
var restrictJustUserIdOneRule = {
userId: myUserId,
IPAddr: null,
method: null
};
r.addRule(rule1, 1, 1000);
r.addRule(restrictJustUserId1Rule, 1, 1000);
var connectionHandle = createTempConnectionHandle(123, '127.0.0.1');
var methodInvc1 = createTempMethodInvocation(myUserId, connectionHandle,
var methodInvc1 = createTempMethodInvocation(userIdOne, connectionHandle,
'login');
var methodInvc2 = createTempMethodInvocation(2, connectionHandle,
'login');
@@ -32,17 +32,18 @@ testAsyncMulti("Run multiple invocations and wait for one to return", [
function (test, expect) {
var self = this;
self.r = new RateLimiter();
self.myUserId = 1;
self.rule1 = {
userId: self.myUserId,
self.userIdOne = 1;
self.userIdTwo = 2;
self.restrictJustUserIdOneRule = {
userId: myUserId,
IPAddr: null,
method: null
};
self.r.addRule(self.rule1, 1, 1000);
self.r.addRule(self.restrictJustUserIdOneRule, 1, 1000);
self.connectionHandle = createTempConnectionHandle(123, '127.0.0.1')
self.methodInvc1 = createTempMethodInvocation(self.myUserId, self.connectionHandle,
self.methodInvc1 = createTempMethodInvocation(self.userIdOne, self.connectionHandle,
'login');
self.methodInvc2 = createTempMethodInvocation(2, self.connectionHandle,
self.methodInvc2 = createTempMethodInvocation(self.userIdTwo, self.connectionHandle,
'login');
for (var i = 0; i < 2; i++) {
self.r.increment(self.methodInvc1);
@@ -66,27 +67,28 @@ testAsyncMulti("Run multiple invocations and wait for one to return", [
Tinytest.add('Check two rules that affect same methodInvc still throw',
function (test) {
r = new RateLimiter();
var loginRule = {
var loginMethodRule = {
userId: null,
IPAddr: null,
method: 'login'
};
var userIdRule = {
var onlyLimitEvenUserIdRule = {
userId: function (userId) {
return userId % 2 === 0
},
IPAddr: null,
method: null
};
r.addRule(loginRule, 10, 100);
r.addRule(userIdRule, 4, 100);
r.addRule(loginMethodRule, 10, 100);
r.addRule(onlyLimitEvenUserIdRule, 4, 100);
var connectionHandle = createTempConnectionHandle(1234, '127.0.0.1');
var methodInvc1 = createTempMethodInvocation(1, connectionHandle,
'login');
var methodInvc2 = createTempMethodInvocation(2, connectionHandle,
'login');
var methodInvc3 = createTempMethodInvocation(3, connectionHandle, 'test');
var methodInvc3 = createTempMethodInvocation(3, connectionHandle,
'test');
for (var i = 0; i < 5; i++) {
r.increment(methodInvc1);
@@ -103,6 +105,7 @@ Tinytest.add('Check two rules that affect same methodInvc still throw',
// Running one more test causes it to be false, since we're at 11 now.
r.increment(methodInvc1);
test.equal(r.check(methodInvc1).allowed, false);
// 3rd Method Invocation isn't affected by either rules.
test.equal(r.check(methodInvc3).allowed, true);
});
@@ -110,12 +113,12 @@ Tinytest.add('Check two rules that affect same methodInvc still throw',
Tinytest.add('Check two rules that are affected by different invocations',
function (test) {
r = new RateLimiter();
var loginRule = {
var loginMethodRule = {
userId: null,
IPAddr: null,
method: 'login'
}
r.addRule(loginRule, 10, 10000);
r.addRule(loginMethodRule, 10, 10000);
var connectionHandle = createTempConnectionHandle(1234, '127.0.0.1');
var methodInvc1 = createTempMethodInvocation(1, connectionHandle,
@@ -127,6 +130,8 @@ Tinytest.add('Check two rules that are affected by different invocations',
r.increment(methodInvc1);
r.increment(methodInvc2);
}
// This throws us over the limit since both increment the login rule
// counter
r.increment(methodInvc1);
test.equal(r.check(methodInvc1).allowed, false);
@@ -152,17 +157,19 @@ Tinytest.add("add global rule", function (test) {
var methodInvc3 = createTempMethodInvocation(3, connectionHandle,
'user-accounts');
// First invocation, all methods would still be allowed.
r.increment(methodInvc2);
test.equal(r.check(methodInvc1).allowed, true);
test.equal(r.check(methodInvc2).allowed, true);
test.equal(r.check(methodInvc3).allowed, true);
// Second invocation, everything has reached common rate limit
r.increment(methodInvc3);
test.equal(r.check(methodInvc1).allowed, false);
test.equal(r.check(methodInvc2).allowed, false);
test.equal(r.check(methodInvc3).allowed, false);
});
Tinytest.add('add fuzzy rule match doesnt trigger', function (test) {
Tinytest.add('Fuzzy rule match does not trigger rate limit', function (test) {
r = new RateLimiter();
var rule = {
a: function (inp) {
@@ -191,8 +198,8 @@ Tinytest.add('add fuzzy rule match doesnt trigger', function (test) {
// Past limit so should be false
test.equal(r.check(matchingInput).allowed, false);
// Add secondary rule and check that longer time is returned when multiple rules limits are hit
// Add secondary rule and check that longer time is returned when multiple
// rules limits are hit
var newRule = {
a: function (inp) {
return inp % 3 == 0
@@ -202,15 +209,15 @@ Tinytest.add('add fuzzy rule match doesnt trigger', function (test) {
d: 1
}
r.addRule(newRule, 1, 10);
// First rule should still throw while second rule will trigger as well, causing us to return
// longer time to reset to user
// First rule should still throw while second rule will trigger as well,
// causing us to return longer time to reset to user
r.increment(matchingInput);
r.increment(matchingInput);
test.equal(r.check(matchingInput).timeToReset > 50, true);
});
/****** Test Helper Methods *****/
/****** Test Our Helper Methods *****/
Tinytest.add("test matchRule method", function (test) {
r = new RateLimiter();
@@ -222,14 +229,14 @@ Tinytest.add("test matchRule method", function (test) {
}
var globalRuleId = r.addRule(globalRule);
var RateLimiterInput = {
var rateLimiterInput = {
userId: 1023,
IPAddr: "127.0.0.1",
type: 'sub',
name: 'getSubLists'
};
test.equal(r.rules[globalRuleId].match(RateLimiterInput), true);
test.equal(r.rules[globalRuleId].match(rateLimiterInput), true);
var oneNotNullRule = {
userId: 102,
@@ -238,18 +245,18 @@ Tinytest.add("test matchRule method", function (test) {
name: null
}
var oneNotId = r.addRule(oneNotNullRule);
test.equal(r.rules[oneNotId].match(RateLimiterInput), false);
var oneNotNullId = r.addRule(oneNotNullRule);
test.equal(r.rules[oneNotNullId].match(RateLimiterInput), false);
oneNotNullRule.userId = 1023;
test.equal(r.rules[oneNotId].match(RateLimiterInput), true);
test.equal(r.rules[oneNotNullId].match(RateLimiterInput), true);
var notCompleteInput = {
userId: 102,
IPAddr: '127.0.0.1'
};
test.equal(r.rules[globalRuleId].match(notCompleteInput), true);
test.equal(r.rules[oneNotId].match(notCompleteInput), false);
test.equal(r.rules[oneNotNullId].match(notCompleteInput), false);
});
Tinytest.add('test generateMethodKey string', function (test) {
@@ -262,17 +269,17 @@ Tinytest.add('test generateMethodKey string', function (test) {
}
var globalRuleId = r.addRule(globalRule);
var RateLimiterInput = {
var rateLimiterInput = {
userId: 1023,
IPAddr: "127.0.0.1",
type: 'sub',
name: 'getSubLists'
};
test.equal(r.rules[globalRuleId]._generateKeyString(RateLimiterInput), "");
test.equal(r.rules[globalRuleId]._generateKeyString(rateLimiterInput), "");
globalRule.userId = 1023;
test.equal(r.rules[globalRuleId]._generateKeyString(RateLimiterInput),
test.equal(r.rules[globalRuleId]._generateKeyString(rateLimiterInput),
"userId1023");
var ruleWithFuncs = {
@@ -283,15 +290,15 @@ Tinytest.add('test generateMethodKey string', function (test) {
type: null
};
var funcRuleId = r.addRule(ruleWithFuncs);
test.equal(r.rules[funcRuleId]._generateKeyString(RateLimiterInput), "");
RateLimiterInput.userId = 1024;
test.equal(r.rules[funcRuleId]._generateKeyString(RateLimiterInput),
test.equal(r.rules[funcRuleId]._generateKeyString(rateLimiterInput), "");
rateLimiterInput.userId = 1024;
test.equal(r.rules[funcRuleId]._generateKeyString(rateLimiterInput),
"userId1024");
var multipleRules = ruleWithFuncs;
multipleRules.IPAddr = '127.0.0.1';
var multipleRuleId = r.addRule(multipleRules);
test.equal(r.rules[multipleRuleId]._generateKeyString(RateLimiterInput),
test.equal(r.rules[multipleRuleId]._generateKeyString(rateLimiterInput),
"userId1024IPAddr127.0.0.1")
})