/
pubsub_test.go
167 lines (136 loc) · 2.67 KB
/
pubsub_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package pubsub
import (
"fmt"
"log"
"runtime"
"sync"
"testing"
rand "github.com/trpedersen/rand"
)
const (
TOPICS = 100
MSG_PER_TOPIC = 100
SUBSCRIBERS_PER_TOPIC = 10 //00
)
func merge(done <-chan struct{}, cs ...<-chan Msg) <-chan Msg {
var wg sync.WaitGroup
out := make(chan Msg)
output := func(c <-chan Msg) {
defer wg.Done()
for {
select {
case msg, ok := <-c:
if ok {
out <- msg
} else {
return
}
case <-done:
return
}
}
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func sink(done chan struct{}, in <-chan Msg) <-chan int {
maxCount := TOPICS * SUBSCRIBERS_PER_TOPIC * MSG_PER_TOPIC
log.Printf("sink: maxCount: %d\n", maxCount)
count := 0
var wg sync.WaitGroup
out := make(chan int)
wg.Add(1)
go func() {
defer wg.Done()
for count < maxCount {
select {
case _, ok := <-in:
if ok {
count += 1
if count%1000 == 0 {
log.Println(count)
}
} else {
return
}
case <-done:
return
}
}
}()
go func() {
wg.Wait()
out <- count
close(out)
}()
return out
}
func consume(done <-chan struct{}, subscription Subscription) <-chan Msg {
out := make(chan Msg)
go func() {
defer close(out)
for {
select {
case msg, ok := <-subscription.Delivery():
if ok {
out <- msg
} else {
return
}
case <-done:
return
}
}
}()
return out
}
func TestPubSub(t *testing.T) {
nCPU := runtime.NumCPU()
log.Println("nCPU: ", nCPU)
//runtime.GOMAXPROCS(nCPU)
hub, _ := NewHub()
done := make(chan struct{})
log.Print("making topics...")
topics := make([]string, 0)
for i := 0; i < TOPICS; i++ {
topics = append(topics, fmt.Sprintf("TOPIC.%d", i))
}
log.Println("...making topics done")
log.Print("making subscriptions...")
deliveries := make([]<-chan Msg, 0)
for _, topic := range topics {
for i := 0; i < SUBSCRIBERS_PER_TOPIC; i++ {
sub, err := hub.Subscribe(topic)
if err != nil {
t.Errorf("hub.Subscribe returned err: %s\n", err)
} else {
deliveries = append(deliveries, sub.Delivery())
}
}
}
log.Println("...making subscriptions done")
sinkC := sink(done, merge(done, deliveries...))
log.Print("publishing topics...")
for _, topic := range topics {
for i := 0; i < MSG_PER_TOPIC; i++ {
bodyStr := rand.RandStr(1000, "alphanum")
body := []byte(bodyStr)
msg := NewMsg(topic, body)
hub.Publish(msg)
}
}
log.Print("...publishing topics done")
count := <-sinkC
log.Print("halting...")
close(done)
hub.Halt()
log.Print("...halted")
log.Printf("stats: %s, messages back: %d\n", hub.Stats(), count)
}