重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+61 -61
View File
@@ -12,12 +12,12 @@ import (
// TestHandleMessage_SystemEventRoutesToAutomation 付款待发货系统卡片进入自动化中心,不进入回复链。
func TestHandleMessage_SystemEventRoutesToAutomation(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// decrypted 保存decrypted,供当前处理流程使用
// decrypted 用于本次流程后续判断的decrypted
decrypted := mustPaidDeliveryCard(t)
acc.handleMessage(decrypted)
@@ -26,7 +26,7 @@ func TestHandleMessage_SystemEventRoutesToAutomation(t *testing.T) {
// 直接验证:handleMessage 不应触发任何防抖回复投递。
time.Sleep(MessageDebounceDelay + 200*time.Millisecond)
h.mu.Lock()
// chats 保存chats,供当前处理流程使用
// chats 用于本次流程后续判断的chats
chats := len(h.chats)
h.mu.Unlock()
if chats != 0 {
@@ -51,12 +51,12 @@ func (s *systemCapturingHandler) HandleSystemEvent(_ context.Context, task autom
// TestHandleMessage_SystemEventDispatchedToHandler 系统卡片经 handleMessage 进入 handler.HandleSystemEvent。
func TestHandleMessage_SystemEventDispatchedToHandler(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := &systemCapturingHandler{}
acc.handler = h
acc.handleMessage(mustPaidDeliveryCard(t))
@@ -73,7 +73,7 @@ func TestHandleMessage_SystemEventDispatchedToHandler(t *testing.T) {
// TestHandleMessage_PlainChatRoutesToDebounce 普通聊天消息进入防抖回复链。
func TestHandleMessage_PlainChatRoutesToDebounce(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -95,7 +95,7 @@ func TestHandleMessage_PlainChatRoutesToDebounce(t *testing.T) {
// TestHandleMessage_ContentType14Filtered contentType=14 系统提示不进入回复链。
func TestHandleMessage_ContentType14Filtered(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -111,7 +111,7 @@ func TestHandleMessage_ContentType14Filtered(t *testing.T) {
// TestHandleMessage_RefundCardFiltered contentType=26 退款卡片不进入回复链。
func TestHandleMessage_RefundCardFiltered(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -127,12 +127,12 @@ func TestHandleMessage_RefundCardFiltered(t *testing.T) {
// TestHandleMessage_DedupSkipsRepeat 同一 msgID 的重复消息只处理一次。
func TestHandleMessage_DedupSkipsRepeat(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// decrypted 保存decrypted,供当前处理流程使用
// decrypted 用于本次流程后续判断的decrypted
decrypted := plainChatMessage(t, "重复消息", "buyer1", "chat-dup")
acc.handleMessage(decrypted)
acc.handleMessage(decrypted) // 重复
@@ -166,7 +166,7 @@ func TestHandleMessage_IgnoresOwnWebSocketEcho(t *testing.T) {
// TestHandleMessage_NoReminderNoOp 无 reminderContent 的消息不进入回复链也不报错。
func TestHandleMessage_NoReminderNoOp(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -191,7 +191,7 @@ func TestHandleMessage_NoReminderNoOp(t *testing.T) {
// TestDispatch_UpdatesLastMsgReceived dispatch 记录消息接收时间。
func TestDispatch_UpdatesLastMsgReceived(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -200,14 +200,14 @@ func TestDispatch_UpdatesLastMsgReceived(t *testing.T) {
acc.lastMsgReceived = time.Time{}
acc.mu.Unlock()
// before 保存before,供当前处理流程使用
// before 用于本次流程后续判断的before
before := time.Now()
acc.dispatch(plainChatMessage(t, "hi", "b1", "c1"))
// dispatch 内部起 goroutine,等其更新 lastMsgReceived。
time.Sleep(50 * time.Millisecond)
acc.mu.Lock()
// last 保存last,供当前处理流程使用
// last 用于本次流程后续判断的last
last := acc.lastMsgReceived
acc.mu.Unlock()
if last.IsZero() || last.Before(before) {
@@ -217,7 +217,7 @@ func TestDispatch_UpdatesLastMsgReceived(t *testing.T) {
// TestDispatch_SemaphoreOverflowDrops 消息并发达上限时丢弃多余消息(不阻塞)。
func TestDispatch_SemaphoreOverflowDrops(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -244,22 +244,22 @@ func TestDispatch_SemaphoreOverflowDrops(t *testing.T) {
}
}
// TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping 负责TestDispatch系统EventWaitsForCapacityInsteadOfDropping相关处理。
// TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping 封装TestDispatch系统EventWaitsForCapacityInsteadOfDropping业务协调。
func TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := &systemCapturingHandler{}
acc.handler = h
for // i 保存i,供当前处理流程使用
for // i 用于本次流程后续判断的i
i := 0; i < MessageSemaphoreSize; i++ {
acc.sem <- struct{}{}
}
// event 保存event,供当前处理流程使用
// event 用于本次流程后续判断的event
event := mustPaidDeliveryCard(t)
// done 保存done,供当前处理流程使用
// done 用于本次流程后续判断的done
done := make(chan struct{})
go func() {
acc.dispatch(event)
@@ -276,11 +276,11 @@ func TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping(t *testing.T) {
case <-time.After(time.Second):
t.Fatal("system event did not continue after capacity became available")
}
// deadline 保存deadline,供当前处理流程使用
// deadline 用于本次流程后续判断的deadline
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
h.mu.Lock()
// count 保存数量,供当前处理流程使用
// count 用于本次流程后续判断的数量
count := len(h.tasks)
h.mu.Unlock()
if count == 1 {
@@ -289,7 +289,7 @@ func TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping(t *testing.T) {
time.Sleep(10 * time.Millisecond)
}
h.mu.Lock()
// count 保存数量,供当前处理流程使用
// count 用于本次流程后续判断的数量
count := len(h.tasks)
h.mu.Unlock()
if count != 1 {
@@ -302,14 +302,14 @@ func TestDispatch_SystemEventWaitsForCapacityInsteadOfDropping(t *testing.T) {
// TestScheduleDebouncedReply_DifferentChatIDsNotCoalesced 不同 chat_id 的消息各自独立防抖,不合并。
func TestScheduleDebouncedReply_DifferentChatIDsNotCoalesced(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// chat1 保存chat1,供当前处理流程使用
// chat1 用于本次流程后续判断的chat1
chat1 := ChatMessage{AccountID: "cid", ChatID: "chat-a", Text: "A1", SenderUserID: "b1"}
// chat2 保存chat2,供当前处理流程使用
// chat2 用于本次流程后续判断的chat2
chat2 := ChatMessage{AccountID: "cid", ChatID: "chat-b", Text: "B1", SenderUserID: "b2"}
chat1.CookieStr = "cookie"
chat2.CookieStr = "cookie"
@@ -323,7 +323,7 @@ func TestScheduleDebouncedReply_DifferentChatIDsNotCoalesced(t *testing.T) {
if len(h.chats) != 2 {
t.Fatalf("不同 chat_id 应各自投递,got %d 条: %+v", len(h.chats), h.chats)
}
// texts 保存texts,供当前处理流程使用
// texts 用于本次流程后续判断的texts
texts := map[string]bool{}
// c 表示当前遍历过程中的c
for _, c := range h.chats {
@@ -336,7 +336,7 @@ func TestScheduleDebouncedReply_DifferentChatIDsNotCoalesced(t *testing.T) {
// TestScheduleDebouncedReply_TimerReset 同一 chat_id 连续消息重置定时器,旧消息被跳过。
func TestScheduleDebouncedReply_TimerReset(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -348,7 +348,7 @@ func TestScheduleDebouncedReply_TimerReset(t *testing.T) {
// 在防抖延迟内再投递第二条,应取消第一条的定时器。
time.Sleep(MessageDebounceDelay / 2)
// second 保存second,供当前处理流程使用
// second 用于本次流程后续判断的second
second := ChatMessage{AccountID: "cid", ChatID: "chat-reset", Text: "second", SenderUserID: "b1"}
second.CookieStr = "cookie"
acc.scheduleDebouncedReply(second)
@@ -366,11 +366,11 @@ func TestScheduleDebouncedReply_TimerReset(t *testing.T) {
// TestScheduleDebouncedReply_StopClearsTimers Stop 后未触发的定时器被取消,不再投递。
func TestScheduleDebouncedReply_StopClearsTimers(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
// acc、h、cleanup 用于本次流程后续判断的acc、h、cleanup
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
// chat 保存聊天,供当前处理流程使用
// chat 用于本次流程后续判断的聊天
chat := ChatMessage{AccountID: "cid", ChatID: "chat-stop", Text: "won't deliver", SenderUserID: "b1"}
chat.CookieStr = "cookie"
acc.scheduleDebouncedReply(chat)
@@ -385,7 +385,7 @@ func TestScheduleDebouncedReply_StopClearsTimers(t *testing.T) {
}
}
// cancelAwareHandler 保存取消AwareHandler,供当前处理流程使用
// cancelAwareHandler 用于本次流程后续判断的取消AwareHandler
type cancelAwareHandler struct {
started chan struct{}
canceled chan struct{}
@@ -402,20 +402,20 @@ func (h *cancelAwareHandler) HandleChatMessage(ctx context.Context, _ ChatMessag
// HandleSystemEvent 处理系统Event。
func (h *cancelAwareHandler) HandleSystemEvent(context.Context, automation.Task) error { return nil }
// OnPasswordLoginRefresh 负责On密码登录Refresh相关处理。
// OnPasswordLoginRefresh 封装On密码登录Refresh业务协调。
func (h *cancelAwareHandler) OnPasswordLoginRefresh(context.Context, string) bool { return false }
// OnAccountAlert 负责On账号Alert相关处理。
// OnAccountAlert 封装On账号Alert业务协调。
func (h *cancelAwareHandler) OnAccountAlert(context.Context, string, string, string, string) {}
// TestStopCancelsInFlightReplyHandler 负责TestStopCancelsInFlight回复Handler相关处理。
// TestStopCancelsInFlightReplyHandler 封装TestStopCancelsInFlight回复Handler业务协调。
func TestStopCancelsInFlightReplyHandler(t *testing.T) {
// handler 保存handler,供当前处理流程使用
// handler 用于本次流程后续判断的handler
handler := &cancelAwareHandler{started: make(chan struct{}), canceled: make(chan struct{})}
// acc 保存acc,供当前处理流程使用
// acc 用于本次流程后续判断的acc
acc := New(Config{CookieID: "cid", CookieStr: "unb=1", Handler: handler})
acc.reply = nil
// ctx、cancel 保存ctx、cancel,供当前处理流程使用
// ctx、cancel 用于本次流程后续判断的ctx、cancel
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
acc.lifecycle.start(ctx, cancel)
@@ -425,7 +425,7 @@ func TestStopCancelsInFlightReplyHandler(t *testing.T) {
case <-time.After(MessageDebounceDelay + time.Second):
t.Fatal("handler did not start")
}
// done 保存done,供当前处理流程使用
// done 用于本次流程后续判断的done
done := make(chan struct{})
go func() {
acc.Stop()
@@ -445,12 +445,12 @@ func TestStopCancelsInFlightReplyHandler(t *testing.T) {
// TestCleanupDedupLocked_ExpiredRemoved 过期消息 ID 被清理。
func TestCleanupDedupLocked_ExpiredRemoved(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// now 保存now,供当前处理流程使用
// now 用于本次流程后续判断的now
now := time.Now()
acc.dedupMu.Lock()
// 一条过期(>1h)、一条新鲜。
@@ -464,11 +464,11 @@ func TestCleanupDedupLocked_ExpiredRemoved(t *testing.T) {
acc.dedupMu.Lock()
defer acc.dedupMu.Unlock()
if // ok 保存ok,供当前处理流程使用
if // ok 用于本次流程后续判断的ok
_, ok := acc.processed["old-msg"]; ok {
t.Error("过期消息应被清理")
}
if // ok 保存ok,供当前处理流程使用
if // ok 用于本次流程后续判断的ok
_, ok := acc.processed["fresh-msg"]; !ok {
t.Error("新鲜消息不应被清理")
}
@@ -476,23 +476,23 @@ func TestCleanupDedupLocked_ExpiredRemoved(t *testing.T) {
// TestCleanupDedupLocked_OverLimitDropsOldestHalf 超上限且都未过期时删最旧一半。
func TestCleanupDedupLocked_OverLimitDropsOldestHalf(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// now 保存now,供当前处理流程使用
// now 用于本次流程后续判断的now
now := time.Now()
acc.dedupMu.Lock()
// 填入 ProcessedIDsMaxSize + 10 条新鲜消息,时间递增。
total := ProcessedIDsMaxSize + 10
for // i 保存i,供当前处理流程使用
for // i 用于本次流程后续判断的i
i := 0; i < total; i++ {
// 时间按 i 递增,确保有明确的最旧一半。
acc.processed["msg-"+itoa(i)] = now.Add(time.Duration(i) * time.Millisecond)
}
acc.cleanupDedupLocked(now)
// remaining 保存remaining,供当前处理流程使用
// remaining 用于本次流程后续判断的remaining
remaining := len(acc.processed)
acc.dedupMu.Unlock()
@@ -508,14 +508,14 @@ func itoa(n int) string {
if n == 0 {
return "0"
}
// neg 保存neg,供当前处理流程使用
// neg 用于本次流程后续判断的neg
neg := n < 0
if neg {
n = -n
}
// b 保存b,供当前处理流程使用
// b 用于本次流程后续判断的b
var b [20]byte
// i 保存i,供当前处理流程使用
// i 用于本次流程后续判断的i
i := len(b)
for n > 0 {
i--
@@ -531,12 +531,12 @@ func itoa(n int) string {
// TestCleanupDedupLocked_TriggeredByMarkAndCheck markAndCheckDedup 在超上限时触发清理。
func TestCleanupDedupLocked_TriggeredByMarkAndCheck(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
// acc、cleanup 用于本次流程后续判断的acc、cleanup
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// now 保存now,供当前处理流程使用
// now 用于本次流程后续判断的now
now := time.Now()
acc.dedupMu.Lock()
// 预填 ProcessedIDsMaxSize 条过期消息,使下一次 markAndCheckDedup 触发清理。
@@ -547,14 +547,14 @@ func TestCleanupDedupLocked_TriggeredByMarkAndCheck(t *testing.T) {
// 构造一条新消息触发清理。msgID 由 extractMessageID 提取,这里直接用现成样本。
decrypted := plainChatMessage(t, "trigger", "b1", "c1")
// chat 保存聊天,供当前处理流程使用
// chat 用于本次流程后续判断的聊天
chat := extractChatMessage(decrypted, "cid", "cookie")
if !acc.markAndCheckDedup(decrypted, chat) {
t.Fatal("新消息应允许处理")
}
acc.dedupMu.Lock()
// remaining 保存remaining,供当前处理流程使用
// remaining 用于本次流程后续判断的remaining
remaining := len(acc.processed)
acc.dedupMu.Unlock()
// 过期消息应被清理,仅剩新消息 1 条(或新消息+残留,但应远小于 ProcessedIDsMaxSize)。
@@ -565,7 +565,7 @@ func TestCleanupDedupLocked_TriggeredByMarkAndCheck(t *testing.T) {
// TestScheduleDebouncedReply_HandlerErrorLogged reply.Handle 报错仅记录日志,不影响 handler 投递。
func TestScheduleDebouncedReply_HandlerErrorLogged(t *testing.T) {
// acc、h、store、cleanup 保存acc、h、store、cleanup,供当前处理流程使用
// acc、h、store、cleanup 用于本次流程后续判断的acc、h、store、cleanup
acc, h, store, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -573,7 +573,7 @@ func TestScheduleDebouncedReply_HandlerErrorLogged(t *testing.T) {
// 让 reply.Handle 走默认回复路径并成功发送(sender 是 acc 自身,无 conn 会失败但仅记录日志)。
// 这里主要验证防抖回调路径在 reply 报错时仍会调用 handler.HandleChatMessage。
_ = store
// chat 保存聊天,供当前处理流程使用
// chat 用于本次流程后续判断的聊天
chat := ChatMessage{
AccountID: "cid", ChatID: "chat-err", Text: "hi",
SenderUserID: "b1", CookieStr: "unb=123;",
@@ -589,14 +589,14 @@ func TestScheduleDebouncedReply_HandlerErrorLogged(t *testing.T) {
// plainChatMessage 构造一条普通聊天消息(非系统卡片、contentType 非 14/26),
// 含 bizTag.messageId 以便去重提取。
// plainChatMessage 负责plain聊天消息相关处理。
// plainChatMessage 封装plain聊天消息业务协调。
func plainChatMessage(t *testing.T, text, senderUserID, chatID string) map[string]any {
t.Helper()
// s 保存s,供当前处理流程使用
// s 用于本次流程后续判断的s
s := `{"1":{"2":"` + chatID + `@goofish","10":{"bizTag":"{\"messageId\":\"msg-` + chatID + `\"}","reminderContent":"` + text + `","senderUserId":"` + senderUserID + `","senderNick":"买家","reminderUrl":"fleamarket://message_chat?itemId=item-` + chatID + `&peerUserId=` + senderUserID + `"}}}`
// m 保存m,供当前处理流程使用
// m 用于本次流程后续判断的m
var m map[string]any
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("unmarshal: %v", err)
}