Skip to content

Commit

Permalink
add test for the limit func
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Schwartz committed Apr 28, 2022
1 parent 9fd3f3f commit 5ec084a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gateway/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,27 @@ func TestGatewayVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, api.FullAPIVersion1, v.APIVersion)
}

func TestGatewayLimitTokensAvailable(t *testing.T) {
ctx := context.Background()
mock := &mockGatewayDepsAPI{}
tokens := 3
a := NewNode(mock, DefaultLookbackCap, DefaultStateWaitLookbackLimit, int64(tokens), time.Minute)
require.NoError(t, a.limit(ctx, tokens), "requests should not be limited when there are enough tokens availble")
}

func TestGatewayLimitTokensNotAvailable(t *testing.T) {
ctx := context.Background()
mock := &mockGatewayDepsAPI{}
tokens := 3
a := NewNode(mock, DefaultLookbackCap, DefaultStateWaitLookbackLimit, int64(1), time.Millisecond)
var err error
// try to be rate limited
for i := 0; i <= 1000; i++ {
err = a.limit(ctx, tokens)
if err != nil {
break
}
}
require.Error(t, err, "requiests should be rate limited when they hit limits")
}

0 comments on commit 5ec084a

Please sign in to comment.