Skip to content

Commit cb396dc

Browse files
committed
CLDSRV-810: Add unit tests for token bucket for config
1 parent d30d844 commit cb396dc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/unit/api/apiUtils/rateLimit/tokenBucket.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,72 @@ describe('WorkerTokenBucket', () => {
5151
assert.strictEqual(bucket.refillInProgress, false);
5252
assert.strictEqual(bucket.refillCount, 0);
5353
});
54+
55+
it('should use custom bufferSize from config.rateLimiting', () => {
56+
sandbox.restore();
57+
sandbox.stub(config, 'rateLimiting').value({
58+
nodes: 1,
59+
tokenBucketBufferSize: 100,
60+
bucket: {
61+
defaultConfig: {
62+
requestsPerSecond: { burstCapacity: 2 },
63+
},
64+
},
65+
});
66+
67+
const bucket = new tokenBucket.WorkerTokenBucket('test-bucket', { limit: 100 }, mockLog);
68+
69+
assert.strictEqual(bucket.bufferSize, 100);
70+
assert.strictEqual(bucket.tokens, 100); // tokens = bufferSize
71+
});
72+
73+
it('should use custom refillThreshold from config.rateLimiting', () => {
74+
sandbox.restore();
75+
sandbox.stub(config, 'rateLimiting').value({
76+
nodes: 1,
77+
tokenBucketRefillThreshold: 30,
78+
bucket: {
79+
defaultConfig: {
80+
requestsPerSecond: { burstCapacity: 2 },
81+
},
82+
},
83+
});
84+
85+
const bucket = new tokenBucket.WorkerTokenBucket('test-bucket', { limit: 100 }, mockLog);
86+
87+
assert.strictEqual(bucket.refillThreshold, 30);
88+
});
89+
90+
it('should use both custom bufferSize and refillThreshold from config', () => {
91+
sandbox.restore();
92+
sandbox.stub(config, 'rateLimiting').value({
93+
nodes: 1,
94+
tokenBucketBufferSize: 75,
95+
tokenBucketRefillThreshold: 25,
96+
bucket: {
97+
defaultConfig: {
98+
requestsPerSecond: { burstCapacity: 2 },
99+
},
100+
},
101+
});
102+
103+
const bucket = new tokenBucket.WorkerTokenBucket('test-bucket', { limit: 100 }, mockLog);
104+
105+
assert.strictEqual(bucket.bufferSize, 75);
106+
assert.strictEqual(bucket.refillThreshold, 25);
107+
assert.strictEqual(bucket.tokens, 75); // tokens = bufferSize
108+
});
109+
110+
it('should fallback to defaults when rateLimiting is undefined', () => {
111+
sandbox.restore();
112+
sandbox.stub(config, 'rateLimiting').value(undefined);
113+
114+
const bucket = new tokenBucket.WorkerTokenBucket('test-bucket', { limit: 100 }, mockLog);
115+
116+
assert.strictEqual(bucket.bufferSize, 50);
117+
assert.strictEqual(bucket.refillThreshold, 20);
118+
assert.strictEqual(bucket.tokens, 50);
119+
});
54120
});
55121

56122
describe('tryConsume', () => {

0 commit comments

Comments
 (0)