Skip to content

Commit

Permalink
Fixing rate limiting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Dec 26, 2023
1 parent 9a3db0e commit 437267c
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions packages/requester-utils/test/unit/RequesterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as RateLimiter from 'rate-limiter-flexible';

import { RateLimiterMemory } from 'rate-limiter-flexible';
import {
RequestOptions,
ResourceOptions,
Expand All @@ -11,6 +10,8 @@ import {
presetResourceArguments,
} from '../../src/RequesterUtils';

jest.mock('rate-limiter-flexible');

const methods = ['get', 'put', 'patch', 'delete', 'post'];

describe('defaultOptionsHandler', () => {
Expand Down Expand Up @@ -194,8 +195,6 @@ describe('createInstance', () => {
});

it('should pass the rate limiters to the requestHandler function', async () => {
const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterQueue');

const testEndpoint = 'test endpoint';
const requester = createRequesterFn(
optionsHandler,
Expand All @@ -213,9 +212,6 @@ describe('createInstance', () => {

await requester.get(testEndpoint, {});

expect(rateLimitSpy).toHaveBeenCalledWith(10, { timeUnit: 60000 });
expect(rateLimitSpy).toHaveBeenCalledWith(40, { timeUnit: 60000 });

expect(requestHandler).toHaveBeenCalledWith(testEndpoint, {
rateLimiters: {
'*': expect.toBeFunction(),
Expand All @@ -230,8 +226,6 @@ describe('createInstance', () => {

describe('createRateLimiters', () => {
it('should create rate limiter functions when configured', () => {
const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterQueue');

const limiters = createRateLimiters({
'*': 40,
'projects/*/test': {
Expand All @@ -240,8 +234,8 @@ describe('createRateLimiters', () => {
},
});

expect(rateLimitSpy).toHaveBeenCalledWith(10, { timeUnit: 60000 });
expect(rateLimitSpy).toHaveBeenCalledWith(40, { timeUnit: 60000 });
expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 10, duration: 60 });
expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 40, duration: 60 });

expect(limiters).toStrictEqual({
'*': expect.toBeFunction(),
Expand Down Expand Up @@ -339,11 +333,9 @@ describe('getMatchingRateLimiter', () => {
});

it('should default the rateLimiters to an empty object if not passed and return the default rate of 3000 rpm', () => {
const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterMemory');

getMatchingRateLimiter('endpoint');

expect(rateLimitSpy).toHaveBeenCalledWith({ points: 3000, duration: 60 });
expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 3000, duration: 60 });
});

it('should return the most specific rate limit', async () => {
Expand All @@ -359,11 +351,9 @@ describe('getMatchingRateLimiter', () => {
});

it('should return a default rate limit of 3000 rpm if nothing matches', () => {
const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterMemory');

getMatchingRateLimiter('endpoint', { someurl: jest.fn() });

expect(rateLimitSpy).toHaveBeenCalledWith({ points: 3000, duration: 60 });
expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 3000, duration: 60 });
});

it('should handle expanded rate limit options with a particular method and limit', async () => {
Expand Down

0 comments on commit 437267c

Please sign in to comment.