rate_limit/redis_test.go

38 lines
895 B
Go
Raw Normal View History

2024-06-20 18:32:16 +08:00
// Package rate_limit ...
//
// Description : rate_limit ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-06-20 18:01
package rate_limit
import (
"context"
"fmt"
"git.zhangdeman.cn/zhangdeman/rate_limit/define"
wrapRedis "git.zhangdeman.cn/zhangdeman/redis"
wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define"
"testing"
2024-06-20 18:43:27 +08:00
"time"
2024-06-20 18:32:16 +08:00
)
func TestRedis_AllowN(t *testing.T) {
2024-06-20 18:43:27 +08:00
_ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{
2024-06-20 18:48:54 +08:00
Addr: "127.0.0.1:6379",
Password: "123456",
DB: 6,
2024-06-20 18:32:16 +08:00
})
RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit"))
2024-06-20 18:48:54 +08:00
for i := 0; i < 1000; i++ {
2024-06-20 18:43:27 +08:00
allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{
Key: "deman",
2024-06-20 18:48:54 +08:00
Total: 60,
2024-06-20 18:43:27 +08:00
Rate: 10,
TimeInterval: 60,
}, 8)
fmt.Println(allow, err)
time.Sleep(time.Second)
}
2024-06-20 18:32:16 +08:00
}