mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-04-30 20:40:09 +00:00
Replace string(rune(i)) with strconv.Itoa(i) in hub_concurrency_test.go for generating client IDs. While this is test code and not a production bug, it uses the same incorrect pattern that caused the PR #575 bug. This ensures consistent best practices across the codebase and avoids confusion for developers who might copy this pattern. Related: #575
56 lines
1 KiB
Go
56 lines
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.BroadcastMessage(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)
|
|
}
|