Files
VLoop/backend/internal/feed/repo_integration_test.go
huanghaosheng 85995f9f73 feat(feed): 热榜读路径收口到闭合分钟的只建一次快照
窗口右端改为上一个已闭合分钟。当前分钟仍在增长,不能当右端。
一个 as_of 的快照只建一次。ZUNIONSTORE 先写临时键,再用 RENAMENX 原子占位,抢不到的人读赢家。
空并集不再发布成快照。否则这个 as_of 在 TTL 内每次都会命中一张空表,却报告 snapshot_rebuilt=false。
快照 TTL 改为 1 小时。EXPIRE 失败时删掉 dest,避免它继承临时键的 1 分钟寿命而永远翻不到第 2 页。
NextOffset 按 len(members) 推进。视频被删除后 member 仍在,按存活视频数推进会重复读到已跳过的位置。
DB 回退改用 ListHotWindow 重算同一窗口。排序为 score DESC, video_id DESC,与 ZSET 一致。
窗口内无互动时返回 latest。这一支是终结页,NextOffset 为 0。
ZUnionStore 返回并集基数,供调用方判空。新增 RenameNX 包装方法。
删除旧游标字段与 latest_* 配对校验。请求只剩 limit、as_of、offset。
验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
2026-11-01 15:15:31 +08:00

122 lines
3.7 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.
//go:build integration
package feed
import (
"context"
"os"
"strings"
"testing"
"time"
"feedsystem_video_go/internal/video"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// 这些用例需要真实 MySQL:ListHotWindow 的排序定义(分数降序、同分按 id 降序)
// 必须与 Redis 路径逐字一致,而 SQL 的 GROUP BY / CAST / ORDER BY 语义
// 用替身验证等于什么都没验证。
//
// docker compose up -d mysql
// MYSQL_TEST_DSN='root:123456@tcp(127.0.0.1:3306)/feedsystem_test?charset=utf8mb4&parseTime=True&loc=Local' \
// go test -tags integration -count=1 ./internal/feed/
func newFeedTestDB(t *testing.T) *gorm.DB {
t.Helper()
dsn := os.Getenv("MYSQL_TEST_DSN")
if dsn == "" {
t.Skip("MYSQL_TEST_DSN 未设置:需要真实 MySQL(见本文件顶部注释)")
}
if strings.Contains(dsn, "clientFoundRows") {
t.Fatalf("测试 DSN 不能带 clientFoundRows:它会破坏 RowsAffected 判重")
}
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
t.Fatalf("connect mysql: %v", err)
}
if err := db.Migrator().DropTable(&video.Video{}, &video.Like{}, &video.Comment{}); err != nil {
t.Fatalf("drop tables: %v", err)
}
if err := db.AutoMigrate(&video.Video{}, &video.Like{}, &video.Comment{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return db
}
func seedFeedVideo(t *testing.T, db *gorm.DB) *video.Video {
t.Helper()
v := &video.Video{
AuthorID: 1, Username: "author", Title: "t",
PlayURL: "p", CoverURL: "c",
}
if err := db.Create(v).Error; err != nil {
t.Fatalf("seed video: %v", err)
}
return v
}
func seedLikeAt(t *testing.T, db *gorm.DB, videoID, accountID uint, at time.Time) {
t.Helper()
if err := db.Create(&video.Like{VideoID: videoID, AccountID: accountID, CreatedAt: at}).Error; err != nil {
t.Fatalf("seed like: %v", err)
}
}
func seedCommentAt(t *testing.T, db *gorm.DB, videoID uint, at time.Time) {
t.Helper()
c := &video.Comment{Username: "alice", VideoID: videoID, AuthorID: 1, Content: "x", CreatedAt: at}
if err := db.Create(c).Error; err != nil {
t.Fatalf("seed comment: %v", err)
}
}
// Redis 路径用补零 member,同分时按 member 字符串降序 —— 也就是 id 降序。
// DB 回退必须复刻同一个顺序,否则同一次请求在两条路径上看到两个榜。
func TestListHotWindow_OrdersByScoreThenIDDesc(t *testing.T) {
db := newFeedTestDB(t)
repo := NewFeedRepository(db)
ctx := context.Background()
low := seedFeedVideo(t, db)
highA := seedFeedVideo(t, db)
highB := seedFeedVideo(t, db)
asOf := time.Now().UTC().Truncate(time.Minute).Add(-time.Minute)
at := asOf.Add(-5 * time.Minute)
to := asOf.Add(time.Minute)
// 分数:highA = highB = 3(同分,按 id DESC 应 highB 在前),low = 1。
for i := uint(1); i <= 3; i++ {
seedLikeAt(t, db, highA.ID, i, at)
}
seedLikeAt(t, db, highB.ID, 1, at)
seedCommentAt(t, db, highB.ID, at)
seedCommentAt(t, db, highB.ID, at.Add(time.Second))
seedLikeAt(t, db, low.ID, 1, at)
// 窗口外的互动不得进入结果。
seedLikeAt(t, db, low.ID, 2, to)
rows, err := repo.ListHotWindow(ctx, asOf.Add(-59*time.Minute), to, 10, 0)
if err != nil {
t.Fatalf("ListHotWindow: %v", err)
}
wantIDs := []uint{highB.ID, highA.ID, low.ID}
if len(rows) != len(wantIDs) {
t.Fatalf("rows = %+v, want %d 行", rows, len(wantIDs))
}
for i, want := range wantIDs {
if rows[i].VideoID != want {
t.Fatalf("第 %d 行 video_id = %d, want %d(分数降序,同分 id 降序)", i, rows[i].VideoID, want)
}
}
if rows[0].Score != 3 || rows[2].Score != 1 {
t.Fatalf("分数 = %d/%d, want 3/1", rows[0].Score, rows[2].Score)
}
}