Skip to content

Commit e0e0179

Browse files
authored
Merge pull request #5991 from scality/improvement/CLDSRV-770/rate_limit_config_loading
CLDSRV-770: parseRateLimitConfig unit tests
2 parents fcc2688 + 289cf0a commit e0e0179

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

lib/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ class Config extends EventEmitter {
18571857
typeof config.rateLimiting.bucket.defaultConfig, 'object',
18581858
'rateLimiting.bucket.defaultConfig must be an object'
18591859
);
1860-
defaultConfig = parseRateLimitConfig(config.rateLimiting.bucket);
1860+
defaultConfig = parseRateLimitConfig(config.rateLimiting.bucket.defaultConfig);
18611861
}
18621862

18631863
let configCacheTTL = constants.rateLimitDefaultConfigCacheTTL;

lib/api/apiUtils/rateLimit/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ function parseRateLimitConfig(config) {
1919
}
2020

2121
limitConfig.requestsPerSecond = {
22-
interval: Math.ceil((1 / limit) * 1000),
22+
interval: 1000 / limit,
2323
bucketSize: burstCapacity * 1000,
2424
};
2525
}
26+
27+
return limitConfig;
2628
}
2729

2830
module.exports = {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const assert = require('assert');
2+
3+
const { parseRateLimitConfig } = require('../../../../../lib/api/apiUtils/rateLimit/config');
4+
5+
describe('test parseRateLimitConfig', () => {
6+
const testCases = [
7+
{
8+
desc: 'should return an empty config if given one',
9+
input: {},
10+
expected: {},
11+
},
12+
{
13+
desc: '[rps] should calculate the request interval',
14+
input: {
15+
requestsPerSecond: {
16+
limit: 500
17+
},
18+
},
19+
expected: {
20+
requestsPerSecond: {
21+
interval: 2,
22+
bucketSize: 1000,
23+
}
24+
},
25+
},
26+
{
27+
desc: '[rps] should calculate bucket size',
28+
input: {
29+
requestsPerSecond: {
30+
limit: 500,
31+
burstCapacity: 5,
32+
},
33+
},
34+
expected: {
35+
requestsPerSecond: {
36+
interval: 2,
37+
bucketSize: 5000,
38+
}
39+
},
40+
},
41+
{
42+
desc: '[rps] should throw an error if requestsPerSecond isn\'t an object',
43+
input: {
44+
requestsPerSecond: 'foo'
45+
},
46+
throws: true,
47+
},
48+
{
49+
desc: '[rps] should throw an error if requestsPerSecond.limit isn\'t a number',
50+
input: {
51+
requestsPerSecond: {
52+
limit: 'foo',
53+
},
54+
},
55+
throws: true,
56+
},
57+
{
58+
desc: '[rps] should throw an error if requestsPerSecond.burstCapacity isn\'t a number',
59+
input: {
60+
requestsPerSecond: {
61+
limit: 500,
62+
burstCapacity: '5',
63+
},
64+
},
65+
throws: true,
66+
},
67+
];
68+
69+
testCases.forEach(testCase => {
70+
it(testCase.desc, () => {
71+
if (testCase.throws) {
72+
let err;
73+
try {
74+
parseRateLimitConfig(testCase.input);
75+
} catch (e) {
76+
err = e;
77+
}
78+
assert(err instanceof Error);
79+
} else {
80+
assert.deepStrictEqual(parseRateLimitConfig(testCase.input), testCase.expected);
81+
}
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)