-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast_test.go
189 lines (166 loc) · 5.14 KB
/
broadcast_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package broadcast
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
t.Run("assert default values", func(t *testing.T) {
hub := New()
assert.Equal(t, 5, hub.concurrency)
assert.Equal(t, make(map[ID][]Subscriber), hub.subs)
assert.Equal(t, true, hub.allowDuplicableID)
})
t.Run("assert constructed values", func(t *testing.T) {
hub := New(
Concurrency(100),
AllowDuplicableID(false),
)
assert.Equal(t, 100, hub.concurrency)
assert.Equal(t, false, hub.allowDuplicableID)
})
}
func TestSetSubs(t *testing.T) {
hub := New()
assert.NotPanics(t, func() {
for i := 0; i < 100; i++ {
go func() {
hub.setSubs("some id", []Subscriber{})
}()
}
})
}
func TestClearSubs(t *testing.T) {
hub := New()
assert.NotPanics(t, func() {
for i := 0; i < 300; i++ {
go func() {
hub.clearSubs()
}()
}
})
}
func TestSubscribe(t *testing.T) {
t.Run("if duplicable id is not allowed and is same id", func(t *testing.T) {
hub := New(AllowDuplicableID(false))
t.Run("override old one by new one", func(t *testing.T) {
subscriber1 := &stabSubscriber{id: "id1"}
subscriber2 := &stabSubscriber{id: "id1"}
assert.NoError(t, hub.Subscribe(subscriber1))
assert.Equal(t, []Subscriber{subscriber1}, hub.subs["id1"])
assert.NoError(t, hub.Subscribe(subscriber2))
assert.Equal(t, []Subscriber{subscriber2}, hub.subs["id1"])
})
t.Run("if the Unsubscribe method returns an error, returns an error", func(t *testing.T) {
subscriber1 := &stabSubscriber{id: "id1", unsubscribeErr: errors.New("error")}
subscriber2 := &stabSubscriber{id: "id1"}
assert.NoError(t, hub.Subscribe(subscriber1))
assert.Equal(t, []Subscriber{subscriber1}, hub.subs["id1"])
assert.Error(t, hub.Subscribe(subscriber2))
assert.Equal(t, []Subscriber{subscriber1}, hub.subs["id1"])
})
})
t.Run("if duplicable id is allowed", func(t *testing.T) {
hub := New(AllowDuplicableID(true))
t.Run("can append subscribers as same id", func(t *testing.T) {
subscriber1 := &stabSubscriber{id: "id1"}
subscriber2 := &stabSubscriber{id: "id1"}
assert.NoError(t, hub.Subscribe(subscriber1))
assert.Equal(t, []Subscriber{subscriber1}, hub.subs["id1"])
assert.NoError(t, hub.Subscribe(subscriber2))
assert.Equal(t, []Subscriber{subscriber1, subscriber2}, hub.subs["id1"])
})
t.Run("if the Subscribe method returns an error, return an error", func(t *testing.T) {
subscriber1 := &stabSubscriber{id: "id1", subscribeErr: errors.New("error")}
assert.Error(t, hub.Subscribe(subscriber1))
})
})
}
func TestUnsubscribe(t *testing.T) {
hub := New()
subscriber1 := &stabSubscriber{id: "id1"}
subscriber2 := &stabSubscriber{id: "id1"}
subscriber3 := &stabSubscriber{id: "id1"}
assert.NoError(t, hub.Subscribe(subscriber1))
assert.NoError(t, hub.Subscribe(subscriber2))
assert.Equal(t, []Subscriber{subscriber1, subscriber2}, hub.subs["id1"])
assert.NoError(t, hub.Unsubscribe(subscriber3))
assert.Equal(t, []Subscriber{subscriber1, subscriber2}, hub.subs["id1"])
assert.NoError(t, hub.Unsubscribe(subscriber2))
assert.Equal(t, []Subscriber{subscriber1}, hub.subs["id1"])
t.Run("if the Unsubscribe method of Subscriber returns an error, returns an error", func(t *testing.T) {
hub := New()
subscriberErr := &stabSubscriber{id: "id1", unsubscribeErr: errors.New("error")}
assert.NoError(t, hub.Subscribe(subscriberErr))
assert.Error(t, hub.Unsubscribe(subscriberErr))
})
}
func ExampleHub_Publish() {
hub := New()
hub.Subscribe(&stabSubscriber{id: "id1"})
hub.Subscribe(&stabSubscriber{id: "id2"})
hub.Subscribe(&stabSubscriber{id: "id3"})
hub.Publish(func(s Subscriber) error {
fmt.Println(s.(*stabSubscriber).id)
return nil
})
// Unordered output: id1
// id2
// id3
}
func TestPublish(t *testing.T) {
hub := New()
hub.Subscribe(&stabSubscriber{id: "id1"})
err := hub.Publish(func(s Subscriber) error {
return errors.New("error")
})
assert.Error(t, err)
}
func TestPublishTo(t *testing.T) {
hub := New()
hub.Subscribe(&stabSubscriber{id: "id1"})
hub.Subscribe(&stabSubscriber{id: "id2"})
hub.Subscribe(&stabSubscriber{id: "id3"})
assert.NoError(t, hub.PublishTo(func(s Subscriber) error {
switch s.(*stabSubscriber).id {
case "id1", "id2":
return nil
default:
return errors.New("error")
}
}, "id1", "id2"))
assert.Error(t, hub.PublishTo(func(s Subscriber) error {
return errors.New("error")
}, "id1", "id2"))
}
func ExampleHub_Terminate() {
hub := New()
hub.Subscribe(&stabSubscriber{id: "id1"})
hub.Subscribe(&stabSubscriber{id: "id2"})
hub.Subscribe(&stabSubscriber{id: "id3"})
hub.Terminate()
// Output:
//
// unsubscribe: id1
// unsubscribe: id2
// unsubscribe: id3
}
func TestTerminate(t *testing.T) {
hub := New()
hub.Subscribe(&stabSubscriber{id: "id1", unsubscribeErr: errors.New("error")})
assert.Error(t, hub.Terminate())
}
type stabSubscriber struct {
id string
subscribeErr error
unsubscribeErr error
}
func (s stabSubscriber) GetID() ID { return s.id }
func (s stabSubscriber) Subscribe() error {
return s.subscribeErr
}
func (s stabSubscriber) Unsubscribe() error {
fmt.Println("unsubscribe:", s.id)
return s.unsubscribeErr
}