110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// newMiniRedis 启动 miniredis 并返回直连客户端。
|
|
func newMiniRedis(t *testing.T) redis.UniversalClient {
|
|
t.Helper()
|
|
mr := miniredis.RunT(t)
|
|
return redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
}
|
|
|
|
// newRateLimitRouter 构建带限流中间件与 /ping 路由的测试引擎。
|
|
func newRateLimitRouter(c redis.UniversalClient, cfg RateLimitConfig) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(RateLimit(c, cfg))
|
|
r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") })
|
|
return r
|
|
}
|
|
|
|
// TestRateLimit_RejectWhenExceeded 验收项:超过桶容量后返回 429。
|
|
func TestRateLimit_RejectWhenExceeded(t *testing.T) {
|
|
c := newMiniRedis(t)
|
|
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 3, Rate: 1}) // 容量 3,1 个/秒
|
|
|
|
code200, code429 := 0, 0
|
|
for i := 0; i < 6; i++ {
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
|
switch w.Code {
|
|
case 200:
|
|
code200++
|
|
case 429:
|
|
code429++
|
|
default:
|
|
t.Fatalf("意外状态码 %d", w.Code)
|
|
}
|
|
}
|
|
if code200 != 3 || code429 != 3 {
|
|
t.Errorf("容量3发6次: 200x%d + 429x%d, 期望 3+3", code200, code429)
|
|
}
|
|
}
|
|
|
|
// TestRateLimit_RefillOverTime 验证令牌按速率恢复。
|
|
func TestRateLimit_RefillOverTime(t *testing.T) {
|
|
c := newMiniRedis(t)
|
|
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 2, Rate: 10}) // 每秒回 10 个
|
|
|
|
for i := 0; i < 2; i++ { // 耗尽
|
|
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/ping", nil))
|
|
}
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
|
if w.Code != 429 {
|
|
t.Fatalf("耗尽后应为 429, 实际 %d", w.Code)
|
|
}
|
|
time.Sleep(150 * time.Millisecond) // 回填 ~1.5 个
|
|
w = httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("回填后应放行, 实际 %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestRateLimit_PerKeyIsolation 验证按 KeyFunc 维度独立限流。
|
|
func TestRateLimit_PerKeyIsolation(t *testing.T) {
|
|
c := newMiniRedis(t)
|
|
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 1, Rate: 1, KeyFunc: func(g *gin.Context) string {
|
|
return g.Query("uid")
|
|
}})
|
|
|
|
hit := func(uid string) int {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/ping?uid="+uid, nil)
|
|
r.ServeHTTP(w, req)
|
|
return w.Code
|
|
}
|
|
if hit("a") != 200 || hit("a") != 429 {
|
|
t.Error("同 uid 第二次应 429")
|
|
}
|
|
if hit("b") != 200 {
|
|
t.Error("不同 uid 应独立限流(200)")
|
|
}
|
|
}
|
|
|
|
// TestRateLimit_BackendDownFailsOpen 验证 Redis 故障时放行(可用性优先)。
|
|
func TestRateLimit_BackendDownFailsOpen(t *testing.T) {
|
|
mr := miniredis.RunT(t)
|
|
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
mr.Close() // 立即关闭后端
|
|
|
|
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 1, Rate: 1})
|
|
for i := 0; i < 3; i++ {
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("后端故障应放行, 实际 %d", w.Code)
|
|
}
|
|
}
|
|
}
|