fix(feed): 热榜回退跳过整页已删视频
`DeleteVideo` 不级联删互动行。整页视频都被删掉时,旧代码返回空列表。`has_more` 仍为 true,客户端会翻到一个空页。 `listByHotWindowDB` 改为按 SQL 行推进的扫批循环。整批全删且本批满页时,游标前进 `len(rows)` 继续取。上限 `hotDBMaxSweep = 5` 批,避免一次请求变成几十次查询。`next_offset` 仍是 `offset + 已消费行数`,被跳过的行也计入。 补四条测试。整页已删时不重不漏并能终止。扫满上限后收尾。非整分钟 `as_of` 截断后采纳。 验证:go test -count=1 ./... 全绿
This commit is contained in:
@@ -213,6 +213,114 @@ func TestListByPopularity_LatestBranchIsTerminal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 视频被硬删后互动行还在(DeleteVideo 不级联),整页都被 GetByIDs 过滤掉时
|
||||
// 不能直接回空页,游标必须越过被跳过的行继续取。
|
||||
func TestListByPopularity_DBWindowSkipsDeletedVideos(t *testing.T) {
|
||||
f := newFeedFixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
alive := f.seedHot(t, 1)
|
||||
for _, likes := range []int{5, 4} {
|
||||
gone := f.seedHot(t, likes)
|
||||
if err := f.db.Delete(&video.Video{}, gone.ID).Error; err != nil {
|
||||
t.Fatalf("硬删视频 %d: %v", gone.ID, err)
|
||||
}
|
||||
}
|
||||
f.mr.FlushAll() // 桶与快照一起丢:强制走 DB 窗口
|
||||
|
||||
resp, err := f.svc.ListByPopularity(ctx, 2, f.asOf.Unix(), 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByPopularity: %v", err)
|
||||
}
|
||||
if got := ids(resp.VideoList); len(got) != 1 || got[0] != alive.ID {
|
||||
t.Fatalf("首屏 = %v, want [%d](整页已删视频不能吃掉分页位置)", got, alive.ID)
|
||||
}
|
||||
if resp.HasMore {
|
||||
t.Fatalf("has_more = true(next_offset=%d):跳过已删视频后窗口已经到底", resp.NextOffset)
|
||||
}
|
||||
if resp.NextOffset != 3 {
|
||||
t.Fatalf("next_offset = %d, want 3(游标要越过被跳过的 2 行,否则第 2 页重复读)", resp.NextOffset)
|
||||
}
|
||||
}
|
||||
|
||||
// 按客户端方式翻到底:整页已删视频后不重不漏,并且能终止。
|
||||
func TestListByPopularity_DBWindowPaginatesPastDeletedVideos(t *testing.T) {
|
||||
f := newFeedFixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 分数严格递减:4 个高分视频(会被删)排在 2 个存活视频之前。
|
||||
want := map[uint]bool{}
|
||||
for i := 0; i < 2; i++ {
|
||||
want[f.seedHot(t, 2).ID] = true
|
||||
}
|
||||
for i := 0; i < 4; i++ {
|
||||
gone := f.seedHot(t, 10+i)
|
||||
if err := f.db.Delete(&video.Video{}, gone.ID).Error; err != nil {
|
||||
t.Fatalf("硬删视频 %d: %v", gone.ID, err)
|
||||
}
|
||||
}
|
||||
f.mr.FlushAll()
|
||||
|
||||
got := map[uint]bool{}
|
||||
offset := 0
|
||||
for page := 0; page < 10; page++ {
|
||||
resp, err := f.svc.ListByPopularity(ctx, 2, f.asOf.Unix(), offset, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("第 %d 页: %v", page+1, err)
|
||||
}
|
||||
for _, id := range ids(resp.VideoList) {
|
||||
if got[id] {
|
||||
t.Fatalf("第 %d 页重复返回视频 %d(offset=%d)", page+1, id, offset)
|
||||
}
|
||||
got[id] = true
|
||||
}
|
||||
if !resp.HasMore {
|
||||
break
|
||||
}
|
||||
if resp.NextOffset <= offset {
|
||||
t.Fatalf("has_more=true 但 next_offset(%d) 没前进(offset=%d):翻页会死循环", resp.NextOffset, offset)
|
||||
}
|
||||
offset = resp.NextOffset
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("翻完拿到 %v, want %v(漏了存活视频)", got, want)
|
||||
}
|
||||
for id := range want {
|
||||
if !got[id] {
|
||||
t.Fatalf("翻完没拿到存活视频 %d", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 连续 hotDBMaxSweep 批都只剩已删视频时收尾,更靠后的视频留到下一次 as_of。
|
||||
func TestListByPopularity_DBWindowStopsAfterSweepLimit(t *testing.T) {
|
||||
f := newFeedFixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
alive := f.seedHot(t, 1) // 分数最低,排在最后
|
||||
for i := 0; i < 12; i++ {
|
||||
gone := f.seedHot(t, 10+i) // 分数 10..21,全部排在前面
|
||||
if err := f.db.Delete(&video.Video{}, gone.ID).Error; err != nil {
|
||||
t.Fatalf("硬删视频 %d: %v", gone.ID, err)
|
||||
}
|
||||
}
|
||||
f.mr.FlushAll()
|
||||
|
||||
// limit=2 → 5 批只够越过 10 行,第 11、12 行与存活的 alive 都还在后面。
|
||||
resp, err := f.svc.ListByPopularity(ctx, 2, f.asOf.Unix(), 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByPopularity: %v", err)
|
||||
}
|
||||
if len(resp.VideoList) != 0 || resp.HasMore {
|
||||
t.Fatalf("视频列表 = %v has_more=%v, want 空/false(撞到扫批上限后收尾)",
|
||||
ids(resp.VideoList), resp.HasMore)
|
||||
}
|
||||
if want := 2 * hotDBMaxSweep; resp.NextOffset != want {
|
||||
t.Fatalf("next_offset = %d, want %d(游标只越过 5 批 rows,alive=%d 留到下一个 as_of)",
|
||||
resp.NextOffset, want, alive.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// 60 个空桶的并集是"空表",不能把它发布成快照:否则这个 as_of 在 TTL 内
|
||||
// 每次都会命中"存在的空快照","真的空榜"与"快照丢了"从此分不开。
|
||||
func TestListByPopularity_EmptyUnionIsNotPublished(t *testing.T) {
|
||||
@@ -298,6 +406,23 @@ func TestListByPopularity_StaleAsOfIsReplaced(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 非整分钟的 as_of 不是非法输入:截断到整分钟再采纳,由客户端按返回的 as_of 重置。
|
||||
func TestListByPopularity_NonWholeMinuteAsOfIsTruncated(t *testing.T) {
|
||||
f := newFeedFixture(t)
|
||||
ctx := context.Background()
|
||||
f.seedHot(t, 1)
|
||||
|
||||
requested := f.asOf.Add(-5*time.Minute + 30*time.Second)
|
||||
resp, err := f.svc.ListByPopularity(ctx, 10, requested.Unix(), 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByPopularity: %v", err)
|
||||
}
|
||||
if want := f.asOf.Add(-5 * time.Minute).Unix(); resp.AsOf != want {
|
||||
t.Fatalf("as_of = %d, want %d(非整分钟必须先截断再采纳,不是退回默认值)",
|
||||
resp.AsOf, want)
|
||||
}
|
||||
}
|
||||
|
||||
// 负 offset 是非法游标,必须在读路径入口夹到 0。
|
||||
//
|
||||
// 不夹的话它一路走到 SQL:GORM 把 -1 绑成 OFFSET 的参数,MySQL 抛
|
||||
|
||||
Reference in New Issue
Block a user