-
Notifications
You must be signed in to change notification settings - Fork 95
/
channeler_test.go
265 lines (216 loc) · 6.49 KB
/
channeler_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package goczmq
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestPushPullChanneler(t *testing.T) {
push := NewPushChanneler("inproc://channelerpushpull")
defer push.Destroy()
pull := NewPullChanneler("inproc://channelerpushpull")
defer pull.Destroy()
push.SendChan <- [][]byte{[]byte("hello")}
resp := <-pull.RecvChan
if want, got := "hello", string(resp[0]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
push.SendChan <- [][]byte{[]byte("world")}
resp = <-pull.RecvChan
if want, got := "world", string(resp[0]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
}
func TestPubSubChanneler(t *testing.T) {
pub := NewXPubChanneler("inproc://channelerpubsub")
defer pub.Destroy()
sub := NewSubChanneler("inproc://channelerpubsub", "a,b")
defer sub.Destroy()
confirmXPubSubscriptions(t, pub, 2)
pub.SendChan <- [][]byte{[]byte("a"), []byte("message")}
select {
case resp := <-sub.RecvChan:
topic, message := string(resp[0]), string(resp[1])
if want, got := "a", topic; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
if want, got := "message", message; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case <-time.After(time.Second * 2):
t.Errorf("timeout")
}
pub.SendChan <- [][]byte{[]byte("X"), []byte("message")}
pub.SendChan <- [][]byte{[]byte("b"), []byte("message")}
select {
case resp := <-sub.RecvChan:
topic, message := string(resp[0]), string(resp[1])
if want, got := "b", topic; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
if want, got := "message", message; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case <-time.After(time.Second * 1):
t.Errorf("timeout")
}
sub.Subscribe("c")
sub.Unsubscribe("a")
confirmXPubSubscriptions(t, pub, 2)
pub.SendChan <- [][]byte{[]byte("a"), []byte("message")}
pub.SendChan <- [][]byte{[]byte("c"), []byte("message")}
select {
case resp := <-sub.RecvChan:
topic, message := string(resp[0]), string(resp[1])
if want, got := "c", topic; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
if want, got := "message", message; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case <-time.After(time.Second * 1):
t.Errorf("timeout")
}
}
func TestPubSubChannelerNoInitialSubscription(t *testing.T) {
pub := NewXPubChanneler("inproc://channelerpubsub2")
defer pub.Destroy()
sub := NewSubChanneler("inproc://channelerpubsub2")
defer sub.Destroy()
sub.Subscribe("a")
confirmXPubSubscriptions(t, pub, 1)
pub.SendChan <- [][]byte{[]byte("a"), []byte("message")}
select {
case resp := <-sub.RecvChan:
topic, message := string(resp[0]), string(resp[1])
if want, got := "a", topic; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
if want, got := "message", message; want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case <-time.After(time.Second * 2):
t.Errorf("timeout")
}
}
func TestPubSubChannelerOptionError(t *testing.T) {
sub := NewSubChanneler("inproc://channelerpubsub2", 32)
defer sub.Destroy()
err := <-sub.ErrChan
expected := fmt.Errorf("Don't know how to handle a %T argument to NewSubChanneler", 32)
assertEqual(t, expected.Error(), err.Error())
}
func TestDealerRouterChanneler(t *testing.T) {
dealer := NewDealerChanneler("inproc://channelerdealerrouter")
defer dealer.Destroy()
router := NewRouterChanneler("inproc://channelerdealerrouter")
defer router.Destroy()
dealer.SendChan <- [][]byte{[]byte("hello")}
resp := <-router.RecvChan
if want, got := "hello", string(resp[1]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
resp[1] = []byte("world")
router.SendChan <- resp
resp = <-dealer.RecvChan
if want, got := "world", string(resp[0]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
}
func TestDealerRouterChannelerAttachError(t *testing.T) {
dealer := NewDealerChanneler("bad endpoint")
defer dealer.Destroy()
dealer.SendChan <- [][]byte{[]byte("hello")}
select {
case resp := <-dealer.RecvChan:
if want, got := "world", string(resp[0]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case err := <-dealer.ErrChan:
assertEqual(t, ErrSockAttach, err)
}
}
func TestDealerRouterChannelerEmptyEndpointsError(t *testing.T) {
dealer := NewDealerChanneler("")
defer dealer.Destroy()
dealer.SendChan <- [][]byte{[]byte("hello")}
select {
case resp := <-dealer.RecvChan:
if want, got := "world", string(resp[0]); want != got {
t.Errorf("want '%s', got '%s'", want, got)
}
case err := <-dealer.ErrChan:
assertEqual(t, ErrSockAttachEmptyEndpoints, err)
}
}
func TestDealerChannelerRecvChanIsClosedOnDestroy(t *testing.T) {
test_router := NewRouterChanneler("inproc://channelerouter")
done := make(chan bool, 1)
go func(router *Channeler) {
resp := <-router.RecvChan
if resp != nil {
t.Errorf("expected nil response")
}
done <- true
}(test_router)
go func(router *Channeler) {
time.Sleep(100 * time.Millisecond)
router.Destroy()
}(test_router)
select {
case <-done:
break
case <-time.After(1 * time.Second):
t.Errorf("Router channel is not closed")
}
}
func ExampleChanneler_output() {
// create a dealer channeler
dealer := NewDealerChanneler("inproc://channelerdealerrouter")
defer dealer.Destroy()
// create a router channeler
router := NewRouterChanneler("inproc://channelerdealerrouter")
defer router.Destroy()
// send a hello message
dealer.SendChan <- [][]byte{[]byte("Hello")}
// receive the hello message
request := <-router.RecvChan
// first frame is identity of client - let's append 'World'
// to the message and route it back
request = append(request, []byte("World"))
// send the reply
router.SendChan <- request
// receive the reply
reply := <-dealer.RecvChan
fmt.Printf("%s %s", string(reply[0]), string(reply[1]))
// Output: Hello World
}
func BenchmarkChanneler(b *testing.B) {
r := rand.Int63()
pull := NewPullChanneler(fmt.Sprintf("inproc://benchchanneler-%d", r))
defer pull.Destroy()
go func() {
push := NewPushChanneler(fmt.Sprintf("inproc://benchchanneler-%d", r))
defer push.Destroy()
payload := make([]byte, 1024)
for i := 0; i < b.N; i++ {
push.SendChan <- [][]byte{payload}
}
}()
for i := 0; i < b.N; i++ {
msg := <-pull.RecvChan
if len(msg[0]) != 1024 {
panic("message is corrupt")
}
b.SetBytes(1024)
}
}
func confirmXPubSubscriptions(t *testing.T, pub *Channeler, count int) {
for i := 0; i < count; i++ {
select {
case <-pub.RecvChan:
case <-time.After(time.Second * 2):
t.Errorf("timeout")
}
}
}