-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_manager_test.go
More file actions
124 lines (96 loc) · 2.82 KB
/
websocket_manager_test.go
File metadata and controls
124 lines (96 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"testing"
)
func TestNewWebSocketManager(t *testing.T) {
wsm := NewWebSocketManager()
if wsm == nil {
t.Fatal("NewWebSocketManager() = nil; want non-nil")
}
if wsm.connections == nil {
t.Error("NewWebSocketManager() connections = nil; want non-nil")
}
if len(wsm.connections) != 0 {
t.Errorf("NewWebSocketManager() connections length = %d; want 0", len(wsm.connections))
}
}
func TestWebSocketManagerAdd(t *testing.T) {
wsm := NewWebSocketManager()
// Create a mock safeConn
conn := &safeConn{
conn: nil, // We don't need a real connection for testing
}
wsm.Add(conn)
if len(wsm.connections) != 1 {
t.Errorf("WebSocketManager.Add() connections length = %d; want 1", len(wsm.connections))
}
if !wsm.connections[conn] {
t.Error("WebSocketManager.Add() connection not found in map")
}
}
func TestWebSocketManagerRemove(t *testing.T) {
wsm := NewWebSocketManager()
conn := &safeConn{conn: nil}
// Add then remove
wsm.Add(conn)
if len(wsm.connections) != 1 {
t.Fatal("Setup failed: connection not added")
}
wsm.Remove(conn)
if len(wsm.connections) != 0 {
t.Errorf("WebSocketManager.Remove() connections length = %d; want 0", len(wsm.connections))
}
if wsm.connections[conn] {
t.Error("WebSocketManager.Remove() connection still in map")
}
}
func TestWebSocketManagerCount(t *testing.T) {
wsm := NewWebSocketManager()
if wsm.Count() != 0 {
t.Errorf("WebSocketManager.Count() = %d; want 0", wsm.Count())
}
conn1 := &safeConn{conn: nil}
conn2 := &safeConn{conn: nil}
wsm.Add(conn1)
if wsm.Count() != 1 {
t.Errorf("WebSocketManager.Count() = %d; want 1", wsm.Count())
}
wsm.Add(conn2)
if wsm.Count() != 2 {
t.Errorf("WebSocketManager.Count() = %d; want 2", wsm.Count())
}
wsm.Remove(conn1)
if wsm.Count() != 1 {
t.Errorf("WebSocketManager.Count() = %d; want 1", wsm.Count())
}
}
func TestWebSocketManagerBroadcastShutdown(t *testing.T) {
wsm := NewWebSocketManager()
// Note: BroadcastShutdown requires real websocket connections
// For unit testing, we just verify it doesn't panic with empty connections
// Integration tests would be needed for full coverage
message := "Server shutting down"
// Should not panic even with no connections
wsm.BroadcastShutdown(message)
// With connections, we'd need real WebSocket connections to test properly
// This is better suited for integration tests
}
func TestWebSocketManagerConcurrentAccess(t *testing.T) {
wsm := NewWebSocketManager()
// Test concurrent Add operations
done := make(chan bool, 10)
for i := 0; i < 10; i++ {
go func(id int) {
conn := &safeConn{conn: nil}
wsm.Add(conn)
done <- true
}(i)
}
// Wait for all goroutines
for i := 0; i < 10; i++ {
<-done
}
if wsm.Count() != 10 {
t.Errorf("WebSocketManager concurrent Add() count = %d; want 10", wsm.Count())
}
}