test(channelbridge): 窗口夹具按截止时刻推钟 + 去掉无法证实的替身锁(M33 r1 复审)
P1-1(channelbridge 的 -race 门仍红,红在用例不确定性而不是竞态): - coalesceTimers 改成**截止时刻**语义:每次 after(d) 记 at = 注入时钟 + d, 推钟只把时钟推进到「当前最早的截止时刻」再触发它。旧写法按 FIFO 逐个触发并 按请求时长累加,实测 12 条消息内攒到 5 个废弃定时器把时钟推 +10s(窗口 2s、 绝对上限 6s)⇒ 循环头走「到点直接发车、不 takeAll」的分支,队列尾部落到下一批。 - 推钟前强制「车道排空」这条不变量(pump/advanceAndFire/waitIdle): 时钟只表示时间过去,它一转就可能跳过 takeAll,因此先等 worker 把货收进本批。 - sendSettled:投递后按**序号**等「这次投递之后新挂的定时器」(旧的都已被 worker 放弃、再没人监听),供「投递与推钟交替」的用例(每 500ms 一条的绝对上限用例)。 - fireLatest/advanceAndFire 带 done 谓词重试,避免「登记与触发之间续臂」导致 推了钟却没人醒;waitIdle 一边等一边推窗口(worker 停在窗口里时永远走不到退休)。 - 用例侧只补相位与等待,不动断言:TwelveBurst 读历史前等 assistant 行落库; 准入用例断言「最新一次续臂」(原来按下标取 d[1],隐含臂数假设); stale 用例两条都投完再推时钟(车道不会在两条之间退休)。 P2-1(替身锁无法用变异证实)选 A:删掉 stubChatter/stubSessions 的锁与访问器, 退回原字段访问,并写清理由 —— 21 个用例全走同步便捷路径 (b.process → processFrom → runBatch → Loop.Run),DEV HEAD 上 -race 数十次跑 (含把锁删掉的变异)DATA RACE 恒为 0;若将来从车道 worker 触碰这些字段必须加锁 并补 -race 用例。 证据:go test -race -count=3 ./internal/services/channelbridge/ 连跑 6 轮 18/18 全绿、DATA RACE=0;coalesce 子集 -count=80 全绿;整包 -race -count=20 全绿; 全树 go build/vet/test -count=1 全绿。
This commit is contained in:
1 parent
7d9b2132bb
commit
1ae9158250
20 files changed
+584
-371
No files matched your search
@@ -46,6 +46,17 @@ func coalesceOutboundReceipt(platform, groupID string) adapter.UnifiedMessage {
|
||||
return msg
|
||||
}
|
||||
|
||||
// latestDuration 取请求时长序列里**最新**的一个。
|
||||
//
|
||||
// 用例关心的是「刚发的那条消息之后 worker 续的臂」,而不是历史第几条:臂的次数取决于
|
||||
// worker 什么时候把消息收进本批(调度决定),按下标取会引入时序假设。
|
||||
func latestDuration(d []time.Duration) time.Duration {
|
||||
if len(d) == 0 {
|
||||
return 0
|
||||
}
|
||||
return d[len(d)-1]
|
||||
}
|
||||
|
||||
// waitTimerCalls 等到定时器工厂至少被请求 n 次,返回到目前为止的**请求时长序列**。
|
||||
func waitTimerCalls(t *testing.T, f *coalesceBridge, n int) []time.Duration {
|
||||
t.Helper()
|
||||
@@ -60,22 +71,43 @@ func waitTimerCalls(t *testing.T, f *coalesceBridge, n int) []time.Duration {
|
||||
return nil
|
||||
}
|
||||
|
||||
// fireLatest 按**最后一个**待定定时器自己的时长推进时钟并触发它。
|
||||
// fireLatest 把注入时钟推进到**最近一次**请求的截止时刻并触发它。
|
||||
//
|
||||
// 为什么是最后一个:窗口被重置/续臂时,旧的定时器通道留在那里(不会被触发),
|
||||
// 为什么是最近一次:窗口被重置/续臂时,旧的定时器通道留在那里(再没人监听),
|
||||
// 真正决定发车时刻的是**最近一次**请求的那个。
|
||||
func (t *coalesceTimers) fireLatest(tb *testing.T, clock *laneClock) {
|
||||
//
|
||||
// 为什么按**截止时刻**推进而不是「再推一个请求时长」:worker 可能已经在这段时间里
|
||||
// 重新续过臂(例如一条系统通知落在窗口中途),把时长再累加一次会让时钟越过绝对上限,
|
||||
// 循环头于是走「到点直接发车、不 takeAll」的分支;按截止时刻推进只表示「时间走到了
|
||||
// 它约定的那个点」。
|
||||
//
|
||||
// 为什么要重试:登记与触发之间隔着 worker 的一次循环 —— 万一它刚续了新臂,被触发的
|
||||
// 那个通道就没人接(时钟已经推了,却没人醒)。这里在 done() 未成立、仍有待触发定时器时
|
||||
// 继续触发,直到 worker 真的发车;断言口径由调用方守。
|
||||
func (t *coalesceTimers) fireLatest(tb *testing.T, clock *laneClock, done func() bool) {
|
||||
tb.Helper()
|
||||
t.mu.Lock()
|
||||
if len(t.pending) == 0 {
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
t.mu.Lock()
|
||||
if len(t.pending) == 0 {
|
||||
t.mu.Unlock()
|
||||
if done != nil && done() {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
continue
|
||||
}
|
||||
timer := t.pending[len(t.pending)-1]
|
||||
t.pending = t.pending[:len(t.pending)-1]
|
||||
t.mu.Unlock()
|
||||
tb.Fatalf("no pending coalesce timer to fire")
|
||||
clock.advanceTo(timer.at)
|
||||
timer.ch <- timer.at
|
||||
if done != nil && done() {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
timer := t.pending[len(t.pending)-1]
|
||||
t.pending = t.pending[:len(t.pending)-1]
|
||||
t.mu.Unlock()
|
||||
clock.advance(timer.d)
|
||||
timer.ch <- clock.Now()
|
||||
tb.Fatalf("fireLatest: 3s 内没有把窗口推到发车(calls=%d)", t.callCount())
|
||||
}
|
||||
|
||||
// TestCoalesceActionFrameDoesNotResetWindow 断言**准入在窗口等待阶段就生效**:
|
||||
@@ -103,12 +135,14 @@ func TestCoalesceActionFrameDoesNotResetWindow(t *testing.T) {
|
||||
|
||||
// 🔴 关键断言(状态,不是结果):下一次请求的**剩余**时长仍是 500ms。
|
||||
// 若这条帧被当成聊天消息(修前),计时会重置成完整的 2000ms。
|
||||
d := waitTimerCalls(t, f, 2)
|
||||
if d[1] != 500*time.Millisecond {
|
||||
t.Fatalf("re-armed window = %s, want 500ms (an action frame must not reset the quiet window)", d[1])
|
||||
// 断言的是**最新一次**请求(= 这条帧之后 worker 续的臂)。旧写法取 d[1],
|
||||
// 隐含「帧之前只续过一次臂」这个时序假设:worker 何时把 m1 收进本批、何时续臂
|
||||
// 都是调度决定的,于是 d[1] 有时是 m1 的那次续臂(2s),用例假红。
|
||||
if d := latestDuration(waitTimerCalls(t, f, 2)); d != 500*time.Millisecond {
|
||||
t.Fatalf("re-armed window = %s, want 500ms (an action frame must not reset the quiet window)", d)
|
||||
}
|
||||
|
||||
f.timers.fireLatest(t, f.clock)
|
||||
f.timers.fireLatest(t, f.clock, func() bool { return chat.runCount() >= 1 })
|
||||
f.waitRuns(t, 1)
|
||||
f.waitIdle(t)
|
||||
|
||||
@@ -146,7 +180,7 @@ func TestCoalesceSystemNoticeDoesNotJoinBatch(t *testing.T) {
|
||||
t.Fatalf("re-armed window = %s, want 500ms (a system notice must not reset the quiet window)", d[1])
|
||||
}
|
||||
|
||||
f.timers.fireLatest(t, f.clock)
|
||||
f.timers.fireLatest(t, f.clock, func() bool { return chat.runCount() >= 1 })
|
||||
f.waitRuns(t, 1)
|
||||
f.waitIdle(t)
|
||||
|
||||
@@ -215,10 +249,11 @@ func TestCoalesceChatStillResetsWindow(t *testing.T) {
|
||||
}
|
||||
f.clock.advance(1500 * time.Millisecond)
|
||||
f.send(coalesceGroupMsg("telegram", "-20", "9", "张三", "m2"))
|
||||
if d := waitTimerCalls(t, f, 2); d[1] != 2000*time.Millisecond {
|
||||
t.Fatalf("re-armed window = %s, want a full 2s (a chat message resets the window)", d[1])
|
||||
// 同 action-frame 用例:断言最新一次请求(本消息触发的续臂),不假设此前的臂数。
|
||||
if d := latestDuration(waitTimerCalls(t, f, 2)); d != 2000*time.Millisecond {
|
||||
t.Fatalf("re-armed window = %s, want a full 2s (a chat message resets the window)", d)
|
||||
}
|
||||
f.timers.fireLatest(t, f.clock)
|
||||
f.timers.fireLatest(t, f.clock, func() bool { return chat.runCount() >= 1 })
|
||||
f.waitRuns(t, 1)
|
||||
if got := userTextsIn(chat.runAt(0).messages); len(got) != 2 {
|
||||
t.Fatalf("batch user messages = %v, want both chat messages", got)
|
||||
|
||||
Reference in new issue
Block a user