Files
xlgo-core/cache/lock_test.go
T
杭州明婳科技 6595e94677 fix: 多模型评审终审收口 — P0安全 + 主线A并发治理 + 文档对齐
经 deepseek/GLM/Claude 三轮对抗性评审 + 交叉核验 + 终审(见
last_report.md、cross_review_assessment.md),逐条回读源码核实并修复。
go build / vet / test -race ./... 全绿。

安全(P0):
- JWT alg confusion:WithValidMethods 固定 HMAC + keyfunc 方法断言
- JWT 空密钥 / 不支持算法 fail-closed(ErrEmptySecret / ErrUnsupportedAlgorithm)
- 上传大小实测封顶(enforceUploadSize),不信任客户端 file.Size
- HTTP headers/cookies map 竞态(写锁+快照)+ SSRF 防护(opt-in dialer.Control)

并发/资源(主线A 闭合):
- 包级可变全局一律 atomic.Pointer:DefaultRedis / DefaultStorage /
  DefaultManager / Validator / DefaultCache 等,消除裸指针数据竞争
- trace.Close 去 sync.Once(防 exporter 泄漏);app OnReady 失败走 Shutdown
- ws.Hub.Stop stopOnce 防 double-close;cron 接入 App 生命周期(WithCron)
- ratelimit failClosed atomic.Bool;Recover Written() 守卫
- cron checkAndRun 锁内收集锁外 spawn(wg.Add 在锁内防 Stop 竞态)

终审剩余项收口(H-A~M-H + L 系列):
- 副本连接池 MaxOpenConns/2 截断修复(replicaMaxOpenConns)
- JWT 黑名单 1s 超时 + 显式错误;ratelimit GetRedis 取一次复用
- GetPage 深分页上限;HashFile 流式;ReadFile 去 TOCTOU
- config Clone() 深拷贝;cache 锁操作返 ErrRedisNotReady
- compress 解压残留清理;正则预编译;EqualsIgnoreCase→EqualFold
- 删 redisLimiters 死代码;CLI 输入校验 + 回滚

文档:
- README/GUIDE/CHANGELOG 对齐当前 API(含破坏性变更迁移说明)
- docs/README 索引修正;config 注释修正(RS256 已不支持)

破坏性变更详见 CHANGELOG.md [Unreleased]。项目处于研发初期、无下游用户。

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

149 lines
3.4 KiB
Go
Raw 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 cache_test
import (
"context"
"errors"
"testing"
"time"
"github.com/EthanCodeCraft/xlgo-core/cache"
)
func TestLockToken(t *testing.T) {
token := &cache.LockToken{
Key: "test_lock",
Token: "abc123",
}
if token.Key != "test_lock" {
t.Error("LockToken Key failed")
}
if token.Token != "abc123" {
t.Error("LockToken Token failed")
}
}
func TestLockErrors(t *testing.T) {
ctx := context.Background()
// Test ErrLockNotHeld - 当 Redis 未初始化时,先检查 nil token
err := cache.Unlock(ctx, nil)
// 当 Redis 未初始化时,会返回 ErrRedisNotReady
if err != cache.ErrLockNotHeld && err != cache.ErrRedisNotReady {
t.Errorf("Unlock with nil token should return ErrLockNotHeld or ErrRedisNotReady, got %v", err)
}
// Test IsLocked without Redis — M-E:应返回 (false, ErrRedisNotReady)
// 让调用方可区分"Redis 不可用"与"锁未占用"(原 (false,nil) 与"未占用"不可区分)。
locked, err := cache.IsLocked(ctx, "test_key")
if !errors.Is(err, cache.ErrRedisNotReady) {
t.Errorf("IsLocked without Redis should return ErrRedisNotReady, got %v", err)
}
if locked {
t.Error("IsLocked should return false without Redis")
}
// Test GetLockTTL without Redis — M-E:应返回 (0, ErrRedisNotReady)。
ttl, err := cache.GetLockTTL(ctx, "test_key")
if !errors.Is(err, cache.ErrRedisNotReady) {
t.Errorf("GetLockTTL without Redis should return ErrRedisNotReady, got %v", err)
}
if ttl != 0 {
t.Error("GetLockTTL should return 0 without Redis")
}
}
func TestIncrDecr(t *testing.T) {
ctx := context.Background()
// Test Incr without Redis
n, err := cache.Incr(ctx, "counter")
if err != nil {
t.Errorf("Incr error: %v", err)
}
if n != 0 {
t.Error("Incr should return 0 without Redis")
}
// Test IncrBy
n, err = cache.IncrBy(ctx, "counter", 10)
if err != nil {
t.Errorf("IncrBy error: %v", err)
}
if n != 0 {
t.Error("IncrBy should return 0 without Redis")
}
// Test Decr
n, err = cache.Decr(ctx, "counter")
if err != nil {
t.Errorf("Decr error: %v", err)
}
if n != 0 {
t.Error("Decr should return 0 without Redis")
}
}
func TestSetExpire(t *testing.T) {
ctx := context.Background()
ok, err := cache.SetExpire(ctx, "test_key", time.Minute)
if err != nil {
t.Errorf("SetExpire error: %v", err)
}
if ok {
t.Error("SetExpire should return false without Redis")
}
}
func TestGetRawSetRaw(t *testing.T) {
ctx := context.Background()
// Test SetRaw
err := cache.SetRaw(ctx, "test_key", "test_value", time.Minute)
if err != nil {
t.Errorf("SetRaw error: %v", err)
}
// Test GetRaw
val, err := cache.GetRaw(ctx, "test_key")
if err != nil {
t.Errorf("GetRaw error: %v", err)
}
if val != "" {
t.Error("GetRaw should return empty string without Redis")
}
}
func TestKFunctions(t *testing.T) {
// Test various K functions
key := cache.K("user:1")
if key == "" {
t.Error("K should not return empty string")
}
tempKey := cache.KTemp("token")
if tempKey == "" {
t.Error("KTemp should not return empty string")
}
permKey := cache.KPerm("config")
if permKey == "" {
t.Error("KPerm should not return empty string")
}
lockKey := cache.KLock("order:123")
if lockKey == "" {
t.Error("KLock should not return empty string")
}
counterKey := cache.KCounter("visit")
if counterKey == "" {
t.Error("KCounter should not return empty string")
}
sessionKey := cache.KSession("sid")
if sessionKey == "" {
t.Error("KSession should not return empty string")
}
}