58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package chathub
|
|
|
|
import "testing"
|
|
|
|
func TestDisconnectUserClosesAllConnections(t *testing.T) {
|
|
hub := NewHub(nil)
|
|
first := hub.Subscribe("user", 7)
|
|
second := hub.Subscribe("user", 7)
|
|
hub.Subscribe("user", 8)
|
|
hub.Subscribe("admin", 9)
|
|
|
|
hub.DisconnectUser(7)
|
|
for name, ch := range map[string]<-chan *ChatEvent{"first": first, "second": second} {
|
|
select {
|
|
case _, ok := <-ch:
|
|
if ok {
|
|
t.Fatalf("%s connection was not closed", name)
|
|
}
|
|
default:
|
|
t.Fatalf("%s connection did not close immediately", name)
|
|
}
|
|
}
|
|
if got := hub.OnlineCount(); got != 2 {
|
|
t.Fatalf("online connection count = %d, want 2", got)
|
|
}
|
|
if got := hub.OnlineCountByType("user"); got != 1 {
|
|
t.Fatalf("online user connection count = %d, want 1", got)
|
|
}
|
|
if got := hub.OnlineCountByType("admin"); got != 1 {
|
|
t.Fatalf("online admin connection count = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestNotifyAllAdminsRespectsTenantAndPlatformScope(t *testing.T) {
|
|
hub := NewHub(nil)
|
|
tenantA := hub.SubscribeAdmin(1, 101, false)
|
|
tenantB := hub.SubscribeAdmin(2, 202, false)
|
|
platform := hub.SubscribeAdmin(3, 101, true)
|
|
|
|
hub.NotifyAllAdmins(&ChatEvent{
|
|
Type: "conversation_updated",
|
|
ConversationID: 9,
|
|
TenantIDs: []uint64{101},
|
|
})
|
|
|
|
if got := <-tenantA; got.ConversationID != 9 {
|
|
t.Fatalf("tenant A conversation = %d", got.ConversationID)
|
|
}
|
|
if got := <-platform; got.ConversationID != 9 {
|
|
t.Fatalf("platform conversation = %d", got.ConversationID)
|
|
}
|
|
select {
|
|
case event := <-tenantB:
|
|
t.Fatalf("tenant B received cross-tenant event: %+v", event)
|
|
default:
|
|
}
|
|
}
|