Skip to content

Commit c8d3eac

Browse files
committed
Corrected tests for sliding window log
1 parent 2ae92de commit c8d3eac

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

test/rateLimiters/slidingWindowLog.test.ts

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
119119
]);
120120

121121
// Check the received response
122-
const expectedResponse: RateLimiterResponse = { tokens: 1, success: true };
122+
const expectedResponse: RateLimiterResponse = { tokens: 10, success: true };
123123
expect(user1Response).toEqual(expectedResponse);
124124
expect(user2Response).toEqual(expectedResponse);
125125
expect(user3Response).toEqual(expectedResponse);
@@ -147,7 +147,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
147147
// Check the received response
148148
expect(user1Response).toEqual({ tokens: CAPACITY - user1Tokens, success: true });
149149
expect(user2Response).toEqual({ tokens: CAPACITY - user2Tokens, success: true });
150-
expect(user3Response).toEqual({ tokens: CAPACITY - user2Tokens, success: true });
150+
expect(user3Response).toEqual({ tokens: CAPACITY - user3Tokens, success: true });
151151

152152
// Check that redis is correctly updated.
153153
[user1Log, user2Log, user3Log] = await Promise.all([
@@ -157,7 +157,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
157157
]);
158158
expect(user1Log).toEqual([{ timestamp, tokens: user1Tokens }]);
159159
expect(user2Log).toEqual([{ timestamp, tokens: user2Tokens }]);
160-
expect(user3Log).toEqual([{ timestamp, tokens: user2Tokens }]);
160+
expect(user3Log).toEqual([{ timestamp, tokens: user3Tokens }]);
161161
});
162162
test('and the request complexity is equal to the capacity', async () => {
163163
const user1Tokens = CAPACITY;
@@ -182,9 +182,9 @@ describe('SlidingWindowLog Rate Limiter', () => {
182182
getLogFromClient(client, user2),
183183
getLogFromClient(client, user3),
184184
]);
185-
expect(user1Log).toEqual([{ timestamp, tokens: 0 }]);
186-
expect(user2Log).toEqual([{ timestamp, tokens: 0 }]);
187-
expect(user3Log).toEqual([{ timestamp, tokens: 0 }]);
185+
expect(user1Log).toEqual([{ timestamp, tokens: user1Tokens }]);
186+
expect(user2Log).toEqual([{ timestamp, tokens: user2Tokens }]);
187+
expect(user3Log).toEqual([{ timestamp, tokens: user3Tokens }]);
188188
});
189189
test('and the request complexity is greater than the capacity', async () => {
190190
const user1Tokens = CAPACITY + 1;
@@ -218,11 +218,11 @@ describe('SlidingWindowLog Rate Limiter', () => {
218218
describe('the redis log contains active requests in the window when...', () => {
219219
test('the sum of requests is equal to capacity', async () => {
220220
// add 2 requests to the redis store 3, 7
221-
const intialLog = [
221+
const initialLog = [
222222
{ timestamp, tokens: 3 },
223223
{ timestamp: timestamp + 100, tokens: 7 },
224224
];
225-
await setLogInClient(client, user1, intialLog);
225+
await setLogInClient(client, user1, initialLog);
226226

227227
timestamp += 100;
228228
const response: RateLimiterResponse = await limiter.processRequest(
@@ -231,22 +231,27 @@ describe('SlidingWindowLog Rate Limiter', () => {
231231
1
232232
);
233233

234-
expect(response.tokens).toBe(1);
234+
expect(response.tokens).toBe(0);
235235
expect(response.success).toBe(false);
236236

237237
const redisLog = await getLogFromClient(client, user1);
238-
expect(redisLog).toEqual(intialLog);
238+
expect(redisLog).toEqual(initialLog);
239239
});
240240
describe('the sum of requests is less than capacity and..', () => {
241-
const intialLog = [
242-
{ timestamp, tokens: 3 },
243-
{ timestamp: timestamp + 100, tokens: 4 },
244-
];
245-
const initalTokenSum = 7;
241+
let initialLog: RedisLog;
242+
let initialTokenSum = 0;
243+
244+
beforeAll(() => {
245+
initialLog = [
246+
{ timestamp, tokens: 3 },
247+
{ timestamp: timestamp + 100, tokens: 4 },
248+
];
249+
initialTokenSum = 7;
250+
});
246251

247252
beforeEach(async () => {
248-
await setLogInClient(client, user1, intialLog);
249-
timestamp += 100;
253+
await setLogInClient(client, user1, initialLog);
254+
timestamp += 200;
250255
});
251256
test('the current request complexity is small enough to be allowed', async () => {
252257
const tokens = 2;
@@ -256,12 +261,12 @@ describe('SlidingWindowLog Rate Limiter', () => {
256261
tokens
257262
);
258263

259-
expect(response.tokens).toBe(CAPACITY - (initalTokenSum + tokens));
264+
expect(response.tokens).toBe(CAPACITY - (initialTokenSum + tokens));
260265
expect(response.success).toBe(true);
261266

262267
const redisLog = await getLogFromClient(client, user1);
263268

264-
expect(redisLog).toEqual([...intialLog, { timestamp, tokens }]);
269+
expect(redisLog).toEqual([...initialLog, { timestamp, tokens }]);
265270
});
266271

267272
test('the current request has complexity = remaining capacity', async () => {
@@ -272,12 +277,12 @@ describe('SlidingWindowLog Rate Limiter', () => {
272277
tokens
273278
);
274279

275-
expect(response.tokens).toBe(CAPACITY - (initalTokenSum + tokens));
280+
expect(response.tokens).toBe(CAPACITY - (initialTokenSum + tokens));
276281
expect(response.success).toBe(true);
277282

278283
const redisLog = await getLogFromClient(client, user1);
279284

280-
expect(redisLog).toEqual([...intialLog, { timestamp, tokens }]);
285+
expect(redisLog).toEqual([...initialLog, { timestamp, tokens }]);
281286
});
282287
test('the current request complexity to big to be allowed', async () => {
283288
const tokens = 4;
@@ -287,12 +292,12 @@ describe('SlidingWindowLog Rate Limiter', () => {
287292
tokens
288293
);
289294

290-
expect(response.tokens).toBe(initalTokenSum);
295+
expect(response.tokens).toBe(CAPACITY - initialTokenSum);
291296
expect(response.success).toBe(false);
292297

293298
const redisLog = await getLogFromClient(client, user1);
294299

295-
expect(redisLog).toEqual(intialLog);
300+
expect(redisLog).toEqual(initialLog);
296301
});
297302
test('the current request complexity = 0', async () => {
298303
const tokens = 0;
@@ -302,29 +307,35 @@ describe('SlidingWindowLog Rate Limiter', () => {
302307
tokens
303308
);
304309

305-
expect(response.tokens).toBe(initalTokenSum);
310+
expect(response.tokens).toBe(CAPACITY - initialTokenSum);
306311
expect(response.success).toBe(true);
307312

308313
const redisLog = await getLogFromClient(client, user1);
309314

310-
expect(redisLog).toEqual(intialLog);
315+
expect(redisLog).toEqual(initialLog);
311316
});
312317
});
313318
});
314319

315320
describe('the redis log contains active and expired requests when...', () => {
316321
// Current request is sent at timestamp + 1.5 * WINDOW_SIZE (1500)
317-
const intialLog = [
318-
{ timestamp, tokens: 1 }, // expired
319-
{ timestamp: timestamp + 100, tokens: 2 }, // expired
320-
{ timestamp: timestamp + 600, tokens: 3 }, // active
321-
{ timestamp: timestamp + 700, tokens: 4 }, // active
322-
];
323-
const activeLog = intialLog.slice(2);
324-
const activeTokenSum = 7;
322+
let initialLog: RedisLog;
323+
let activeLog: RedisLog;
324+
let activeTokenSum = 0;
325+
326+
beforeAll(() => {
327+
initialLog = [
328+
{ timestamp, tokens: 1 }, // expired
329+
{ timestamp: timestamp + 100, tokens: 2 }, // expired
330+
{ timestamp: timestamp + 600, tokens: 3 }, // active
331+
{ timestamp: timestamp + 700, tokens: 4 }, // active
332+
];
333+
activeLog = initialLog.slice(2);
334+
activeTokenSum = 7;
335+
});
325336

326337
beforeEach(async () => {
327-
await setLogInClient(client, user1, intialLog);
338+
await setLogInClient(client, user1, initialLog);
328339
timestamp += 1500;
329340
});
330341

@@ -340,7 +351,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
340351

341352
const redisLog = await getLogFromClient(client, user1);
342353

343-
expect(redisLog).toEqual([activeLog]);
354+
expect(redisLog).toEqual(activeLog);
344355
});
345356
test('the current request has complexity < capacity', async () => {
346357
const tokens = 2;
@@ -381,18 +392,18 @@ describe('SlidingWindowLog Rate Limiter', () => {
381392
);
382393

383394
expect(response.tokens).toBe(CAPACITY - activeTokenSum);
384-
expect(response.success).toBe(true);
395+
expect(response.success).toBe(false);
385396

386397
const redisLog = await getLogFromClient(client, user1);
387398

388-
expect(redisLog).toEqual([activeLog]);
399+
expect(redisLog).toEqual(activeLog);
389400
});
390401
});
391402

392403
test('the log contains a request on a window boundary', async () => {
393-
const intialLog = [{ timestamp, tokens: CAPACITY }];
404+
const initialLog = [{ timestamp, tokens: CAPACITY }];
394405

395-
await setLogInClient(client, user1, intialLog);
406+
await setLogInClient(client, user1, initialLog);
396407

397408
// Should not be allowed to perform any requests inside the indow
398409
const inWindowRequest = await limiter.processRequest(
@@ -406,7 +417,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
406417
timestamp + WINDOW_SIZE,
407418
1
408419
);
409-
expect(startNewWindowRequest.success).toBe(false);
420+
expect(startNewWindowRequest.success).toBe(true);
410421
});
411422
});
412423

@@ -505,7 +516,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
505516
customWindowSuccess = await customCapacitylimiter
506517
.processRequest(user1, timestamp + 100, customCapacity)
507518
.then((res) => res.success);
508-
expect(customSizeSuccess).toBe(true);
519+
expect(customWindowSuccess).toBe(true);
509520
});
510521
});
511522
});

0 commit comments

Comments
 (0)