Skip to content

Conversation

@shalarewicz
Copy link
Collaborator

Initial jest and tsconfig.
Closes #4 with token bucket testing
Closes #9 with a class spec for RateLimiters

@shalarewicz shalarewicz linked an issue May 17, 2022 that may be closed by this pull request
Copy link
Contributor

@evanmcneely evanmcneely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments moved to below

Copy link
Collaborator

@jondeweydev jondeweydev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted comments, mainly suggestions.

@shalarewicz shalarewicz self-assigned this May 19, 2022
@shalarewicz
Copy link
Collaborator Author

shalarewicz commented May 19, 2022

Summary

  • Configures typescript with corresponding changes to eslint configuration.
  • Configures jest for testing with ts-jest
  • Adds unit tests for the Token Bucket Rate limiting algorithm using jest and redis-mock.
  • Establishes framework for future RateLimiters and a class specification for TokenBucket.

Type of Change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Issues

Evidence

  • All tests except test for negative values being provided to constructor fail as expected
    image

const tokenCountFull = await getBucketFromClient(client, user1);
expect(tokenCountFull).toBe(CAPACITY - withdraw5);

// Bucket partially full but enough time has elapsed to fill the bucket since the last request and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break these out into separate tests instead of having one test for all of these variations on situations with allowed requests

timestamp + 1000 * (CAPACITY - initial),
initial + partialWithdraw
)
).toBe(CAPACITY - (initial + partialWithdraw));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These case reads to me like:
line 58 - set token bucket to have 6 tokens
line 60 - process request withdrawing 7 tokens (6+1)
line 65 - expect the 3 tokens in the bucket (10 - (6 + 1)) when I think there will be -1 tokens in the bucket

I'm having a hard time following all this redis stuff so I might be wrong. Need more time.

Copy link
Contributor

@evanmcneely evanmcneely May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be processRequest().tokens to access the tokens property on the returned object from processRequest method. This goes for a lot of the tests in this area. Maybe also assert that processRequest().sucess is true/false as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 62 is saying that enough time has elapsed to refill the bucket since the last request (4000 ms). So when we withdraw 7 tokens there should be 3 remaining. Was trying to get after a case where the bucket fills enough between requests.

@shalarewicz shalarewicz requested a review from feiw101 May 21, 2022 16:40
@shalarewicz shalarewicz requested a review from evanmcneely May 21, 2022 17:24
Copy link
Contributor

@evanmcneely evanmcneely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. I made one small followup comment on the optional parameters in typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create typescript config file - graph-beaver Write RateLimiter class spec Implement testing for Token Bucket algorithm

5 participants