-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast_test.go
81 lines (63 loc) · 1.29 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
package main
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBroadcastMultiListener(t *testing.T) {
assert := assert.New(t)
b := NewBroadcaster()
pcheck(b.Start())
wg := sync.WaitGroup{}
started := make(chan bool)
counts := make(chan int, 8)
stdRoutines := 1
for i := 0; i < stdRoutines; i++ {
wg.Add(1)
go func(num int) {
defer wg.Done()
count := 0
t.Logf("LIST %d started\n", num)
li := b.GetListener()
started <- true
for msg := range li.Delivery {
t.Logf("LIST %d: (%v) %s\n", num, msg.CreateTime, msg.Msg)
li.Respond(true)
count++
}
counts <- count
}(i)
<-started
}
pcheck(b.Send("Message A"))
pcheck(b.Send("Message B"))
pcheck(b.Send("Message C"))
diffCount := make(chan int)
wg.Add(1)
go func() {
wg.Done()
count := 0
t.Logf("LIST X started\n")
li := b.GetListener()
started <- true
for msg := range li.Delivery {
t.Logf("LIST X: (%v) %s\n", msg.CreateTime, msg.Msg)
count++
if count >= 4 {
li.Respond(false)
break
}
li.Respond(true)
}
diffCount <- count
}()
<-started
pcheck(b.Send("Message D [last for X]"))
pcheck(b.Send("Message LAST"))
pcheck(b.Kill())
wg.Wait()
for i := 0; i < stdRoutines; i++ {
assert.Equal(5, <-counts)
}
assert.Equal(4, <-diffCount)
}