forked from go-zeromq/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq4_reqrep_test.go
153 lines (132 loc) · 3.15 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
// 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"
"reflect"
"testing"
"time"
"github.com/go-zeromq/zmq4"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)
var (
reqreps = []testCaseReqRep{
{
name: "tcp-req-rep",
endpoint: must(EndPoint("tcp")),
req: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "ipc-req-rep",
endpoint: "ipc://ipc-req-rep",
req: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
{
name: "inproc-req-rep",
endpoint: "inproc://inproc-req-rep",
req: zmq4.NewReq(bkg),
rep: zmq4.NewRep(bkg),
},
}
)
type testCaseReqRep struct {
name string
skip bool
endpoint string
req 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.req.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, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := tc.rep.Listen(ep)
if err != nil {
return xerrors.Errorf("could not listen: %w", err)
}
if addr := tc.rep.Addr(); addr == nil {
return xerrors.Errorf("listener with nil Addr")
}
loop := true
for loop {
msg, err := tc.rep.Recv()
if err != nil {
return xerrors.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 xerrors.Errorf("could not send REP message to %v: %w", msg, err)
}
}
return err
})
grp.Go(func() error {
err := tc.req.Dial(ep)
if err != nil {
return xerrors.Errorf("could not dial: %w", err)
}
if addr := tc.req.Addr(); addr != nil {
return xerrors.Errorf("dialer with non-nil Addr")
}
for _, msg := range []struct {
req zmq4.Msg
rep zmq4.Msg
}{
{reqName, repName},
{reqLang, repLang},
{reqQuit, repQuit},
} {
err = tc.req.Send(msg.req)
if err != nil {
return xerrors.Errorf("could not send REQ message %v: %w", msg.req, err)
}
rep, err := tc.req.Recv()
if err != nil {
return xerrors.Errorf("could not recv REP message %v: %w", msg.req, err)
}
if got, want := rep, msg.rep; !reflect.DeepEqual(got, want) {
return xerrors.Errorf("got = %v, want= %v", got, want)
}
}
return err
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
})
}
}