-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub_test.go
65 lines (53 loc) · 1.04 KB
/
hub_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
package hub
import (
"fmt"
"testing"
"time"
)
func data1(ch chan interface{}) {
go func() {
for i := 0; i < 10; i++ {
ch <- i
time.Sleep(time.Second)
}
close(ch)
}()
}
func data2(ch chan interface{}) {
go func() {
for i := 'a'; i < 'z'; i++ {
ch <- string([]byte{byte(i)})
time.Sleep(time.Second)
}
close(ch)
}()
}
type tHandleData struct{}
func (tHandleData) Name() string {
return "tHandleData"
}
func (tHandleData) OnData(data interface{}) interface{} {
fmt.Println("tHandleData:", data)
return data
}
func TestHub(t *testing.T) {
ch1 := make(chan interface{}, 1)
ch2 := make(chan interface{}, 1)
h := newHub(12, 0, &tHandleData{})
h.Add(ch1, nil)
h.Add(ch2, nil)
data1(ch1)
data2(ch2)
time.Sleep(time.Second * 30)
}
func TestRemoveProducer(t *testing.T) {
ch1 := make(chan interface{}, 1)
h := newHub(12, 0)
h.Add(ch1, func() {
t.Log("producer removed")
})
h.Remove(ch1, func() {
t.Log("producer add")
})
time.Sleep(time.Second)
}