perf(chat): 使用预聚合快照优化客服会话统计
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func TestAdminConversationCountsUsesFreshSnapshot(t *testing.T) {
|
||||
db := setupAdminListTestDB(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: "127.0.0.1:16379", DB: 15})
|
||||
t.Cleanup(func() {
|
||||
_ = client.Close()
|
||||
})
|
||||
if err := client.Ping(t.Context()).Err(); err != nil {
|
||||
t.Skipf("Redis 测试服务不可用: %v", err)
|
||||
}
|
||||
|
||||
admin := model.AdminUser{Username: "counts-cache", Nickname: "缓存测试", Status: "active"}
|
||||
if err := db.Create(&admin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversation := model.ChatConversation{
|
||||
TenantID: 1,
|
||||
Type: ConversationTypeGeneralSupport,
|
||||
Title: "缓存测试会话",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&conversation).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
participant := model.ChatParticipant{
|
||||
ConversationID: conversation.ID,
|
||||
ParticipantType: "admin",
|
||||
ParticipantID: admin.ID,
|
||||
Role: "support",
|
||||
JoinedAt: time.Now(),
|
||||
}
|
||||
if err := db.Create(&participant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil, client)
|
||||
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: 1})
|
||||
principal := Principal{Type: "admin", ID: admin.ID}
|
||||
cacheKey := repo.adminChatCountsCacheKey(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
t.Cleanup(func() {
|
||||
_ = client.Del(t.Context(), cacheKey).Err()
|
||||
})
|
||||
|
||||
first, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.Ownership[adminChatFilterMine] != 1 {
|
||||
t.Fatalf("首次统计 mine = %d, want 1", first.Ownership[adminChatFilterMine])
|
||||
}
|
||||
|
||||
if err := db.Delete(&participant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cached, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cached.Ownership[adminChatFilterMine] != 1 {
|
||||
t.Fatalf("15 秒内应读取快照,mine = %d, want 1", cached.Ownership[adminChatFilterMine])
|
||||
}
|
||||
|
||||
repo.invalidateAdminChatCounts(ctx, []uint64{1})
|
||||
|
||||
stale, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !stale.Refreshing {
|
||||
t.Fatal("过期快照应标记 refreshing")
|
||||
}
|
||||
|
||||
raw, err := client.Get(t.Context(), cacheKey).Bytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var snapshot adminChatCountsSnapshot
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
raw, err = client.Get(t.Context(), cacheKey).Bytes()
|
||||
if err == nil && json.Unmarshal(raw, &snapshot) == nil && snapshot.Counts.Ownership[adminChatFilterMine] == 0 {
|
||||
return
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("过期快照未在超时前完成后台刷新")
|
||||
}
|
||||
Reference in New Issue
Block a user