-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbcast_test.go
161 lines (145 loc) · 3.42 KB
/
bcast_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
package bcast
/*
bcast package for Go. Broadcasting on a set of channels.
Copyright © 2013 Alexander I.Grafov <grafov@gmail.com>.
All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
*/
import (
"gopkg.in/fatih/set.v0"
"testing"
"time"
)
// Create new broadcast group.
// Join two members.
func TestNewGroupAndJoin(t *testing.T) {
group := NewGroup()
member1 := group.Join()
member2 := group.Join()
if member1.group != member2.group {
t.Fatal("group for these members must be same")
}
}
// Create new broadcast group.
// Join two members.
// Unjoin first member.
func TestUnjoin(t *testing.T) {
group := NewGroup()
member1 := group.Join()
member2 := group.Join()
if len(group.members) != 2 {
t.Fatal("incorrect length of `out` slice")
}
go group.Broadcast(2 * time.Second)
member1.Close()
if len(group.members) > 1 || group.members[0] != member2 {
t.Fatal("unjoin member does not work")
}
}
// Create new broadcast group.
// Join 12 members.
// Make group broadcast to all group members.
func TestMemberBroadcast(t *testing.T) {
group := NewGroup()
var channels []chan bool
max := 12
broadcaster := 3
for i := 0; i <= max; i++ {
c := make(chan bool)
channels = append(channels, c)
}
for i, c := range channels {
m := group.Join()
go func(i int, group *Group, channel chan bool, member *Member) {
if i == broadcaster {
m.Send(i)
} else {
val := m.Recv()
if val != broadcaster {
t.Fatal("incorrect message received")
}
}
channel <- true
val := m.Recv()
if val != "done" {
t.Fatal("incorrect message received")
}
channel <- true
}(i, group, c, m)
}
go group.Broadcast(0)
for _, channel := range channels {
<-channel
}
group.Send("done")
for _, channel := range channels {
<-channel
}
}
// Create new broadcast group.
// Join 12 members.
// Make group broadcast to all group members.
func TestGroupBroadcast(t *testing.T) {
group := NewGroup()
var channels []chan bool
max := 12
for i := 0; i <= max; i++ {
c := make(chan bool)
channels = append(channels, c)
}
for i, c := range channels {
m := group.Join()
go func(i int, group *Group, channel chan bool, member *Member) {
val := m.Recv()
if val != "group message" {
t.Fatal("incorrect message received")
}
channel <- true
}(i, group, c, m)
}
go group.Broadcast(0)
group.Send("group message")
for _, channel := range channels {
<-channel
}
}
// Create new broadcast group.
// Join 128 members.
// Make group broadcast to all members.
func TestBroadcastOnLargeNumberOfMembers(t *testing.T) {
const max = 128
var channels []chan *set.Set
var members []*Member
expected := set.New()
group := NewGroup()
for i := 0; i <= max; i++ {
expected.Add(i)
m := group.Join()
members = append(members, m)
}
for i, member := range members {
c := make(chan *set.Set)
channels = append(channels, c)
go func(i int, group *Group, channel chan *set.Set, m *Member) {
m.Send(i)
encountered := set.New()
encountered.Add(i) // The message sent by this member wont be received
for {
newValue := m.Recv()
if encountered.Has(newValue) {
t.Fatal("Received duplicate value")
}
encountered.Add(newValue)
if encountered.IsEqual(expected) {
break
}
}
channel <- encountered
}(i, group, c, member)
}
go group.Broadcast(0)
for _, channel := range channels {
<-channel
}
}