Files
Cyrene_v2/internal/services/channelbridge/engage_session_test.go
T
AskaEth 1ae9158250 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 全绿。
2026-10-24 23:18:45 +08:00

82 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package channelbridge
// 本文件覆盖裁决 06.4.3 在参与度引擎侧的断言:会话状态键与入站节流键是同一个口径,
// 且每个会话的参与度状态互相独立——一个群冷场/参与中不影响另一个群。
import (
"context"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
)
// TestEngageKeyMatchesThrottleKey 固定「成对契约」:参与度引擎的状态键与入站节流键
// (adapter.ConversationKey)必须同口径,否则同一会话会在「计数」与「状态机」里分裂成
// 两个桶(裁决 06.4.3)。
func TestEngageKeyMatchesThrottleKey(t *testing.T) {
t.Parallel()
msg := nonOwnerGroupMsg("hi")
msg.ChannelID, msg.GroupID = "-100", "-100"
actor := adapter.Actor{Role: adapter.RoleMember, IsGroup: true, GroupID: "-100"}
if got, want := engageKey(msg, actor), adapter.ConversationKey(msg); got != want {
t.Errorf("engageKey = %q, 节流键 = %q, 两者必须同口径", got, want)
}
// 群聊缺 group id 时用 actor 上报的群 id 补齐后仍与节流键一致。
bare := nonOwnerGroupMsg("hi")
bare.ChannelID, bare.GroupID = "", ""
patchActor := adapter.Actor{Role: adapter.RoleMember, IsGroup: true, GroupID: "-20"}
patched := bare
patched.GroupID = "-20"
if got, want := engageKey(bare, patchActor), adapter.ConversationKey(patched); got != want {
t.Errorf("缺 group id 时 engageKey = %q, 期望 %q", got, want)
}
// 两个群必须是两个键。
other := nonOwnerGroupMsg("hi")
other.ChannelID, other.GroupID = "-200", "-200"
if engageKey(msg, actor) == engageKey(other, actor) {
t.Error("两个群共用同一个参与度状态键(会话未隔离)")
}
}
// TestEngageStateIsPerConversation 断言两个群并发互不挤占:A 群被要求安静进入 COOLDOWN
// 后,B 群仍能正常触发判定并回复(状态与计数都按会话独立,裁决 06.4.3)。
func TestEngageStateIsPerConversation(t *testing.T) {
t.Parallel()
judge := &engageScriptJudge{responses: []LightDecision{
{WorthReply: true, Interesting: true, Reason: "想聊"},
}}
b, chat, _ := engageTestBridge(t, judge, DefaultEngageConfig(), dayNow(), nil)
ctx := context.Background()
// A 群冷场:一句「太吵了」把 A 群打进 COOLDOWN,且不消耗判定。
cold := nonOwnerGroupMsg("太吵了,闭嘴")
cold.ChannelID, cold.GroupID = "-100", "-100"
b.process(ctx, cold)
if judge.calls != 0 {
t.Fatalf("冷场消息触发了轻判调用 = %d, want 0", judge.calls)
}
// A 群的后续触发消息处于 COOLDOWN:必须不调判定、不回复。
groupA := nonOwnerGroupMsg("昔涟 你来说说")
groupA.ChannelID, groupA.GroupID = "-100", "-100"
b.process(ctx, groupA)
if judge.calls != 0 {
t.Fatalf("A 群 COOLDOWN 期间仍调用了判定 = %d, want 0", judge.calls)
}
// B 群不受 A 群影响:照常 PROBING → 判定 → 回复。
groupB := nonOwnerGroupMsg("昔涟 你来说说")
groupB.ChannelID, groupB.GroupID = "-200", "-200"
b.process(ctx, groupB)
if judge.calls != 1 {
t.Fatalf("B 群判定被 A 群 COOLDOWN 挤占,judge.calls = %d, want 1", judge.calls)
}
if chat.sessionID != "sess-g" {
t.Fatalf("B 群判定为想聊后应驱动 loop,session = %q", chat.sessionID)
}
}