Files
xlgo-core/middleware/ratelimit_phase5_internal_test.go
杭州明婳科技 96ce883d41 feat: cron/storage/cache/ratelimit 实例化 + trace 纳入 App 生命周期 (v1.4.0)
完成 instance+facade 一致性收尾,闭合三个原始问题:
- cron/storage/cache/ratelimit 实例化(App 持 per-App 实例 + SwapDefault* 提升全局默认
  + 包级 facade 代理,照 database.Manager/jwt.Manager 模式),消除单进程多 App 互相污染。
- trace 纳入 App 生命周期(WithTrace + doInit trace.Init + doShutdown trace.Close +
  config.TraceConfig + 自动装 Middleware)。不做实例隔离(OTel 进程全局)。
- 删除 WithoutWire 空函数技术债。

附带修复:
- cache/ratelimit redis 注入(jwt 模型:注入优先、database.GetRedis() 兜底)。
- commitReplacedResources 不再关闭 previousDB/previousRedis/loggerSnapshot
  (多 App 下 previous 可能是另一 App 的活跃资源,关闭致 redis: client is closed)。
- app.RateLimitRegistry() + App-bound 限流中间件(多 App per-App 计数隔离)。
- StorageManager.Close() 闭合 Init/Close 对称性(io.Closer 断言,预留扩展点),
  closeResources 与 rollbackReplacedResources 对称收口。

新增公开 API:WithTrace / WithCronTask;app.Scheduler()/Cache()/RedisClient()/
RateLimitRegistry();cron/storage/cache/middleware 各 SwapDefault*;config.TraceConfig;
middleware.RateLimitRegistry + App-bound 方法;middleware.WithRedisClient;
storage.StorageManager.Close。

Breaking:删 WithoutWire;cron.AddTask pre-Init 注册失效(改用 WithCronTask);
middleware.StopRateLimiters 语义收窄;commitReplacedResources 不再关 previous;
cache.NewRedisCacheWithRedis / CacheManager.InitWithRedis 等签名变更。
详见 CHANGELOG [1.4.0] 升级说明。

验证:go build + go vet + go test -race ./... 全绿(-buildvcs=false)。
独立对抗性复审无 CRITICAL/HIGH。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 16:25:37 +08:00

97 lines
3.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"testing"
"time"
"github.com/EthanCodeCraft/xlgo-core/database"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
)
// TestRedisRateLimiterInjectedClientPriority 固化 Phase 5jwt 模型):
// RedisRateLimiter.redisClient() 注入优先、全局兜底。
func TestRedisRateLimiterInjectedClientPriority(t *testing.T) {
mrG := miniredis.RunT(t)
clientG := redis.NewClient(&redis.Options{Addr: mrG.Addr()})
t.Cleanup(func() { _ = clientG.Close() })
origGlobal := database.SetTestRedisClient(clientG)
t.Cleanup(func() { database.SetTestRedisClient(origGlobal) })
mrA := miniredis.RunT(t)
clientA := redis.NewClient(&redis.Options{Addr: mrA.Addr()})
t.Cleanup(func() { _ = clientA.Close() })
// 注入优先
rl := NewRedisRateLimiter("test", 10, time.Minute, WithRedisClient(clientA))
if got := rl.redisClient(); got != clientA {
t.Fatalf("injected redisClient() = %v, want clientA", got)
}
// 未注入 -> 全局兜底
rl2 := NewRedisRateLimiter("test", 10, time.Minute)
if got := rl2.redisClient(); got != clientG {
t.Fatalf("fallback redisClient() = %v, want clientG (global)", got)
}
}
// TestSwapDefaultRateLimitRegistryReturnsOld 固化 Phase 5Swap 返回被替换的旧 Registry。
func TestSwapDefaultRateLimitRegistryReturnsOld(t *testing.T) {
orig := defaultRateLimitRegistry.Load()
defer SwapDefaultRateLimitRegistry(orig)
first := NewRateLimitRegistry()
if prev := SwapDefaultRateLimitRegistry(first); prev != orig {
t.Fatalf("Swap returned %v, want orig", prev)
}
second := NewRateLimitRegistry()
if returned := SwapDefaultRateLimitRegistry(second); returned != first {
t.Fatalf("Swap returned %v, want first", returned)
}
if defaultRateLimitRegistry.Load() != second {
t.Fatal("Swap did not install second as default")
}
if got := SwapDefaultRateLimitRegistry(nil); got != second {
t.Fatalf("Swap(nil) = %v, want second (current default)", got)
}
}
// TestRateLimitRegistryStopIdempotent 固化 Phase 5Stop 幂等,重复调用不 panic。
func TestRateLimitRegistryStopIdempotent(t *testing.T) {
r := NewRateLimitRegistry()
_ = r.loginLimiterInstance()
_ = r.apiLimiterInstance()
_ = r.uploadLimiterInstance()
r.registerCustom(NewRateLimiter(5, time.Minute))
r.Stop()
r.Stop() // 幂等
}
// TestRateLimitRegistryIsolation 固化 Phase 5:两个 Registry 实例的 limiter 互相独立。
func TestRateLimitRegistryIsolation(t *testing.T) {
rA := NewRateLimitRegistry()
rB := NewRateLimitRegistry()
defer rA.Stop()
defer rB.Stop()
la := rA.loginLimiterInstance()
lb := rB.loginLimiterInstance()
if la == lb {
t.Fatal("two registries returned the same loginLimiter instance (not isolated)")
}
// rA 用尽 10 次;rB 不受影响
for i := 0; i < 10; i++ {
if !la.Allow("1.1.1.1") {
t.Fatalf("rA request %d should be allowed", i+1)
}
}
// rA 第 11 次应被拒绝(count=10 >= rate=10
if la.Allow("1.1.1.1") {
t.Fatal("rA 11th should be rejected (count reached limit)")
}
// rB 的 loginLimiter 独立计数,1.1.1.1 仍可放行
if !lb.Allow("1.1.1.1") {
t.Fatal("rB loginLimiter should be independent of rA (still allowed)")
}}