|
| 1 | +const assert = require('assert'); |
| 2 | +const { RateLimitClient } = require('../../../../../lib/api/apiUtils/rateLimit/client'); |
| 3 | +const { config } = require('../../../../../lib/Config'); |
| 4 | +const { skipIfRateLimitDisabled } = require('./tooling'); |
| 5 | + |
| 6 | +const testBucket = 'rate-limit-test-bucket'; |
| 7 | + |
| 8 | +skipIfRateLimitDisabled('RateLimitClient', () => { |
| 9 | + let client; |
| 10 | + |
| 11 | + before(async () => { |
| 12 | + // Create a test client using the same config as the application |
| 13 | + client = new RateLimitClient(config.localCache); |
| 14 | + |
| 15 | + // Connect the client (since lazyConnect is true) |
| 16 | + await client.redis.connect(); |
| 17 | + }); |
| 18 | + |
| 19 | + after(async () => client.redis.quit().catch(() => {})); |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + const keys = await client.redis.keys('ratelimit:bucket:*'); |
| 23 | + if (keys.length > 0) { |
| 24 | + await client.redis.del(...keys); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + describe('isReady', () => { |
| 29 | + it('should return true when client is connected to redis', () => { |
| 30 | + assert.strictEqual(client.isReady(), true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return true when the client is waiting to connect or the first time', () => { |
| 34 | + const client = new RateLimitClient(config.localCache); |
| 35 | + assert.strictEqual(client.isReady(), true); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('grantTokens', () => { |
| 40 | + it('should grant requested tokens when quota is available', done => { |
| 41 | + const requested = 5; |
| 42 | + const interval = 100; // 100ms per request = 10 req/s |
| 43 | + const burstCapacity = 1000; // 1000ms burst capacity |
| 44 | + |
| 45 | + client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted) => { |
| 46 | + assert.ifError(err); |
| 47 | + assert.strictEqual(granted, requested); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should grant tokens multiple times within burst capacity', done => { |
| 53 | + const requested = 2; |
| 54 | + const interval = 100; // 100ms per request |
| 55 | + const burstCapacity = 1000; // 1000ms burst capacity |
| 56 | + |
| 57 | + // First request |
| 58 | + client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted1) => { |
| 59 | + assert.ifError(err); |
| 60 | + assert.strictEqual(granted1, requested); |
| 61 | + |
| 62 | + // Second request immediately after |
| 63 | + client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted2) => { |
| 64 | + assert.ifError(err); |
| 65 | + assert.strictEqual(granted2, requested); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should grant partial tokens when request exceeds available capacity', done => { |
| 72 | + const interval = 100; // 100ms per request |
| 73 | + const burstCapacity = 500; // 500ms burst capacity = max 5 tokens |
| 74 | + |
| 75 | + // Request more tokens than available in burst |
| 76 | + client.grantTokens(testBucket, 10, interval, burstCapacity, (err, granted) => { |
| 77 | + assert.ifError(err); |
| 78 | + // Should grant partial tokens (5 tokens max with 500ms burst) |
| 79 | + assert(granted > 0, 'Should grant at least some tokens'); |
| 80 | + assert(granted <= 5, 'Should not grant more than burst capacity allows'); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should deny tokens (return 0) when quota is exhausted', done => { |
| 86 | + const interval = 100; // 100ms per request |
| 87 | + const burstCapacity = 100; // 100ms burst capacity = max 1 token |
| 88 | + |
| 89 | + // First request consumes the burst capacity |
| 90 | + client.grantTokens(testBucket, 1, interval, burstCapacity, (err, granted1) => { |
| 91 | + assert.ifError(err); |
| 92 | + assert.strictEqual(granted1, 1); |
| 93 | + |
| 94 | + // Second request immediately after should be denied |
| 95 | + client.grantTokens(testBucket, 1, interval, burstCapacity, (err, granted2) => { |
| 96 | + assert.ifError(err); |
| 97 | + assert.strictEqual(granted2, 0, 'Should deny tokens when quota exhausted'); |
| 98 | + done(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments