This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast_test.go
90 lines (76 loc) · 2.17 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
package main
import (
"context"
"net"
"testing"
"time"
)
func TestBroadcast(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
src := make(chan ServerList)
broadcast := newBroadcast(ctx, src)
listener := make(chan ServerList)
// adding a listener should not cause trouble and do not trigger the listener
// as long as the broadcast never received a server list
addDummyListeners(ctx, broadcast)
broadcast.addListener(listener)
addDummyListeners(ctx, broadcast)
assertNoDataReceived(listener, t)
// broadcasting should work
srv := Server{net.IP{127, 0, 0, 1}, 1234}
src <- ServerList{srv}
assertDataReceived(listener, t, srv)
// broadcasting the same data twice shouldn't happen
src <- ServerList{srv}
assertNoDataReceived(listener, t)
// broadcasting different data should work
srv = Server{net.IP{127, 0, 0, 1}, 5678}
src <- ServerList{srv}
assertDataReceived(listener, t, srv)
// removing a listener should mean we won't receive further updates
broadcast.remListener(listener)
src <- ServerList{srv}
assertNoDataReceived(listener, t)
// adding the listener should result in known state being sent
broadcast.addListener(listener)
assertDataReceived(listener, t, srv)
}
func addDummyListeners(ctx context.Context, b *broadcast) {
for i := 0; i < 10; i++ {
l := make(chan ServerList)
go func() {
for {
select {
case <-ctx.Done():
return
case <-l:
}
}
}()
b.addListener(l)
}
}
func assertDataReceived(listener chan ServerList, t *testing.T, expected Server) {
select {
case list := <-listener:
if len(list) != 1 {
t.Errorf("expected server list with len 1, got %d", len(list))
}
if list[0].IP.String() != expected.IP.String() {
t.Errorf("expected ip %s, got %s", expected.IP, list[0].IP)
}
if list[0].Port != expected.Port {
t.Errorf("expected port %d, got %d", expected.Port, list[0].Port)
}
case <-time.After(100 * time.Millisecond):
t.Error("expected listener to receive data")
}
}
func assertNoDataReceived(listener chan ServerList, t *testing.T) {
select {
case <-listener:
t.Error("listener received data but shouldn't")
case <-time.After(100 * time.Millisecond):
}
}