This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPeriodLimit_Take(t *testing.T) {
|
||||
testPeriodLimit(t)
|
||||
}
|
||||
|
||||
func TestPeriodLimit_TakeWithAlign(t *testing.T) {
|
||||
testPeriodLimit(t, Align())
|
||||
}
|
||||
|
||||
func TestPeriodLimit_RedisUnavailable(t *testing.T) {
|
||||
//t.Skipf("skip this test because it's not stable")
|
||||
const (
|
||||
seconds = 1
|
||||
quota = 5
|
||||
)
|
||||
rds := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:12345",
|
||||
})
|
||||
|
||||
l := NewPeriodLimit(seconds, quota, rds, "periodlimit:")
|
||||
val, err := l.Take("first")
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, 0, val)
|
||||
}
|
||||
|
||||
func testPeriodLimit(t *testing.T, opts ...PeriodOption) {
|
||||
store, _ := CreateRedisWithClean(t)
|
||||
const (
|
||||
seconds = 1
|
||||
total = 100
|
||||
quota = 5
|
||||
)
|
||||
l := NewPeriodLimit(seconds, quota, store, "periodlimit", opts...)
|
||||
var allowed, hitQuota, overQuota int
|
||||
for i := 0; i < total; i++ {
|
||||
val, err := l.Take("first")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
switch val {
|
||||
case Allowed:
|
||||
allowed++
|
||||
case HitQuota:
|
||||
hitQuota++
|
||||
case OverQuota:
|
||||
overQuota++
|
||||
default:
|
||||
t.Error("unknown status")
|
||||
}
|
||||
}
|
||||
assert.Equal(t, quota-1, allowed)
|
||||
assert.Equal(t, 1, hitQuota)
|
||||
assert.Equal(t, total-quota, overQuota)
|
||||
}
|
||||
|
||||
func TestQuotaFull(t *testing.T) {
|
||||
rds, _ := CreateRedisWithClean(t)
|
||||
l := NewPeriodLimit(1, 1, rds, "periodlimit")
|
||||
val, err := l.Take("first")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, HitQuota, val)
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTokenLimit_WithCtx(t *testing.T) {
|
||||
const (
|
||||
total = 100
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
store, _ := CreateRedisWithClean(t)
|
||||
l := NewTokenLimiter(rate, burst, store, "tokenlimit")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.True(t, ok)
|
||||
|
||||
cancel()
|
||||
for i := 0; i < total; i++ {
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.False(t, ok)
|
||||
assert.False(t, l.monitorStarted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenLimit_Take(t *testing.T) {
|
||||
store, _ := CreateRedisWithClean(t)
|
||||
|
||||
const (
|
||||
total = 100
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
l := NewTokenLimiter(rate, burst, store, "tokenlimit")
|
||||
var allowed int
|
||||
for i := 0; i < total; i++ {
|
||||
time.Sleep(time.Second / time.Duration(total))
|
||||
if l.Allow() {
|
||||
allowed++
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, allowed >= burst+rate)
|
||||
}
|
||||
|
||||
func TestTokenLimit_TakeBurst(t *testing.T) {
|
||||
store, _ := CreateRedisWithClean(t)
|
||||
|
||||
const (
|
||||
total = 100
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
l := NewTokenLimiter(rate, burst, store, "tokenlimit")
|
||||
var allowed int
|
||||
for i := 0; i < total; i++ {
|
||||
if l.Allow() {
|
||||
allowed++
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, allowed >= burst)
|
||||
}
|
||||
|
||||
// CreateRedisWithClean returns an in process redis.Redis and a clean function.
|
||||
func CreateRedisWithClean(t *testing.T) (r *redis.Client, clean func()) {
|
||||
mr := miniredis.RunT(t)
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: mr.Addr(),
|
||||
}), mr.Close
|
||||
}
|
||||
Reference in New Issue
Block a user