134 lines
4.9 KiB
Go
134 lines
4.9 KiB
Go
|
|
//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)
|
|||
|
|
}
|
|||
|
|
}
|