135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
|
|
package video
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"testing"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|||
|
|
|
|||
|
|
"github.com/alicebob/miniredis/v2"
|
|||
|
|
goredis "github.com/redis/go-redis/v9"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func newTestCache(t *testing.T) (*rediscache.Client, *miniredis.Miniredis) {
|
|||
|
|
t.Helper()
|
|||
|
|
mr := miniredis.RunT(t)
|
|||
|
|
rdb := goredis.NewClient(&goredis.Options{Addr: mr.Addr()})
|
|||
|
|
t.Cleanup(func() { _ = rdb.Close() })
|
|||
|
|
return rediscache.NewClient(rdb, "test:"), mr
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 桶名必须是那一分钟的 UTC 起始时刻:非 UTC 时区或未截断都会让同一次
|
|||
|
|
// 互动落进不同的桶,-verify 与 -rebuild-hot 也就对不上。
|
|||
|
|
func TestHotBucket_TruncatesToUTC(t *testing.T) {
|
|||
|
|
at := time.Date(2026, 3, 4, 5, 6, 7, 800_000_000, time.FixedZone("UTC+8", 8*3600))
|
|||
|
|
|
|||
|
|
got := HotBucket(at)
|
|||
|
|
want := "202603032106" // 05:06:07+08:00 == 21:06:07Z,截断到分钟
|
|||
|
|
if got != want {
|
|||
|
|
t.Fatalf("HotBucket(%v) = %q, want %q", at, got, want)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 补零到 12 位让 ZSET 的字典序等同数值序:同分时才能与 DB 的
|
|||
|
|
// "ORDER BY score DESC, video_id DESC" 给出同一个顺序。
|
|||
|
|
func TestHotMember_PadsToTwelveDigits(t *testing.T) {
|
|||
|
|
cases := []struct {
|
|||
|
|
name string
|
|||
|
|
id uint
|
|||
|
|
want string
|
|||
|
|
}{
|
|||
|
|
{"small", 7, "000000000007"},
|
|||
|
|
{"large", 123456789012, "123456789012"},
|
|||
|
|
}
|
|||
|
|
for _, c := range cases {
|
|||
|
|
t.Run(c.name, func(t *testing.T) {
|
|||
|
|
if got := HotMember(c.id); got != c.want {
|
|||
|
|
t.Fatalf("HotMember(%d) = %q, want %q", c.id, got, c.want)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 增量落在 occurredAt 那一分钟,而不是"现在"。
|
|||
|
|
// 这是本方案与旧实现的根本区别:积压 10 分钟的 worker 不能把 10 分钟前的
|
|||
|
|
// 点赞记成此刻的尖峰。
|
|||
|
|
func TestApplyHotDelta_WritesToOccurredBucket(t *testing.T) {
|
|||
|
|
cache, mr := newTestCache(t)
|
|||
|
|
at := time.Now().UTC().Add(-10 * time.Minute).Truncate(time.Minute)
|
|||
|
|
|
|||
|
|
if err := ApplyHotDelta(context.Background(), cache, 42, 1, at); err != nil {
|
|||
|
|
t.Fatalf("ApplyHotDelta: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
key := cache.Key("hot:video:1m:%s", HotBucket(at))
|
|||
|
|
if !mr.Exists(key) {
|
|||
|
|
t.Fatalf("桶 %s 不存在:增量没有落在 occurredAt 那一分钟", key)
|
|||
|
|
}
|
|||
|
|
if score, err := mr.ZScore(key, HotMember(42)); err != nil || score != 1 {
|
|||
|
|
t.Fatalf("桶内分数 = %v, err = %v, want 1", score, err)
|
|||
|
|
}
|
|||
|
|
if nowKey := cache.Key("hot:video:1m:%s", HotBucket(time.Now())); mr.Exists(nowKey) {
|
|||
|
|
t.Fatalf("增量不得落进当前分钟的桶 %s(那是消费时刻,不是事件时刻)", nowKey)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 没有 cache(Redis 未启用)时不得报错:调用方是 best-effort 路径,
|
|||
|
|
// 让它返回错误会把"本来就没有热榜"的部署变成失败请求。
|
|||
|
|
func TestApplyHotDelta_NilCacheIsNoop(t *testing.T) {
|
|||
|
|
if err := ApplyHotDelta(context.Background(), nil, 42, 1, time.Now()); err != nil {
|
|||
|
|
t.Fatalf("nil cache 不应返回错误: %v", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 超过桶寿命的迟到增量直接丢弃:落下去也会在读到它之前过期,
|
|||
|
|
// 留着只会让 -verify 把它当成"应该存在"的桶。
|
|||
|
|
func TestApplyHotDelta_StaleBucketIsSkipped(t *testing.T) {
|
|||
|
|
cache, mr := newTestCache(t)
|
|||
|
|
at := time.Now().UTC().Add(-HotWindowTTL - time.Minute)
|
|||
|
|
|
|||
|
|
if err := ApplyHotDelta(context.Background(), cache, 43, 1, at); err != nil {
|
|||
|
|
t.Fatalf("ApplyHotDelta: %v", err)
|
|||
|
|
}
|
|||
|
|
for _, key := range mr.Keys() {
|
|||
|
|
t.Fatalf("超期增量不得写入任何桶,却发现 key=%s", key)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 未来时间戳按当前分钟处理:否则增量会落进读窗口永远到不了的未来桶。
|
|||
|
|
func TestApplyHotDelta_FutureTimestampUsesCurrentBucket(t *testing.T) {
|
|||
|
|
cache, mr := newTestCache(t)
|
|||
|
|
future := time.Now().UTC().Add(24 * time.Hour)
|
|||
|
|
|
|||
|
|
if err := ApplyHotDelta(context.Background(), cache, 44, 1, future); err != nil {
|
|||
|
|
t.Fatalf("ApplyHotDelta: %v", err)
|
|||
|
|
}
|
|||
|
|
if mr.Exists(cache.Key("hot:video:1m:%s", HotBucket(future))) {
|
|||
|
|
t.Fatal("未来时间戳不得落进未来的桶")
|
|||
|
|
}
|
|||
|
|
if score, err := mr.ZScore(cache.Key("hot:video:1m:%s", HotBucket(time.Now())), HotMember(44)); err != nil || score != 1 {
|
|||
|
|
t.Fatalf("当前分钟桶内分数 = %v, err = %v, want 1", score, err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 缓存失效是 best-effort:热度变了,详情与实体缓存里的旧数字必须消失。
|
|||
|
|
func TestApplyHotDelta_InvalidatesDetailAndEntity(t *testing.T) {
|
|||
|
|
cache, mr := newTestCache(t)
|
|||
|
|
ctx := context.Background()
|
|||
|
|
detail := cache.Key("video:detail:id=%d", 46)
|
|||
|
|
entity := cache.Key("video:entity:%d", 46)
|
|||
|
|
if err := cache.SetBytes(ctx, detail, []byte("x"), time.Hour); err != nil {
|
|||
|
|
t.Fatalf("seed detail: %v", err)
|
|||
|
|
}
|
|||
|
|
if err := cache.SetBytes(ctx, entity, []byte("y"), time.Hour); err != nil {
|
|||
|
|
t.Fatalf("seed entity: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if err := ApplyHotDelta(ctx, cache, 46, 1, time.Now()); err != nil {
|
|||
|
|
t.Fatalf("ApplyHotDelta: %v", err)
|
|||
|
|
}
|
|||
|
|
if mr.Exists(detail) || mr.Exists(entity) {
|
|||
|
|
t.Fatalf("详情/实体缓存必须被失效")
|
|||
|
|
}
|
|||
|
|
}
|