Files
VLoop/backend/internal/worker/likeworker_integration_test.go
huanghaosheng 1b47c9f027 refactor(video): 计数改由业务事务维护并删除热度第二条写路径
点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。
热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。
删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。
删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。
DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。
likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。
验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
2026-11-01 15:15:15 +08:00

134 lines
4.9 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.
//go:build integration
package worker
import (
"context"
"testing"
"time"
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis"
"feedsystem_video_go/internal/video"
"github.com/alicebob/miniredis/v2"
goredis "github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// 热榜增量必须由"事务是否真的插入"驱动,而不是由消息驱动:
// 重投一次不能再加一次分,否则同一次点赞会在榜上加两次。
func newLikeWorkerCache(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
}
func hotScore(t *testing.T, cache *rediscache.Client, mr *miniredis.Miniredis, videoID uint, at time.Time) float64 {
t.Helper()
score, err := mr.ZScore(cache.Key("hot:video:1m:%s", video.HotBucket(at)), video.HotMember(videoID))
if err != nil {
return 0
}
return score
}
func reloadVideo(t *testing.T, db *gorm.DB, id uint) video.Video {
t.Helper()
var v video.Video
if err := db.First(&v, id).Error; err != nil {
t.Fatalf("reload video: %v", err)
}
return v
}
func TestLikeWorker_ReplayDoesNotDoubleHot(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
cache, mr := newLikeWorkerCache(t)
w := NewLikeWorker(nil, nil, video.NewLikeRepository(db), video.NewVideoRepository(db), cache, "like.events")
// 10 分钟前发生的点赞,10 分钟后才被消费:桶要按 OccurredAt 落。
at := time.Now().UTC().Add(-10 * time.Minute).Truncate(time.Second)
evt := &rabbitmq.LikeEvent{EventID: "evt-like-1", Action: "like", UserID: 7, VideoID: v.ID, OccurredAt: at}
for i := 0; i < 2; i++ {
if err := w.applyLike(context.Background(), *evt); err != nil {
t.Fatalf("第 %d 次 applyLike: %v", i+1, err)
}
}
got := reloadVideo(t, db, v.ID)
if got.LikesCount != 1 || got.Popularity != 1 {
t.Fatalf("likes_count=%d popularity=%d, want 1/1(重投不得再计数)", got.LikesCount, got.Popularity)
}
if score := hotScore(t, cache, mr, v.ID, at); score != 1 {
t.Fatalf("热榜分数 = %v, want 1(重投不得再加一次分)", score)
}
}
func TestLikeWorker_UnlikeRevertsOriginalBucket(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
cache, mr := newLikeWorkerCache(t)
w := NewLikeWorker(nil, nil, video.NewLikeRepository(db), video.NewVideoRepository(db), cache, "like.events")
at := time.Now().UTC().Add(-20 * time.Minute).Truncate(time.Second)
if err := w.applyLike(context.Background(), rabbitmq.LikeEvent{EventID: "e1", Action: "like", UserID: 7, VideoID: v.ID, OccurredAt: at}); err != nil {
t.Fatalf("applyLike: %v", err)
}
// 取消事件的重投也要是空操作,且 -1 落回点赞那一分钟。
unlike := &rabbitmq.LikeEvent{EventID: "e2", Action: "unlike", UserID: 7, VideoID: v.ID}
if err := w.applyUnlike(context.Background(), *unlike); err != nil {
t.Fatalf("applyUnlike: %v", err)
}
if err := w.applyUnlike(context.Background(), *unlike); err != nil {
t.Fatalf("重复 applyUnlike: %v", err)
}
got := reloadVideo(t, db, v.ID)
if got.LikesCount != 0 || got.Popularity != 0 {
t.Fatalf("likes_count=%d popularity=%d, want 0/0", got.LikesCount, got.Popularity)
}
if score := hotScore(t, cache, mr, v.ID, at); score != 0 {
t.Fatalf("原桶分数 = %v, want 0(+1 与 -1 落在同一分钟)", score)
}
}
// P2-20:删除评论时必须把热度 -1 记回评论创建的那一分钟。
func TestCommentWorker_DeleteRevertsHot(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
cache, mr := newLikeWorkerCache(t)
repo := video.NewCommentRepository(db)
w := NewCommentWorker(nil, nil, repo, video.NewVideoRepository(db), cache, "comment.events")
createdAt := time.Now().UTC().Add(-15 * time.Minute).Truncate(time.Second)
c := &video.Comment{Username: "alice", VideoID: v.ID, AuthorID: 1, Content: "x", CreatedAt: createdAt}
if created, err := repo.CreateIdempotentWithPopularity(context.Background(), c, 1); err != nil || !created {
t.Fatalf("seed comment = created:%v err:%v", created, err)
}
// 用真实行时间模拟"发布时已经加过一分"。
if err := video.ApplyHotDelta(context.Background(), cache, v.ID, 1, createdAt); err != nil {
t.Fatalf("seed hot delta: %v", err)
}
evt := &rabbitmq.CommentEvent{EventID: "evt-del-1", Action: "delete", CommentID: c.ID}
for i := 0; i < 2; i++ {
if err := w.applyDelete(context.Background(), evt); err != nil {
t.Fatalf("第 %d 次 applyDelete: %v", i+1, err)
}
}
got := reloadVideo(t, db, v.ID)
if got.Popularity != 0 {
t.Fatalf("popularity = %d, want 0(删除回退一次,重投不再减)", got.Popularity)
}
if score := hotScore(t, cache, mr, v.ID, createdAt); score != 0 {
t.Fatalf("原桶分数 = %v, want 0(-1 必须落回创建那一分钟)", score)
}
}