forked from go-zeromq/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq4_pair_test.go
173 lines (143 loc) · 3.57 KB
/
zmq4_pair_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
// 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 (
"bytes"
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/go-zeromq/zmq4"
"golang.org/x/sync/errgroup"
)
var (
pairs = []testCasePair{
{
name: "tcp-pair-pair",
endpoint: must(EndPoint("tcp")),
srv: zmq4.NewPair(bkg),
cli: zmq4.NewPair(bkg),
},
{
name: "ipc-pair-pair",
endpoint: "ipc://ipc-pair-pair",
srv: zmq4.NewPair(bkg),
cli: zmq4.NewPair(bkg),
},
{
name: "inproc-pair-pair",
endpoint: "inproc://inproc-pair-pair",
srv: zmq4.NewPair(bkg),
cli: zmq4.NewPair(bkg),
},
}
)
type testCasePair struct {
name string
skip bool
endpoint string
srv zmq4.Socket
cli zmq4.Socket
}
func TestPair(t *testing.T) {
var (
msg0 = zmq4.NewMsgString("")
msg1 = zmq4.NewMsgString("MSG 1")
msg2 = zmq4.NewMsgString("msg 2")
msgs = []zmq4.Msg{
msg0,
msg1,
msg2,
}
)
for i := range pairs {
tc := pairs[i]
t.Run(tc.name, func(t *testing.T) {
defer tc.srv.Close()
defer tc.cli.Close()
ep := tc.endpoint
cleanUp(ep)
if tc.skip {
t.Skipf(tc.name)
}
// t.Parallel()
var (
wg1 sync.WaitGroup
wg2 sync.WaitGroup
)
wg1.Add(1)
wg2.Add(1)
ctx, timeout := context.WithTimeout(context.Background(), 20*time.Second)
defer timeout()
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := tc.srv.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
if addr := tc.srv.Addr(); addr == nil {
return fmt.Errorf("listener with nil Addr")
}
wg1.Wait()
wg2.Done()
for _, msg := range msgs {
err = tc.srv.Send(msg)
if err != nil {
return fmt.Errorf("could not send message %v: %w", msg, err)
}
reply, err := tc.srv.Recv()
if err != nil {
return fmt.Errorf("could not recv reply to %v: %w", msg, err)
}
if got, want := reply, zmq4.NewMsgString("reply: "+string(msg.Bytes())); !bytes.Equal(got.Bytes(), want.Bytes()) {
return fmt.Errorf("invalid cli reply for msg #%d: got=%v, want=%v", i, got, want)
}
}
quit, err := tc.srv.Recv()
if err != nil {
return fmt.Errorf("could not recv QUIT message: %w", err)
}
if got, want := quit, zmq4.NewMsgString("QUIT"); !bytes.Equal(got.Bytes(), want.Bytes()) {
return fmt.Errorf("invalid QUIT message from cli: got=%v, want=%v", got, want)
}
return err
})
grp.Go(func() error {
err := tc.cli.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
wg1.Done()
wg2.Wait()
for i := range msgs {
msg, err := tc.cli.Recv()
if err != nil {
return fmt.Errorf("could not recv #%d msg from srv: %w", i, err)
}
if !bytes.Equal(msg.Bytes(), msgs[i].Bytes()) {
return fmt.Errorf("invalid #%d msg from srv: got=%v, want=%v",
i, msg, msgs[i],
)
}
err = tc.cli.Send(zmq4.NewMsgString("reply: " + string(msg.Bytes())))
if err != nil {
return fmt.Errorf("could not send message %v: %w", msg, err)
}
}
err = tc.cli.Send(zmq4.NewMsgString("QUIT"))
if err != nil {
return fmt.Errorf("could not send QUIT message: %w", 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)
}
})
}
}