Skip to content

Commit

Permalink
fix: add test coverage for rate limits with 0 permitted events (#1834)
Browse files Browse the repository at this point in the history
Verify that rate limits such as "0/1h", "0/24h" work as intended. This
is a tests only change to ensure this behavior is preserved in the
future.
  • Loading branch information
cstockton authored Nov 11, 2024
1 parent 0a589d0 commit 7c3cf26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
26 changes: 26 additions & 0 deletions internal/api/ratelimits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ func TestNewRateLimiter(t *testing.T) {
{true, now.Add(time.Hour * 25), 0},
},
},
{
cfg: conf.Rate{Events: 0, OverTime: time.Hour},
now: now,
evts: []event{
{false, now.Add(-time.Hour), 0},
{false, now, 0},
{false, now.Add(time.Minute), 0},
{false, now.Add(time.Hour), 0},
{false, now.Add(time.Hour), 12},
{false, now.Add(time.Hour * 24), 0},
{false, now.Add(time.Hour * 24 * 2), 0},
},
},
{
cfg: conf.Rate{Events: 0, OverTime: time.Hour * 24},
now: now,
evts: []event{
{false, now.Add(-time.Hour), 0},
{false, now, 0},
{false, now.Add(time.Minute), 0},
{false, now.Add(time.Hour), 0},
{false, now.Add(time.Hour), 12},
{false, now.Add(time.Hour * 24), 0},
{false, now.Add(time.Hour * 24 * 2), 0},
},
},
}
for _, tc := range cases {
rl := newRateLimiter(tc.cfg)
Expand Down
5 changes: 4 additions & 1 deletion internal/conf/rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestRateDecode(t *testing.T) {
{str: "100/24h",
eps: 0.0011574074074074073,
exp: Rate{Events: 100, OverTime: time.Hour * 24}},

{str: "", eps: 1, exp: Rate{},
err: `rate: value does not match`},
{str: "1h", eps: 1, exp: Rate{},
Expand All @@ -35,6 +34,10 @@ func TestRateDecode(t *testing.T) {
err: `rate: over-time part of rate value`},
{str: "100/1", eps: 1, exp: Rate{},
err: `rate: over-time part of rate value`},

// zero events
{str: "0/1h", eps: 0.0, exp: Rate{Events: 0, OverTime: time.Hour}},
{str: "0/24h", eps: 0.0, exp: Rate{Events: 0, OverTime: time.Hour * 24}},
}
for idx, tc := range cases {
var r Rate
Expand Down

0 comments on commit 7c3cf26

Please sign in to comment.