-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventcast_test.go
90 lines (78 loc) · 1.37 KB
/
eventcast_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 eventcast
import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
func TestNoListener(t *testing.T) {
for i := 0; i < 10; i++ {
Broadcast("hello")
}
}
func TestSingleListener(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
Broadcast("hello")
}()
<-Listen("hello")
}
func TestMultipleListeners(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
<-Listen("hello")
wg.Done()
}()
}
time.Sleep(100 * time.Millisecond)
Broadcast("hello")
wg.Wait()
}
func TestBroadcastWithValue(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
data := <-Listen("hello")
assert.Equal(t, "value", data.(string))
wg.Done()
}()
}
time.Sleep(100 * time.Millisecond)
BroadcastWithValue("hello", "value")
wg.Wait()
}
func TestHeyHoo(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(6)
for i := 0; i < 2; i++ {
go func() {
hey := Listen("hey")
hoo := Listen("hoo")
done := Listen("done")
for {
select {
case _, ok := <-hey:
if ok {
wg.Done()
}
case _, ok := <-hoo:
if ok {
wg.Done()
}
case <-done:
wg.Done()
return
}
}
}()
}
time.Sleep(100 * time.Millisecond)
Broadcast("hey")
Broadcast("hoo")
time.Sleep(100 * time.Millisecond)
Broadcast("done")
wg.Wait()
}