forked from zeromq/goczmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
265 lines (213 loc) · 4.98 KB
/
proxy_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"
"testing"
)
func TestProxy(t *testing.T) {
// Create and configure our proxy
proxy := NewProxy()
defer proxy.Destroy()
var err error
if testing.Verbose() {
err = proxy.Verbose()
if err != nil {
t.Error(err)
}
}
err = proxy.SetFrontend(Pull, "inproc://frontend")
if err != nil {
t.Error(err)
}
err = proxy.SetBackend(Push, "inproc://backend")
if err != nil {
t.Error(err)
}
err = proxy.SetCapture("inproc://capture")
if err != nil {
t.Error(err)
}
// connect application sockets to proxy
faucet := NewSock(Push)
err = faucet.Connect("inproc://frontend")
if err != nil {
t.Error(err)
}
defer faucet.Destroy()
sink := NewSock(Pull)
err = sink.Connect("inproc://backend")
if err != nil {
t.Error(err)
}
defer sink.Destroy()
tap := NewSock(Pull)
_, err = tap.Bind("inproc://capture")
if err != nil {
t.Error(err)
}
defer tap.Destroy()
// send some messages and check they arrived
err = faucet.SendFrame([]byte("Hello"), FlagNone)
if err != nil {
t.Error(err)
}
err = faucet.SendFrame([]byte("World"), FlagNone)
if err != nil {
t.Error(err)
}
// check the tap
b, f, err := tap.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := "Hello", string(b); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
b, f, err = tap.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := "World", string(b); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
b, f, err = sink.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := "Hello", string(b); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
_, f, err = sink.RecvFrame()
if err != nil {
t.Error(err)
}
if f == FlagMore {
t.Error("FlagMore set and should not be")
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
err = proxy.Pause()
if err != nil {
t.Error(err)
}
err = faucet.SendFrame([]byte("Belated Hello"), FlagNone)
if err != nil {
t.Error(err)
}
if want, have := false, sink.Pollin(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := false, tap.Pollin(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
err = proxy.Resume()
if err != nil {
t.Error(err)
}
b, f, err = sink.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := "Belated Hello", string(b); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
b, f, err = tap.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := false, f == FlagMore; want != have {
t.Errorf("want %#v, have %#v", want, have)
}
if want, have := "Belated Hello", string(b); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
proxy.Destroy()
}
func ExampleProxy() {
proxy := NewProxy()
defer proxy.Destroy()
// set front end address and socket type
err := proxy.SetFrontend(Pull, "inproc://frontend")
if err != nil {
panic(err)
}
// set back end address and socket type
err = proxy.SetBackend(Push, "inproc://backend")
if err != nil {
panic(err)
}
// set address for "tee"ing proxy traffic to
err = proxy.SetCapture("inproc://capture")
if err != nil {
panic(err)
}
// we can pause the proxy
err = proxy.Pause()
if err != nil {
panic(err)
}
// and we can resume it
err = proxy.Resume()
if err != nil {
panic(err)
}
proxy.Destroy()
}
func benchmarkProxySendFrame(size int, b *testing.B) {
proxy := NewProxy()
defer proxy.Destroy()
err := proxy.SetFrontend(Pull, fmt.Sprintf("inproc://benchProxyFront%d", size))
if err != nil {
panic(err)
}
err = proxy.SetBackend(Push, fmt.Sprintf("inproc://benchProxyBack%d", size))
if err != nil {
panic(err)
}
pullSock := NewSock(Pull)
defer pullSock.Destroy()
err = pullSock.Connect(fmt.Sprintf("inproc://benchProxyBack%d", size))
if err != nil {
panic(err)
}
go func() {
pushSock := NewSock(Push)
defer pushSock.Destroy()
err := pushSock.Connect(fmt.Sprintf("inproc://benchProxyFront%d", size))
if err != nil {
panic(err)
}
payload := make([]byte, size)
for i := 0; i < b.N; i++ {
err = pushSock.SendFrame(payload, FlagNone)
if err != nil {
panic(err)
}
}
}()
for i := 0; i < b.N; i++ {
msg, _, err := pullSock.RecvFrame()
if err != nil {
panic(err)
}
if len(msg) != size {
panic("msg too small")
}
b.SetBytes(int64(size))
}
}
func BenchmarkProxySendFrame1k(b *testing.B) { benchmarkProxySendFrame(1024, b) }
func BenchmarkProxySendFrame4k(b *testing.B) { benchmarkProxySendFrame(4096, b) }
func BenchmarkProxySendFrame16k(b *testing.B) { benchmarkProxySendFrame(16384, b) }