-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
presence_memory_test.go
145 lines (120 loc) · 3.42 KB
/
presence_memory_test.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package centrifuge
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func testMemoryPresenceManager(t testing.TB) *MemoryPresenceManager {
n, _ := New(Config{
LogLevel: LogLevelDebug,
LogHandler: func(entry LogEntry) {},
})
require.NoError(t, n.Run())
m, _ := NewMemoryPresenceManager(n, MemoryPresenceManagerConfig{})
return m
}
func TestNewMemoryPresenceManager_RemovePresence(t *testing.T) {
m := testMemoryPresenceManager(t)
defer func() { _ = m.node.Shutdown(context.Background()) }()
require.NotEqual(t, nil, m.presenceHub)
require.NoError(t, m.AddPresence("channel", "uid", &ClientInfo{}))
p, err := m.Presence("channel")
require.NoError(t, err)
require.Equal(t, 1, len(p))
require.NoError(t, m.RemovePresence("channel", "uid", ""))
p, err = m.Presence("channel")
require.NoError(t, err)
require.Equal(t, 0, len(p))
}
func TestMemoryPresenceHub(t *testing.T) {
h := newPresenceHub()
require.Equal(t, 0, len(h.presence))
testCh1 := "channel1"
testCh2 := "channel2"
uid := "uid"
info := &ClientInfo{
UserID: "user",
ClientID: "client",
}
_ = h.add(testCh1, uid, info)
require.Equal(t, 1, len(h.presence))
_ = h.add(testCh2, uid, info)
require.Equal(t, 2, len(h.presence))
stats, err := h.getStats(testCh1)
require.NoError(t, err)
require.Equal(t, 1, stats.NumClients)
require.Equal(t, 1, stats.NumUsers)
// stats for unknown channel must not fail.
stats, err = h.getStats("unknown_channel")
require.NoError(t, err)
require.Equal(t, 0, stats.NumClients)
require.Equal(t, 0, stats.NumUsers)
// remove non existing client ID must not fail.
err = h.remove(testCh1, "unknown_client_id")
require.NoError(t, err)
// valid remove.
err = h.remove(testCh1, uid)
require.NoError(t, err)
// remove non existing channel must not fail.
err = h.remove(testCh1, uid)
require.NoError(t, err)
require.Equal(t, 1, len(h.presence))
p, err := h.get(testCh1)
require.NoError(t, err)
require.Equal(t, 0, len(p))
p, err = h.get(testCh2)
require.NoError(t, err)
require.Equal(t, 1, len(p))
}
func BenchmarkMemoryAddPresence_OneChannel(b *testing.B) {
e := testMemoryPresenceManager(b)
defer func() { _ = e.node.Shutdown(context.Background()) }()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := e.AddPresence("channel", "uid", &ClientInfo{})
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMemoryAddPresence_OneChannel_Parallel(b *testing.B) {
e := testMemoryPresenceManager(b)
defer func() { _ = e.node.Shutdown(context.Background()) }()
b.ResetTimer()
b.SetParallelism(getBenchParallelism())
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := e.AddPresence("channel", "uid", &ClientInfo{})
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkMemoryPresence_OneChannel(b *testing.B) {
e := testMemoryPresenceManager(b)
defer func() { _ = e.node.Shutdown(context.Background()) }()
_ = e.AddPresence("channel", "uid", &ClientInfo{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := e.Presence("channel")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMemoryPresence_OneChannel_Parallel(b *testing.B) {
e := testMemoryPresenceManager(b)
defer func() { _ = e.node.Shutdown(context.Background()) }()
_ = e.AddPresence("channel", "uid", &ClientInfo{})
b.SetParallelism(getBenchParallelism())
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := e.Presence("channel")
if err != nil {
b.Fatal(err)
}
}
})
}