-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroker_test.go
228 lines (183 loc) · 6.14 KB
/
broker_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package noise
import (
"reflect"
"testing"
"time"
)
func TestRegister(t *testing.T) {
event := newBroker(4)
subscriber := newSubscriber()
event.Register(NewPeerDetected, subscriber)
event.Register(PeerDisconnected, subscriber)
event.Register(MessageReceived, subscriber)
registered := []struct {
name string
event Event
}{{
name: "New peer",
event: NewPeerDetected,
}, {
name: "Peer Disconnected",
event: PeerDisconnected,
}, {
name: "Message received",
event: MessageReceived,
}}
// Table driven test
// For each expected event
for _, e := range registered {
t.Run(e.name, func(t *testing.T) {
s, ok := event.topics[e.event] // Registered events
subscribed := s.s[0] // first element in event subscribed
if !ok {
t.Errorf("expected event %#v, get registered", e)
}
if !reflect.DeepEqual(subscribed, subscriber) {
t.Errorf("expected event subscriber registered equal to %#v, got %#v", subscriber, s)
}
})
}
}
func TestUnregister(t *testing.T) {
broker := newBroker(4)
subscriber := newSubscriber()
broker.Register(MessageReceived, subscriber)
broker.Register(NewPeerDetected, subscriber)
// Remove self listening from broker events
success := broker.Unregister(MessageReceived, subscriber)
if !success {
t.Errorf("expected success unregister for valid subscriber %v", subscriber)
}
}
func TestUnregisterExpectedLen(t *testing.T) {
broker := newBroker(4)
subscriber := newSubscriber()
broker.Register(MessageReceived, subscriber)
broker.Register(NewPeerDetected, subscriber)
lenListeningSubscribed := len(broker.topics[MessageReceived].s)
// Only NewPeerDetected should be found.
if lenListeningSubscribed == 2 {
t.Errorf("expected MessageReceived event unregistered, got %#v events remaining", lenListeningSubscribed)
}
}
func TestInvalidUnregister(t *testing.T) {
broker := newBroker(4)
subscriber := newSubscriber()
// Remove self listening from broker events
success := broker.Unregister(MessageReceived, subscriber)
if success {
t.Errorf("expected fail unregister for invalid subscriber %v", subscriber)
}
}
func TestTopicAdd(t *testing.T) {
topic := make(topics)
subscribed := newSubscriber()
topic.Add(MessageReceived, subscribed)
topic.Add(PeerDisconnected, subscribed)
topic.Add(NewPeerDetected, subscribed)
m, okMsg := topic[MessageReceived]
p, okPeerDisconnect := topic[PeerDisconnected]
n, okNewPeer := topic[NewPeerDetected]
notFoundKeys := !okMsg || !okNewPeer || !okPeerDisconnect
emptyKeys := m.size == 0 || p.size == 0 || n.size == 0
if notFoundKeys || emptyKeys {
t.Error("expected topics keys contains added events: MessageReceived, PeerDisconnected, NewPeerDetected")
}
}
func TestTopicRemove(t *testing.T) {
topic := make(topics)
subscribed := newSubscriber()
topic.Add(MessageReceived, subscribed)
topic.Add(PeerDisconnected, subscribed)
removed := topic.Remove(MessageReceived, subscribed)
emptyKey := len(topic[MessageReceived].s) == 0
integrityCheck := len(topic[PeerDisconnected].s) > 0
// If subscribed not removed and topic with subscribers has entries
if !removed || !emptyKey || !integrityCheck {
t.Errorf("expected topics MessageReceived not found after remove")
}
}
func TestTopicAddData(t *testing.T) {
topic := make(topics)
subscribed := newSubscriber()
topic.Add(MessageReceived, subscribed)
topicLen := topic[MessageReceived].Len()
subscribers := topic[MessageReceived].Subscribers()
if topicLen == 0 || subscribers[0] != subscribed {
t.Error("expected existing topics MessageReceived with data len > 0")
}
}
func TestTopicDataRemove(t *testing.T) {
topic := make(topics)
subscribed := newSubscriber()
topic.Add(MessageReceived, subscribed)
topic.Remove(MessageReceived, subscribed)
topicLen := topic[MessageReceived].Len()
subscribers := topic[MessageReceived].Subscribers()
if topicLen > 0 && subscribers[0] != nil {
t.Errorf("expected topics MessageReceived not found in data after remove")
}
}
func TestTopicRemoveInvalid(t *testing.T) {
topic := make(topics)
subscribed := newSubscriber()
topic.Add(MessageReceived, subscribed)
topic.Add(PeerDisconnected, subscribed)
topic.Add(NewPeerDetected, subscribed)
// Remove by first time the topic
topic.Remove(NewPeerDetected, subscribed)
// trying to remove an already removed subscriber
removed := topic.Remove(NewPeerDetected, subscribed)
// If subscribed not removed and topic with subscribers has entries
if removed {
t.Error("expected topics NewPeerDetected not found if not registered")
}
}
func TestPublish(t *testing.T) {
var result Signal
subscriber := newSubscriber()
broker := newBroker(4)
session := mockSession(&mockConn{}, PeerAPb)
header1 := header{newPeer(session), NewPeerDetected}
signaling := Signal{header1, ""}
broker.Register(NewPeerDetected, subscriber)
broker.Publish(signaling)
// First to finish wins
// Get first message from channel
// Expected Emit called to set message
select {
case result = <-subscriber.notification:
if string(result.Payload()) != string(signaling.Payload()) {
t.Error("expected message equal result")
}
case <-time.After(1 * time.Second):
// Wait 1 second to receive message
t.Error("expected message received after publish")
t.FailNow() // If fail receiving messages next test will fail too
}
// New message for new topic event
broker.Register(NewPeerDetected, subscriber)
header2 := header{newPeer(session), NewPeerDetected}
signaling = Signal{header2, ""}
// Number of subscribers notified
notified := broker.Publish(signaling)
// Get next message from channel
// Expected Emit called to set message
result = <-subscriber.notification
if result.Type() != NewPeerDetected || notified == 0 {
t.Errorf("expected message type equal to %#v", NewPeerDetected)
}
}
func TestInvalidPublish(t *testing.T) {
broker := newBroker(4)
session := mockSession(&mockConn{}, PeerAPb)
header1 := header{newPeer(session), NewPeerDetected}
signaling := Signal{header1, ""}
// Number of subscribers notified
notified := broker.Publish(signaling)
// Get next message from channel
// Expected Emit called to set message
if notified > 0 {
t.Error("expected notified to 0 subscribers if not topic registered")
}
}