Files
Pulse/internal/websocket/hub_concurrency_test.go
rcourtman 822eac9789 Deliver websocket events only to the organization that owns them
The hub kept tenant-scoped delivery for state and alerts, but every
other event went through Broadcast/BroadcastMessage, which sent to all
connected clients. In a multi-tenant install a member of one
organization therefore received another organization's discovery
progress, discovered servers (addresses, ports, versions, hostnames),
node auto-registration and node-change events.

Discovery now carries the org of the monitor that owns it, and setup
and node handlers send through BroadcastMessageToTenant with the
request's org (the setup token's org for auto-registration). The
global BroadcastMessage accepts only instance-wide types (ping,
settingsUpdate, update:progress) and refuses everything else, so a new
caller cannot leak by default. Legacy clients without an org are
registered under the default tenant, so single-org installs receive
these events as before. The unused unscoped alert wrappers are removed.
2026-10-17 09:05:56 +01:00

57 lines
1.1 KiB
Go

package websocket
import (
"strconv"
"sync"
"testing"
"time"
)
func TestHubConcurrentClients(t *testing.T) {
hub := NewHub(nil)
// Start hub processing loop in background
go hub.Run()
const iterations = 200
var wg sync.WaitGroup
wg.Add(3)
// Simulate register/unregister from multiple goroutines
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
client := &Client{
hub: hub,
send: make(chan []byte, 10),
id: "client-register-" + strconv.Itoa(i),
}
hub.register <- client
time.Sleep(time.Microsecond)
hub.unregister <- client
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
hub.BroadcastMessageToTenant("default", Message{Type: "test", Data: map[string]int{"iteration": i}})
time.Sleep(time.Microsecond)
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
hub.SetAllowedOrigins([]string{"http://localhost", "http://example.com"})
time.Sleep(time.Microsecond)
}
}()
wg.Wait()
// Allow hub to process remaining messages
time.Sleep(10 * time.Millisecond)
}