-
Notifications
You must be signed in to change notification settings - Fork 55
/
zmq4_reqrep_test.go
329 lines (290 loc) · 7.14 KB
/
zmq4_reqrep_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
// Copyright 2018 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"
"reflect"
"testing"
"time"
"github.com/go-zeromq/zmq4"
"golang.org/x/sync/errgroup"
)
var (
reqreps = []testCaseReqRep{
{
name: "tcp-req-rep",
endpoint: must(EndPoint("tcp")),
req1: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "ipc-req-rep",
endpoint: "ipc://ipc-req-rep",
req1: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "inproc-req-rep",
endpoint: "inproc://inproc-req-rep",
req1: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
}
)
type testCaseReqRep struct {
name string
skip bool
endpoint string
req1 zmq4.Socket
req2 zmq4.Socket
rep zmq4.Socket
}
func TestReqRep(t *testing.T) {
var (
reqName = zmq4.NewMsgString("NAME")
reqLang = zmq4.NewMsgString("LANG")
reqQuit = zmq4.NewMsgString("QUIT")
repName = zmq4.NewMsgString("zmq4")
repLang = zmq4.NewMsgString("Go")
repQuit = zmq4.NewMsgString("bye")
)
for i := range reqreps {
tc := reqreps[i]
t.Run(tc.name, func(t *testing.T) {
defer tc.req1.Close()
defer tc.rep.Close()
ep := tc.endpoint
cleanUp(ep)
if tc.skip {
t.Skipf(tc.name)
}
// t.Parallel()
ctx, timeout := context.WithTimeout(context.Background(), 20*time.Second)
defer timeout()
grp, _ := errgroup.WithContext(ctx)
grp.Go(func() error {
err := tc.rep.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
if addr := tc.rep.Addr(); addr == nil {
return fmt.Errorf("listener with nil Addr")
}
loop := true
for loop {
msg, err := tc.rep.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
var rep zmq4.Msg
switch string(msg.Frames[0]) {
case "NAME":
rep = repName
case "LANG":
rep = repLang
case "QUIT":
rep = repQuit
loop = false
}
err = tc.rep.Send(rep)
if err != nil {
return fmt.Errorf("could not send REP message to %v: %w", msg, err)
}
}
return err
})
grp.Go(func() error {
err := tc.req1.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
if addr := tc.req1.Addr(); addr != nil {
return fmt.Errorf("dialer with non-nil Addr")
}
for _, msg := range []struct {
req zmq4.Msg
rep zmq4.Msg
}{
{reqName, repName},
{reqLang, repLang},
{reqQuit, repQuit},
} {
err = tc.req1.Send(msg.req)
if err != nil {
return fmt.Errorf("could not send REQ message %v: %w", msg.req, err)
}
rep, err := tc.req1.Recv()
if err != nil {
return fmt.Errorf("could not recv REP message %v: %w", msg.req, err)
}
if got, want := rep, msg.rep; !reflect.DeepEqual(got, want) {
return fmt.Errorf("got = %v, want= %v", got, want)
}
}
return err
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
})
}
}
func TestMultiReqRepIssue70(t *testing.T) {
var (
reqName1 = zmq4.NewMsgString("NAME")
reqLang1 = zmq4.NewMsgString("LANG")
reqQuit1 = zmq4.NewMsgString("QUIT")
reqName2 = zmq4.NewMsgString("NAME2")
reqLang2 = zmq4.NewMsgString("LANG2")
reqQuit2 = zmq4.NewMsgString("QUIT2")
repName1 = zmq4.NewMsgString("zmq4")
repLang1 = zmq4.NewMsgString("Go")
repQuit1 = zmq4.NewMsgString("bye")
repName2 = zmq4.NewMsgString("zmq42")
repLang2 = zmq4.NewMsgString("Go2")
repQuit2 = zmq4.NewMsgString("bye2")
)
reqreps := []testCaseReqRep{
{
name: "tcp-req-rep",
endpoint: must(EndPoint("tcp")),
req1: zmq4.NewReq(bkg),
req2: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "ipc-req-rep",
endpoint: "ipc://ipc-req-rep",
req1: zmq4.NewReq(bkg),
req2: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "inproc-req-rep",
endpoint: "inproc://inproc-req-rep",
req1: zmq4.NewReq(bkg),
req2: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
}
for i := range reqreps {
tc := reqreps[i]
t.Run(tc.name, func(t *testing.T) {
defer tc.req1.Close()
defer tc.req2.Close()
defer tc.rep.Close()
if tc.skip {
t.Skipf(tc.name)
}
// t.Parallel()
ep := tc.endpoint
cleanUp(ep)
ctx, timeout := context.WithTimeout(context.Background(), 20*time.Second)
defer timeout()
grp, _ := errgroup.WithContext(ctx)
grp.Go(func() error {
err := tc.rep.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
if addr := tc.rep.Addr(); addr == nil {
return fmt.Errorf("listener with nil Addr")
}
loop1, loop2 := true, true
for loop1 || loop2 {
msg, err := tc.rep.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
var rep zmq4.Msg
switch string(msg.Frames[0]) {
case "NAME":
rep = repName1
case "LANG":
rep = repLang1
case "QUIT":
rep = repQuit1
loop1 = false
case "NAME2":
rep = repName2
case "LANG2":
rep = repLang2
case "QUIT2":
rep = repQuit2
loop2 = false
}
err = tc.rep.Send(rep)
if err != nil {
return fmt.Errorf("could not send REP message to %v: %w", msg, err)
}
}
return err
})
grp.Go(func() error {
err := tc.req2.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
if addr := tc.req2.Addr(); addr != nil {
return fmt.Errorf("dialer with non-nil Addr")
}
for _, msg := range []struct {
req zmq4.Msg
rep zmq4.Msg
}{
{reqName2, repName2},
{reqLang2, repLang2},
{reqQuit2, repQuit2},
} {
err = tc.req2.Send(msg.req)
if err != nil {
return fmt.Errorf("could not send REQ message %v: %w", msg.req, err)
}
rep, err := tc.req2.Recv()
if err != nil {
return fmt.Errorf("could not recv REP message %v: %w", msg.req, err)
}
if got, want := rep, msg.rep; !reflect.DeepEqual(got, want) {
return fmt.Errorf("got = %v, want= %v", got, want)
}
}
return err
})
grp.Go(func() error {
err := tc.req1.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
if addr := tc.req1.Addr(); addr != nil {
return fmt.Errorf("dialer with non-nil Addr")
}
for _, msg := range []struct {
req zmq4.Msg
rep zmq4.Msg
}{
{reqName1, repName1},
{reqLang1, repLang1},
{reqQuit1, repQuit1},
} {
err = tc.req1.Send(msg.req)
if err != nil {
return fmt.Errorf("could not send REQ message %v: %w", msg.req, err)
}
rep, err := tc.req1.Recv()
if err != nil {
return fmt.Errorf("could not recv REP message %v: %w", msg.req, err)
}
if got, want := rep, msg.rep; !reflect.DeepEqual(got, want) {
return fmt.Errorf("got = %v, want= %v", got, want)
}
}
return err
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
})
}
}