点赞与评论的计数只由业务事务写。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 ./... 全绿。
169 lines
5.5 KiB
Go
169 lines
5.5 KiB
Go
//go:build integration
|
||
|
||
package worker
|
||
|
||
import (
|
||
"context"
|
||
"os"
|
||
"strings"
|
||
"testing"
|
||
|
||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||
"feedsystem_video_go/internal/video"
|
||
|
||
"gorm.io/driver/mysql"
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/logger"
|
||
)
|
||
|
||
// 评论消费者"只写一次评论、热度跟着事务走"这条不变式只能在真实 MySQL 上验证:
|
||
// 判重落在唯一索引 (author_id, idempotency_key) 上,没有数据库就没有被测对象。
|
||
//
|
||
// 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/worker/
|
||
|
||
func newCommentWorkerTestDB(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.Comment{}, &video.Like{}, &video.Video{}); err != nil {
|
||
t.Fatalf("drop tables: %v", err)
|
||
}
|
||
if err := db.AutoMigrate(&video.Video{}, &video.Comment{}, &video.Like{}); err != nil {
|
||
t.Fatalf("migrate: %v", err)
|
||
}
|
||
return db
|
||
}
|
||
|
||
func seedCommentVideo(t *testing.T, db *gorm.DB) *video.Video {
|
||
t.Helper()
|
||
v := &video.Video{
|
||
AuthorID: 99,
|
||
Username: "owner",
|
||
Title: "t",
|
||
PlayURL: "p",
|
||
CoverURL: "c",
|
||
Popularity: 0,
|
||
}
|
||
if err := db.Create(v).Error; err != nil {
|
||
t.Fatalf("seed video: %v", err)
|
||
}
|
||
return v
|
||
}
|
||
|
||
// 同一条评论事件被投递两次(假失败 + 兜底 + 重投的必然结果),只能落一行,
|
||
// 数据库热度也只能加一次。
|
||
//
|
||
// 两个断言分别钉住两件不同的事:行数 = 1 证明唯一索引吸收了重复;热度 = 1
|
||
// (而不是 2)证明"加热度"与"插入评论"在同一个事务里、且以 created 为门控。
|
||
// 热度写在这里而不是 Redis,是因为 videos.popularity 是事实,热榜只是它的滑窗副本。
|
||
func TestCommentWorker_ApplyPublish_DuplicateEventWritesOneRow(t *testing.T) {
|
||
db := newCommentWorkerTestDB(t)
|
||
v := seedCommentVideo(t, db)
|
||
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), nil, "comment.events")
|
||
evt := &rabbitmq.CommentEvent{
|
||
EventID: "evt-comment-1",
|
||
Action: "publish",
|
||
Username: "alice",
|
||
VideoID: v.ID,
|
||
AuthorID: 1,
|
||
Content: "好看",
|
||
}
|
||
|
||
for i := 0; i < 2; i++ {
|
||
if err := w.applyPublish(context.Background(), evt); err != nil {
|
||
t.Fatalf("第 %d 次 applyPublish: %v", i+1, err)
|
||
}
|
||
}
|
||
|
||
var count int64
|
||
if err := db.Model(&video.Comment{}).Where("video_id = ?", v.ID).Count(&count).Error; err != nil {
|
||
t.Fatalf("count comments: %v", err)
|
||
}
|
||
if count != 1 {
|
||
t.Fatalf("评论行数 = %d, want 1(event_id 相同的重复投递必须被判重吸收)", count)
|
||
}
|
||
|
||
var got video.Video
|
||
if err := db.First(&got, v.ID).Error; err != nil {
|
||
t.Fatalf("reload video: %v", err)
|
||
}
|
||
if got.Popularity != 1 {
|
||
t.Fatalf("popularity = %d, want 1(重复投递不得再加一次)", got.Popularity)
|
||
}
|
||
}
|
||
|
||
// event_id 为空(老版本消息/手工构造)时不能去重:没有键就无法判断重复,
|
||
// 插入 NULL 键是唯一正确选择——用空字符串会撞唯一索引,把所有后续评论都吃掉。
|
||
func TestCommentWorker_ApplyPublish_EmptyEventIDStillWrites(t *testing.T) {
|
||
db := newCommentWorkerTestDB(t)
|
||
v := seedCommentVideo(t, db)
|
||
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), nil, "comment.events")
|
||
|
||
evt := &rabbitmq.CommentEvent{
|
||
Action: "publish",
|
||
Username: "alice",
|
||
VideoID: v.ID,
|
||
AuthorID: 1,
|
||
Content: "第一条",
|
||
}
|
||
if err := w.applyPublish(context.Background(), evt); err != nil {
|
||
t.Fatalf("applyPublish: %v", err)
|
||
}
|
||
|
||
var c video.Comment
|
||
if err := db.Where("video_id = ?", v.ID).First(&c).Error; err != nil {
|
||
t.Fatalf("load comment: %v", err)
|
||
}
|
||
if c.IdempotencyKey != nil {
|
||
t.Fatalf("IdempotencyKey = %q, want NULL", *c.IdempotencyKey)
|
||
}
|
||
}
|
||
|
||
// 删除事件重投两次:第二次是空操作,不是错误(否则会被当成故障送去停泊)。
|
||
func TestCommentWorker_ApplyDelete_DuplicateIsNoop(t *testing.T) {
|
||
db := newCommentWorkerTestDB(t)
|
||
v := seedCommentVideo(t, db)
|
||
repo := video.NewCommentRepository(db)
|
||
w := NewCommentWorker(nil, nil, repo, video.NewVideoRepository(db), nil, "comment.events")
|
||
|
||
c := &video.Comment{Username: "alice", VideoID: v.ID, AuthorID: 1, Content: "x"}
|
||
if _, err := repo.CreateIdempotentWithPopularity(context.Background(), c, 1); err != nil {
|
||
t.Fatalf("seed comment: %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)
|
||
}
|
||
}
|
||
|
||
var count int64
|
||
if err := db.Model(&video.Comment{}).Where("id = ?", c.ID).Count(&count).Error; err != nil {
|
||
t.Fatalf("count: %v", err)
|
||
}
|
||
if count != 0 {
|
||
t.Fatalf("评论行数 = %d, want 0", count)
|
||
}
|
||
// P2-20:删除必须回退热度,且只回退一次(第二次删除是空操作)。
|
||
var got video.Video
|
||
if err := db.First(&got, v.ID).Error; err != nil {
|
||
t.Fatalf("reload video: %v", err)
|
||
}
|
||
if got.Popularity != 0 {
|
||
t.Fatalf("popularity = %d, want 0(删除要回退热度,重复删除不得再减)", got.Popularity)
|
||
}
|
||
}
|