mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-12 04:59:39 +08:00
- 前后端 Redis SCAN 游标统一为字符串传递,避免 Number 精度丢失 - RedisScanKeys 增加 string/number 游标兼容解析,异常游标降级并告警 - 新增游标解析单测 - refs #135
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseRedisScanCursor(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
input any
|
|
want uint64
|
|
wantErr bool
|
|
}{
|
|
{name: "nil defaults to zero", input: nil, want: 0},
|
|
{name: "empty string defaults to zero", input: " ", want: 0},
|
|
{name: "string cursor", input: "123", want: 123},
|
|
{name: "uint64 cursor", input: uint64(456), want: 456},
|
|
{name: "int cursor", input: int(789), want: 789},
|
|
{name: "float cursor", input: float64(42), want: 42},
|
|
{name: "json number cursor", input: json.Number("88"), want: 88},
|
|
{name: "negative int rejected", input: -1, wantErr: true},
|
|
{name: "fraction float rejected", input: float64(1.5), wantErr: true},
|
|
{name: "invalid string rejected", input: "abc", wantErr: true},
|
|
{name: "unsupported type rejected", input: true, wantErr: true},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := parseRedisScanCursor(tc.input)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil (value=%d)", got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != tc.want {
|
|
t.Fatalf("parseRedisScanCursor() mismatch, want=%d got=%d", tc.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|