From 3dc6a9d901453b80c2285b18802ddf0c119a1f60 Mon Sep 17 00:00:00 2001 From: Richard Fontein <32132657+rifont@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:26:20 +0000 Subject: [PATCH] test(api): Fix tolerance for upstash --- .../app/rate-limiting/guards/throttler.guard.e2e.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/api/src/app/rate-limiting/guards/throttler.guard.e2e.ts b/apps/api/src/app/rate-limiting/guards/throttler.guard.e2e.ts index ba2f5b70d70..1d7c8eb2c94 100644 --- a/apps/api/src/app/rate-limiting/guards/throttler.guard.e2e.ts +++ b/apps/api/src/app/rate-limiting/guards/throttler.guard.e2e.ts @@ -224,7 +224,7 @@ describe('API Rate Limiting', () => { expectedCost: mockSingleCost * 100, expectedReset: 5, expectedRetryAfter: 5, - expectedThrottledRequests: 51, // Upstash algorithm currently limits 1 more request than it should + expectedThrottledRequests: 50, }, ]; @@ -292,8 +292,12 @@ describe('API Rate Limiting', () => { ); }); - const expectedMinThrottled = expectedThrottledRequests * (1 - throttledResponseCountTollerance); - const expectedMaxThrottled = expectedThrottledRequests * (1 + throttledResponseCountTollerance); + const expectedMinThrottled = Math.floor( + expectedThrottledRequests * (1 - throttledResponseCountTollerance) + ); + const expectedMaxThrottled = Math.ceil( + expectedThrottledRequests * (1 + throttledResponseCountTollerance) + ); it(`should have between ${expectedMinThrottled} and ${expectedMaxThrottled} requests throttled`, async () => { expect(throttledResponseCount).to.be.greaterThanOrEqual(expectedMinThrottled); expect(throttledResponseCount).to.be.lessThanOrEqual(expectedMaxThrottled);