-
Notifications
You must be signed in to change notification settings - Fork 8
/
subscriptions_map_test.go
103 lines (90 loc) · 2.4 KB
/
subscriptions_map_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
package gobayeux
import "testing"
func TestSubscriptionsMap_Add(t *testing.T) {
sm := newSubscriptionsMap()
want := make(chan []Message)
defer close(want)
if err := sm.Add("/foo/bar", want); err != nil {
t.Errorf("expected successful addition but got err %q", err)
}
got, ok := sm.subs["/foo/bar"]
if !ok {
t.Error("channel was not registered properly")
}
if want != got {
t.Error("chan received was not the chan registered")
}
}
func TestSubscriptionsMap_Remove(t *testing.T) {
sm := newSubscriptionsMap()
want := make(chan []Message)
defer close(want)
if err := sm.Add("/foo/bar", want); err != nil {
t.Errorf("unable to add subscription for test: %q", err)
}
if ls := len(sm.subs); ls != 1 {
t.Errorf("expected ls to be 1, got %d", ls)
}
sm.Remove("/foo/bar")
if ls := len(sm.subs); ls != 0 {
t.Errorf("expected ls to be 0, got %d", ls)
}
}
func TestSubscriptionsMap_Get(t *testing.T) {
sm := newSubscriptionsMap()
if _, err := sm.Get("/foo/bar"); err == nil {
t.Error("expected '/foo/bar' to not have a subscription, but had one")
}
want := make(chan []Message)
sm.subs["/foo/bar"] = want
if got, err := sm.Get("/foo/bar"); want != got {
if err != nil {
t.Errorf("expected Get(\"/foo/bar\") to return without error but got %q", err)
} else {
t.Error("chan retrieved was not the chan registered")
}
}
}
func BenchmarkSubscriptionsMapAddToEmpty(b *testing.B) {
for i := 0; i < b.N; i++ {
sm := newSubscriptionsMap()
_ = sm.Add("/foo/bar", nil)
}
}
func BenchmarkSubscriptionsMapAddNewToNonEmpty(b *testing.B) {
for i := 0; i < b.N; i++ {
sm := &subscriptionsMap{
subs: map[Channel](chan []Message){
"/": nil,
"/foo": nil,
"/bar": nil,
"/baz": nil,
"/frob": nil,
"/foo/baz": nil,
"/foo/frob": nil,
"/frob/foo": nil,
"/frob/baz": nil,
"/bar/baz": nil,
"/baz/bar": nil,
"/*": nil,
"/foo/*": nil,
"/bar/*": nil,
"/baz/*": nil,
"/frob/*": nil,
"/foo/baz/*": nil,
"/foo/frob/*": nil,
"/frob/foo/*": nil,
"/frob/baz/*": nil,
"/bar/baz/*": nil,
"/baz/bar/*": nil,
},
}
_ = sm.Add("/foo/bar", nil)
}
}
func BenchmarkSubscriptionsMapAddDuplicate(b *testing.B) {
for i := 0; i < b.N; i++ {
sm := &subscriptionsMap{subs: map[Channel](chan []Message){"/foo/bar": nil}}
_ = sm.Add("/foo/bar", nil)
}
}