-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_test.go
109 lines (97 loc) · 1.78 KB
/
radio_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
package pubsub
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"sync"
"testing"
"time"
)
var gwg sync.WaitGroup
var gcount = 500
var ncount int = 100
func init() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
func Println(r *Radio) {
time.Sleep(time.Minute)
fmt.Println(r.Event().Content().(int))
}
func waiter(r *Radio, index int) {
wg := r.Listener()
var ev *Event
for {
wg.Wait()
wg = r.Listener()
ev = r.Event()
if ev.Content().(int) == (ncount - 1) {
gwg.Done()
return
}
}
}
func TestRadio_WaitDiscard(t *testing.T) {
r := NewRadio(false)
gwg.Add(gcount)
for i := 0; i < gcount; i++ {
go waiter(r, i)
}
time.Sleep(time.Second)
t1 := time.Now()
for i := 0; i < ncount; i++ {
r.Broadcast(NewEvent(i))
}
gwg.Wait()
r.Close()
fmt.Println("broadcast cost time :", time.Now().Sub(t1))
}
func TestScan(t *testing.T) {
var lock sync.Mutex
t1 := time.Now()
for i := 0; i < gcount; i++ {
for j := 0; j < ncount; j++ {
lock.Lock()
lock.Unlock()
}
}
fmt.Println("broadcast cost time :", time.Now().Sub(t1))
}
func TestRadio_Wait(t *testing.T) {
r := NewRadio(true)
go Println(r)
gwg.Add(gcount)
for i := 0; i < gcount; i++ {
go waiter(r, i)
}
time.Sleep(time.Second)
t1 := time.Now()
for i := 0; i < ncount; i++ {
r.Broadcast(NewEvent(i))
}
gwg.Wait()
r.Close()
fmt.Println("broadcast cost time :", time.Now().Sub(t1))
}
func TestRadioTransform(t *testing.T) {
r := NewRadio(false)
gwg.Add(gcount)
for i := 0; i < gcount; i++ {
go waiter(r, i)
}
go func() {
for i := 0; i < 1000; i++ {
r.Transform()
}
}()
time.Sleep(time.Second)
t1 := time.Now()
for i := 0; i < ncount; i++ {
r.Broadcast(NewEvent(i))
}
gwg.Wait()
r.Close()
fmt.Println("broadcast cost time :", time.Now().Sub(t1))
}