perf: batch group unread fanout

This commit is contained in:
richjohnrambo
2026-09-27 23:10:28 +08:00
parent fc875cc84e
commit 6b8ed4a2cc
10 changed files with 260 additions and 4 deletions
+54
View File
@@ -109,6 +109,60 @@ func TestRealtimeUserPresenceSkipsSessionsAttachedToTopic(t *testing.T) {
}
}
func TestRealtimeGroupUnreadBatchPreservesDetachedMultiDeviceNotification(t *testing.T) {
oldStore := globals.sessionStore
defer func() { globals.sessionStore = oldStore }()
tenantID := types.TenantID(1)
recipient := types.Uid(1002)
other := types.Uid(1003)
topic := "grpUnreadBatchTest"
globals.sessionStore = realtimeTestSessionStore(
realtimeTestSession("sid-attached", tenantID, recipient, topic),
realtimeTestSession("sid-detached-1", tenantID, recipient, ""),
realtimeTestSession("sid-detached-2", tenantID, recipient, ""),
realtimeTestSession("sid-other", tenantID, other, ""),
)
payload := realtimeGroupUnreadMessage{
Topic: topic,
Actor: types.Uid(1001).UserId(),
SeqID: 51,
Recipients: []realtimeGroupUnreadRecipient{
{User: recipient.UserId(), Source: topic},
},
}
raw, err := json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
deliverRealtimeFanoutEnvelope(&store.RealtimeFanoutEnvelope{
TenantID: tenantID,
Kind: realtimeKindGroupUnread,
TargetType: realtimeTargetUsers,
Target: topic,
Message: raw,
})
if len(globals.sessionStore.Get("sid-attached").send) != 0 {
t.Fatal("attached session must rely on the topic data broadcast")
}
for _, sid := range []string{"sid-detached-1", "sid-detached-2"} {
select {
case got := <-globals.sessionStore.Get(sid).send:
pres := got.(*ServerComMessage).Pres
if pres == nil || pres.What != "msg" || pres.Src != topic || pres.SeqId != 51 || pres.SkipTopic != topic {
t.Fatalf("%s got unexpected presence: %#v", sid, pres)
}
default:
t.Fatalf("detached session %s must receive unread presence", sid)
}
}
if len(globals.sessionStore.Get("sid-other").send) != 0 {
t.Fatal("non-recipient session received unread presence")
}
}
func TestRealtimeP2PTopicFallbackRewritesVisibleTopic(t *testing.T) {
oldStore := globals.sessionStore
oldHub := globals.hub