forked from dustin/go-broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_observer_test.go
67 lines (52 loc) · 955 Bytes
/
mux_observer_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
package broadcast
import (
"sync"
"testing"
)
func TestMuxBroadcast(t *testing.T) {
wg := sync.WaitGroup{}
mo := NewMuxObserver(0, 0)
defer mo.Close()
b1 := mo.Sub()
defer b1.Close()
b2 := mo.Sub()
defer b2.Close()
for i := 0; i < 5; i++ {
wg.Add(2)
cch1 := make(chan interface{})
b1.Register(cch1)
cch2 := make(chan interface{})
b2.Register(cch2)
go func() {
defer wg.Done()
defer b1.Unregister(cch1)
<-cch1
}()
go func() {
defer wg.Done()
defer b2.Unregister(cch2)
<-cch2
}()
}
go b1.Submit(1)
go b2.Submit(1)
wg.Wait()
}
func TestMuxBroadcastCleanup(t *testing.T) {
mo := NewMuxObserver(0, 0)
b := mo.Sub()
b.Register(make(chan interface{}))
b.Close()
mo.Close()
}
func BenchmarkMuxBrodcast(b *testing.B) {
chout := make(chan interface{})
mo := NewMuxObserver(0, 0)
defer mo.Close()
bc := mo.Sub()
bc.Register(chout)
for i := 0; i < b.N; i++ {
bc.Submit(nil)
<-chout
}
}