|
| 1 | +import { strict as assert } from 'node:assert'; |
| 2 | +import testUtils, { GLOBAL } from '../test-utils'; |
| 3 | +import GCRA from './GCRA'; |
| 4 | +import { parseArgs } from './generic-transformers'; |
| 5 | + |
| 6 | +describe('GCRA', () => { |
| 7 | + testUtils.isVersionGreaterThanHook([8, 8]); |
| 8 | + |
| 9 | + describe('transformArguments', () => { |
| 10 | + it('with required arguments', () => { |
| 11 | + assert.deepEqual( |
| 12 | + parseArgs(GCRA, 'key', 15, 30, 60), |
| 13 | + ['GCRA', 'key', '15', '30', '60'] |
| 14 | + ); |
| 15 | + }); |
| 16 | + |
| 17 | + it('with a fractional period', () => { |
| 18 | + assert.deepEqual( |
| 19 | + parseArgs(GCRA, 'key', 15, 30, 0.5), |
| 20 | + ['GCRA', 'key', '15', '30', '0.5'] |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it('with TOKENS', () => { |
| 25 | + assert.deepEqual( |
| 26 | + parseArgs(GCRA, 'key', 15, 30, 60, 3), |
| 27 | + ['GCRA', 'key', '15', '30', '60', 'TOKENS', '3'] |
| 28 | + ); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + function assertReplyShape(reply: { |
| 33 | + limited: boolean; |
| 34 | + maxRequests: number; |
| 35 | + availableRequests: number; |
| 36 | + retryAfter: number; |
| 37 | + fullBurstAfter: number; |
| 38 | + }, expectedMaxRequests: number) { |
| 39 | + assert.ok(reply.limited === true || reply.limited === false); |
| 40 | + assert.equal(reply.maxRequests, expectedMaxRequests); |
| 41 | + assert.ok(reply.availableRequests >= 0); |
| 42 | + assert.ok(reply.retryAfter >= -1); |
| 43 | + assert.ok(reply.fullBurstAfter >= 0); |
| 44 | + } |
| 45 | + |
| 46 | + testUtils.testWithClient('gcra allows one request then limits the next with zero burst', async client => { |
| 47 | + const first = await client.gcra('gcra:single-token', 0, 1, 1); |
| 48 | + const second = await client.gcra('gcra:single-token', 0, 1, 1); |
| 49 | + |
| 50 | + assertReplyShape(first, 1); |
| 51 | + assertReplyShape(second, 1); |
| 52 | + assert.notEqual(first.limited, second.limited); |
| 53 | + |
| 54 | + assert.ok(first.retryAfter === -1 || second.retryAfter === -1); |
| 55 | + assert.ok(first.retryAfter >= 0 || second.retryAfter >= 0); |
| 56 | + }, GLOBAL.SERVERS.OPEN); |
| 57 | + |
| 58 | + testUtils.testWithClient('gcra supports weighted requests using TOKENS', async client => { |
| 59 | + const key = 'gcra:weighted'; |
| 60 | + |
| 61 | + const first = await client.gcra(key, 10, 10, 1, 10); |
| 62 | + const second = await client.gcra(key, 10, 10, 1, 10); |
| 63 | + |
| 64 | + assertReplyShape(first, 11); |
| 65 | + assertReplyShape(second, 11); |
| 66 | + assert.notEqual(first.limited, second.limited); |
| 67 | + }, GLOBAL.SERVERS.OPEN); |
| 68 | + |
| 69 | + testUtils.testWithClient('gcra returns the same reply shape on RESP3', async client => { |
| 70 | + const first = await client.gcra('gcra:resp3', 0, 1, 1); |
| 71 | + const second = await client.gcra('gcra:resp3', 0, 1, 1); |
| 72 | + |
| 73 | + assertReplyShape(first, 1); |
| 74 | + assertReplyShape(second, 1); |
| 75 | + assert.notEqual(first.limited, second.limited); |
| 76 | + }, GLOBAL.SERVERS.OPEN_RESP_3); |
| 77 | +}); |
0 commit comments