This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: TungHoang <[email protected]>
- Loading branch information
1 parent
80214ce
commit 064a129
Showing
3 changed files
with
122 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,95 @@ | ||
package plugins | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
auth "github.com/flyteorg/flyteadmin/auth" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewRateLimiter(t *testing.T) { | ||
rl := NewRateLimiter(1, 1, time.Second) | ||
assert.NotNil(t, rl) | ||
rlStore := newRateLimitStore(1, 1, time.Second) | ||
assert.NotNil(t, rlStore) | ||
} | ||
|
||
func TestLimiter_Allow(t *testing.T) { | ||
rl := NewRateLimiter(1, 1, time.Second) | ||
assert.NoError(t, rl.Allow("hello")) | ||
// assert error type is RateLimitError | ||
assert.Error(t, rl.Allow("hello")) | ||
func TestLimiterAllow(t *testing.T) { | ||
rlStore := newRateLimitStore(1, 1, time.Second) | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
assert.Error(t, rlStore.Allow("hello")) | ||
time.Sleep(time.Second) | ||
assert.NoError(t, rl.Allow("hello")) | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
} | ||
|
||
func TestLimiter_AllowBurst(t *testing.T) { | ||
rl := NewRateLimiter(1, 2, time.Second) | ||
assert.NoError(t, rl.Allow("hello")) | ||
assert.NoError(t, rl.Allow("hello")) | ||
assert.Error(t, rl.Allow("hello")) | ||
assert.NoError(t, rl.Allow("world")) | ||
func TestLimiterAllowBurst(t *testing.T) { | ||
rlStore := newRateLimitStore(1, 2, time.Second) | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
assert.Error(t, rlStore.Allow("hello")) | ||
assert.NoError(t, rlStore.Allow("world")) | ||
} | ||
|
||
func TestLimiter_Clean(t *testing.T) { | ||
rl := NewRateLimiter(1, 1, time.Second) | ||
assert.NoError(t, rl.Allow("hello")) | ||
assert.Error(t, rl.Allow("hello")) | ||
func TestLimiterClean(t *testing.T) { | ||
rlStore := newRateLimitStore(1, 1, time.Second) | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
assert.Error(t, rlStore.Allow("hello")) | ||
time.Sleep(time.Second) | ||
rl.clean() | ||
assert.NoError(t, rl.Allow("hello")) | ||
rlStore.clean() | ||
assert.NoError(t, rlStore.Allow("hello")) | ||
} | ||
|
||
func TestLimiter_AllowOnMultipleRequests(t *testing.T) { | ||
rl := NewRateLimiter(1, 1, time.Second) | ||
assert.NoError(t, rl.Allow("a")) | ||
assert.NoError(t, rl.Allow("b")) | ||
assert.NoError(t, rl.Allow("c")) | ||
assert.Error(t, rl.Allow("a")) | ||
assert.Error(t, rl.Allow("b")) | ||
func TestLimiterAllowOnMultipleRequests(t *testing.T) { | ||
rlStore := newRateLimitStore(1, 1, time.Second) | ||
assert.NoError(t, rlStore.Allow("a")) | ||
assert.NoError(t, rlStore.Allow("b")) | ||
assert.NoError(t, rlStore.Allow("c")) | ||
assert.Error(t, rlStore.Allow("a")) | ||
assert.Error(t, rlStore.Allow("b")) | ||
|
||
time.Sleep(time.Second) | ||
|
||
assert.NoError(t, rl.Allow("a")) | ||
assert.Error(t, rl.Allow("a")) | ||
assert.NoError(t, rl.Allow("b")) | ||
assert.Error(t, rl.Allow("b")) | ||
assert.NoError(t, rl.Allow("c")) | ||
assert.NoError(t, rlStore.Allow("a")) | ||
assert.Error(t, rlStore.Allow("a")) | ||
assert.NoError(t, rlStore.Allow("b")) | ||
assert.Error(t, rlStore.Allow("b")) | ||
assert.NoError(t, rlStore.Allow("c")) | ||
} | ||
|
||
func TestRateLimiterLimitPass(t *testing.T) { | ||
rateLimit := NewRateLimiter(1, 1, time.Second) | ||
assert.NotNil(t, rateLimit) | ||
|
||
identityCtx, err := auth.NewIdentityContext("audience", "user1", "app1", time.Now(), nil, nil, nil) | ||
assert.NoError(t, err) | ||
|
||
ctx := context.WithValue(context.TODO(), auth.ContextKeyIdentityContext, identityCtx) | ||
err = rateLimit.Limit(ctx) | ||
assert.NoError(t, err) | ||
|
||
} | ||
|
||
func TestRateLimiterLimitStop(t *testing.T) { | ||
rateLimit := NewRateLimiter(1, 1, time.Second) | ||
assert.NotNil(t, rateLimit) | ||
|
||
identityCtx, err := auth.NewIdentityContext("audience", "user1", "app1", time.Now(), nil, nil, nil) | ||
assert.NoError(t, err) | ||
ctx := context.WithValue(context.TODO(), auth.ContextKeyIdentityContext, identityCtx) | ||
err = rateLimit.Limit(ctx) | ||
assert.NoError(t, err) | ||
|
||
err = rateLimit.Limit(ctx) | ||
assert.Error(t, err) | ||
|
||
} | ||
|
||
func TestRateLimiterLimitWithoutUserIdentity(t *testing.T) { | ||
rateLimit := NewRateLimiter(1, 1, time.Second) | ||
assert.NotNil(t, rateLimit) | ||
|
||
ctx := context.TODO() | ||
|
||
err := rateLimit.Limit(ctx) | ||
assert.Error(t, err) | ||
} |