-
Notifications
You must be signed in to change notification settings - Fork 56
/
proxy_test.go
336 lines (281 loc) · 7.66 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2020 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4_test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/go-zeromq/zmq4"
"golang.org/x/sync/errgroup"
)
func TestProxy(t *testing.T) {
bkg := context.Background()
ctx, timeout := context.WithTimeout(bkg, 20*time.Second)
defer timeout()
var (
frontIn = zmq4.NewPush(ctx, zmq4.WithLogger(zmq4.Devnull))
front = zmq4.NewPull(ctx, zmq4.WithLogger(zmq4.Devnull))
back = zmq4.NewPush(ctx, zmq4.WithLogger(zmq4.Devnull))
backOut = zmq4.NewPull(ctx, zmq4.WithLogger(zmq4.Devnull))
capt = zmq4.NewPush(ctx, zmq4.WithLogger(zmq4.Devnull))
captOut = zmq4.NewPull(ctx, zmq4.WithLogger(zmq4.Devnull))
proxy *zmq4.Proxy
epFront = "ipc://proxy-front"
epBack = "ipc://proxy-back"
epCapt = "ipc://proxy-capt"
wg1 sync.WaitGroup // all sockets ready
wg2 sync.WaitGroup // proxy setup
wg3 sync.WaitGroup // all messages received
wg4 sync.WaitGroup // all capture messages received
wg5 sync.WaitGroup // terminate sent
wg6 sync.WaitGroup // all sockets done
)
wg1.Add(6) // number of sockets
wg2.Add(1) // proxy ready
wg3.Add(1) // messages received at backout
wg4.Add(1) // capture messages received at capt-out
wg5.Add(1) // terminate
wg6.Add(6) // number of sockets
cleanUp(epFront)
cleanUp(epBack)
cleanUp(epCapt)
var (
msgs = []zmq4.Msg{
zmq4.NewMsgFrom([]byte("msg1")),
zmq4.NewMsgFrom([]byte("msg2")),
zmq4.NewMsgFrom([]byte("msg3")),
zmq4.NewMsgFrom([]byte("msg4")),
}
)
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
defer frontIn.Close()
err := frontIn.Dial(epFront)
if err != nil {
return fmt.Errorf("front-in could not dial %q: %w", epFront, err)
}
wg1.Done()
t.Logf("front-in ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
for _, msg := range msgs {
t.Logf("front-in sending %v...", msg)
err = frontIn.Send(msg)
if err != nil {
return fmt.Errorf("could not send front-in %q: %w", msg, err)
}
t.Logf("front-in sending %v... [done]", msg)
}
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("front-in waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
defer front.Close()
err := front.Listen(epFront)
if err != nil {
return fmt.Errorf("front could not listen %q: %w", epFront, err)
}
wg1.Done()
t.Logf("front ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("front waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
defer back.Close()
err := back.Listen(epBack)
if err != nil {
return fmt.Errorf("back could not listen %q: %w", epBack, err)
}
wg1.Done()
t.Logf("back ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("back waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
defer backOut.Close()
err := backOut.Dial(epBack)
if err != nil {
return fmt.Errorf("back-out could not dial %q: %w", epBack, err)
}
wg1.Done()
t.Logf("back-out ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
for _, want := range msgs {
t.Logf("back-out recving %v...", want)
msg, err := backOut.Recv()
if err != nil {
return fmt.Errorf("back-out could not recv: %w", err)
}
if msg.String() != want.String() {
return fmt.Errorf("invalid message: got=%v, want=%v", msg, want)
}
t.Logf("back-out recving %v... [done]", msg)
}
wg3.Done() // all messages received
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("back-out waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
defer captOut.Close()
err := captOut.Listen(epCapt)
if err != nil {
return fmt.Errorf("capt-out could not listen %q: %w", epCapt, err)
}
wg1.Done()
t.Logf("capt-out ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
wg3.Wait() // all messages received
for _, want := range msgs {
t.Logf("capt-out recving %v...", want)
msg, err := captOut.Recv()
if err != nil {
return fmt.Errorf("capt-out could not recv msg: %w", err)
}
if msg.String() != want.String() {
return fmt.Errorf("capt-out: invalid message: got=%v, want=%v", msg, want)
}
t.Logf("capt-out recving %v... [done]", msg)
}
wg4.Done() // all capture messages received
wg4.Wait() // all capture messages received
t.Logf("capt-out waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
defer capt.Close()
err := capt.Dial(epCapt)
if err != nil {
return fmt.Errorf("capt could not dial %q: %w", epCapt, err)
}
wg1.Done()
t.Logf("capt ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("capt waiting for terminate signal")
wg5.Wait() // terminate
wg6.Done() // all sockets done
wg6.Wait()
return nil
})
grp.Go(func() error {
t.Logf("ctrl ready")
wg1.Wait() // sockets
wg2.Wait() // proxy
for _, cmd := range []struct {
name string
fct func()
}{
{"pause", proxy.Pause},
{"resume", proxy.Resume},
{"stats", proxy.Stats},
} {
t.Logf("ctrl sending %v...", cmd.name)
cmd.fct()
t.Logf("ctrl sending %v... [done]", cmd.name)
}
wg3.Wait() // all messages received
wg4.Wait() // all capture messages received
t.Logf("ctrl sending kill...")
proxy.Kill()
t.Logf("ctrl sending kill... [done]")
wg5.Done()
t.Logf("ctrl waiting for terminate signal")
wg5.Wait() // terminate
wg6.Wait()
return nil
})
grp.Go(func() error {
wg1.Wait() // sockets ready
proxy = zmq4.NewProxy(ctx, front, back, capt)
t.Logf("proxy ready")
wg2.Done()
err := proxy.Run()
t.Logf("proxy done: err=%+v", err)
return err
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
if err := ctx.Err(); err != nil && err != context.Canceled {
t.Fatalf("error: %+v", err)
}
}
func TestProxyStop(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var (
epFront = "ipc://proxy-stop-front"
epBack = "ipc://proxy-stop-back"
frontIn = zmq4.NewPush(ctx, zmq4.WithLogger(zmq4.Devnull))
front = zmq4.NewPull(ctx, zmq4.WithLogger(zmq4.Devnull))
back = zmq4.NewPush(ctx, zmq4.WithLogger(zmq4.Devnull))
backOut = zmq4.NewPull(ctx, zmq4.WithLogger(zmq4.Devnull))
)
cleanUp(epFront)
cleanUp(epBack)
defer front.Close()
defer back.Close()
if err := front.Listen(epFront); err != nil {
t.Fatalf("could not listen: %+v", err)
}
if err := frontIn.Dial(epFront); err != nil {
t.Fatalf("could not dial: %+v", err)
}
if err := back.Listen(epBack); err != nil {
t.Fatalf("could not listen: %+v", err)
}
if err := backOut.Dial(epBack); err != nil {
t.Fatalf("could not dial: %+v", err)
}
var errc = make(chan error)
go func() {
errc <- zmq4.NewProxy(ctx, front, back, nil).Run()
}()
go func() {
_ = frontIn.Send(zmq4.NewMsgString("msg1"))
}()
go func() {
_, _ = backOut.Recv()
}()
cancel()
err := <-errc
if err != context.Canceled {
t.Fatalf("error: %+v", err)
}
if err := ctx.Err(); err != nil && err != context.Canceled {
t.Fatalf("error: %+v", err)
}
}