fix(tasks): 任务列表改按 id 降序,消除同刻创建任务的抖动
原先按 created_at 降序排,但没有二级排序键。同一时刻创建的多个任务时间戳 相同,排序不稳定;叠加前端每 10 秒轮询、以及 Manager 用内存 map 存任务 (遍历顺序随机、每次 List 都要重排),这些任务就会在列表里频繁互换位置。 改用 id 降序:id 由 BIGSERIAL 单调递增分配,即插入顺序,唯一且稳定,最新 创建的在最前。db.ListTasks 的 ORDER BY 与 Manager.List 的内存排序同步改。 验证: 新增 TestListTasksOrderByIDDesc 连本地 PG 实跑通过;db/server 全量 无新增失败(仅剩既有的 TestCompanyScopeInvalid)。
This commit is contained in:
1 parent
dfbbb33069
commit
4cf36c7896
9 files changed
+128
-3
No files matched your search
@@ -563,3 +563,45 @@ func TestCreateTaskWithCompanyScopes(t *testing.T) {
|
||||
t.Fatalf("failed company association leaked %d exploration rows", leaked)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListTasksOrderByIDDesc pins list ordering to id-descending. created_at is
|
||||
// deliberately not the sort key: tasks created in the same instant share a
|
||||
// timestamp and would reorder between polls; id is unique and monotonic.
|
||||
func TestListTasksOrderByIDDesc(t *testing.T) {
|
||||
d, err := Open(testDSN(t))
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable (%v) — skipping", err)
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
stamp := time.Now().UnixNano()
|
||||
var ids []int64
|
||||
for i := range 3 {
|
||||
tk, err := d.CreateTask(fmt.Sprintf("order-%d-%d", stamp, i), "goal", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = d.DeleteTask(tk.ID) })
|
||||
ids = append(ids, tk.ID)
|
||||
}
|
||||
|
||||
list, err := d.ListTasks()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Reduce to just the ids created here; other rows may exist in the shared DB.
|
||||
mine := map[int64]bool{ids[0]: true, ids[1]: true, ids[2]: true}
|
||||
var seen []int64
|
||||
for _, task := range list {
|
||||
if mine[task.ID] {
|
||||
seen = append(seen, task.ID)
|
||||
}
|
||||
}
|
||||
if len(seen) != 3 {
|
||||
t.Fatalf("found %d of the created tasks in the list, want 3", len(seen))
|
||||
}
|
||||
// Newest (largest id) first.
|
||||
if seen[0] != ids[2] || seen[1] != ids[1] || seen[2] != ids[0] {
|
||||
t.Fatalf("order=%v, want descending %v", seen, []int64{ids[2], ids[1], ids[0]})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user