-
Notifications
You must be signed in to change notification settings - Fork 56
/
socket_test.go
391 lines (344 loc) · 10 KB
/
socket_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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// 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"
"errors"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
"github.com/go-zeromq/zmq4"
"github.com/go-zeromq/zmq4/transport"
"golang.org/x/sync/errgroup"
)
func TestInvalidConn(t *testing.T) {
// t.Parallel()
ep := must(EndPoint("tcp"))
cleanUp(ep)
ctx, timeout := context.WithTimeout(context.Background(), 20*time.Second)
defer timeout()
pub := zmq4.NewPub(ctx)
defer pub.Close()
err := pub.Listen(ep)
if err != nil {
t.Fatalf("could not listen on end-point: %+v", err)
}
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
conn, err := net.Dial("tcp", ep[len("tcp://"):])
if err != nil {
return fmt.Errorf("could not dial %q: %w", ep, err)
}
defer conn.Close()
var reply = make([]byte, 64)
_, err = io.ReadFull(conn, reply)
if err != nil {
return fmt.Errorf("could not read reply bytes...: %w", err)
}
_, err = conn.Write(make([]byte, 64))
if err != nil {
return fmt.Errorf("could not send bytes...: %w", err)
}
time.Sleep(1 * time.Second) // FIXME(sbinet): hugly.
return nil
})
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 TestConnPairs(t *testing.T) {
// t.Parallel()
bkg := context.Background()
for _, tc := range []struct {
name string
srv zmq4.Socket
wrong zmq4.Socket
cli zmq4.Socket
}{
{
name: "pair",
srv: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewSub(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "pub",
srv: zmq4.NewPub(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewSub(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "sub",
srv: zmq4.NewSub(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewPub(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "req",
srv: zmq4.NewReq(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewRep(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "rep",
srv: zmq4.NewRep(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewReq(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "dealer",
srv: zmq4.NewDealer(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewRouter(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "router",
srv: zmq4.NewRouter(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewDealer(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "pull",
srv: zmq4.NewPull(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewPush(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "push",
srv: zmq4.NewPush(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewPull(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "xpub",
srv: zmq4.NewXPub(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewXSub(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
{
name: "xsub",
srv: zmq4.NewXSub(bkg, zmq4.WithLogger(zmq4.Devnull)),
wrong: zmq4.NewPair(bkg, zmq4.WithLogger(zmq4.Devnull)),
cli: zmq4.NewXPub(bkg, zmq4.WithLogger(zmq4.Devnull)),
},
} {
t.Run(tc.name, func(t *testing.T) {
ep := must(EndPoint("tcp"))
cleanUp(ep)
_, timeout := context.WithTimeout(bkg, 20*time.Second)
defer timeout()
defer tc.srv.Close()
defer tc.wrong.Close()
defer tc.cli.Close()
err := tc.srv.Listen(ep)
if err != nil {
t.Fatalf("could not listen on %q: %+v", ep, err)
}
err = tc.wrong.Dial(ep)
if err == nil {
t.Fatalf("dialed %q", ep)
}
want := fmt.Errorf("zmq4: could not open a ZMTP connection: zmq4: could not initialize ZMTP connection: zmq4: peer=%q not compatible with %q", tc.srv.Type(), tc.wrong.Type())
if got, want := err.Error(), want.Error(); got != want {
t.Fatalf("invalid error:\ngot = %v\nwant= %v", got, want)
}
err = tc.cli.Dial(ep)
if err != nil {
t.Fatalf("could not dial %q: %+v", ep, err)
}
})
}
}
func TestConnReaperDeadlock(t *testing.T) {
// Should avoid deadlock when multiple clients are closed rapidly.
ep := must(EndPoint("tcp"))
defer cleanUp(ep)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Bind the server.
srv := zmq4.NewRouter(ctx, zmq4.WithLogger(zmq4.Devnull))
if err := srv.Listen(ep); err != nil {
t.Fatalf("could not listen on %q: %+v", ep, err)
}
defer srv.Close()
// Connect 10 clients.
var clients []zmq4.Socket
for i := 0; i < 10; i++ {
id := fmt.Sprint("client-", i)
c := zmq4.NewReq(ctx, zmq4.WithLogger(zmq4.Devnull), zmq4.WithID(zmq4.SocketIdentity(id)))
if err := c.Dial(ep); err != nil {
t.Fatalf("could not dial %q: %+v", ep, err)
}
clients = append(clients, c)
}
// Disconnect 5 of them _from the client side_. The server does not know
// the client is gone until it tries to send a message below.
for i := 0; i < 5; i++ {
clients[i].Close()
}
// Now try to send a message from the server to all 10 clients.
msg := zmq4.NewMsgFrom(nil, nil, []byte("payload"))
for i := range clients {
id := fmt.Sprint("client-", i)
msg.Frames[0] = []byte(id)
if err := srv.Send(msg); err != nil {
t.Logf("Send to %s failed: %+v\n", id, err)
}
}
for i := 5; i < 10; i++ {
clients[i].Close()
}
}
func TestSocketSendSubscriptionOnConnect(t *testing.T) {
endpoint := "inproc://test-resub"
message := "test"
sub := zmq4.NewSub(context.Background())
defer sub.Close()
pub := zmq4.NewPub(context.Background())
defer pub.Close()
sub.SetOption(zmq4.OptionSubscribe, message)
if err := sub.Listen(endpoint); err != nil {
t.Fatalf("Sub Dial failed: %v", err)
}
if err := pub.Dial(endpoint); err != nil {
t.Fatalf("Pub Dial failed: %v", err)
}
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg.Add(1)
go func() {
defer wg.Done()
for {
pub.Send(zmq4.NewMsgFromString([]string{message}))
if ctx.Err() != nil {
return
}
time.Sleep(1 * time.Millisecond)
}
}()
msg, err := sub.Recv()
if err != nil {
t.Fatalf("Recv failed: %v", err)
}
if string(msg.Frames[0]) != message {
t.Fatalf("invalid message received: got '%s', wanted '%s'", msg.Frames[0], message)
}
}
type transportMock struct {
dialCalledCount int
errOnDial bool
conn net.Conn
}
func (t *transportMock) Dial(ctx context.Context, dialer transport.Dialer, addr string) (net.Conn, error) {
t.dialCalledCount++
if t.errOnDial {
return nil, errors.New("test error")
}
return t.conn, nil
}
func (t *transportMock) Listen(ctx context.Context, addr string) (net.Listener, error) {
return nil, nil
}
func (t *transportMock) Addr(ep string) (addr string, err error) {
return "", nil
}
func TestConnMaxRetries(t *testing.T) {
retryCount := 123
socket := zmq4.NewSub(context.Background(), zmq4.WithDialerRetry(time.Microsecond), zmq4.WithDialerMaxRetries(retryCount))
transport := &transportMock{errOnDial: true}
transportName := "test-maxretries"
zmq4.RegisterTransport(transportName, transport)
err := socket.Dial(transportName + "://test")
if err == nil {
t.Fatal("expected error")
}
if transport.dialCalledCount != retryCount+1 {
t.Fatalf("Dial called %d times, expected %d", transport.dialCalledCount, retryCount+1)
}
}
func TestConnMaxRetriesInfinite(t *testing.T) {
timeout := time.Millisecond
retryTime := time.Nanosecond
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
socket := zmq4.NewSub(ctx, zmq4.WithDialerRetry(retryTime), zmq4.WithDialerMaxRetries(-1))
transport := &transportMock{errOnDial: true}
transportName := "test-infiniteretries"
zmq4.RegisterTransport(transportName, transport)
err := socket.Dial(transportName + "://test")
if err == nil {
t.Fatal("expected error")
}
atLeastExpectedRetries := 100
if transport.dialCalledCount < atLeastExpectedRetries {
t.Fatalf("Dial called %d times, expected at least %d", transport.dialCalledCount, atLeastExpectedRetries)
}
}
func TestSocketAutomaticReconnect(t *testing.T) {
ep, err := EndPoint("tcp")
if err != nil {
t.Fatalf("could not find endpoint: %+v", err)
}
message := "test"
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sendMessages := func(socket zmq4.Socket) {
wg.Add(1)
go func(t *testing.T) {
defer wg.Done()
for {
socket.Send(zmq4.NewMsgFromString([]string{message}))
if ctx.Err() != nil {
return
}
time.Sleep(1 * time.Millisecond)
}
}(t)
}
sub := zmq4.NewSub(context.Background(), zmq4.WithAutomaticReconnect(true))
defer sub.Close()
sub.SetOption(zmq4.OptionSubscribe, message)
pub := zmq4.NewPub(context.Background())
if err := pub.Listen(ep); err != nil {
t.Fatalf("Pub Dial failed: %v", err)
}
if err := sub.Dial(ep); err != nil {
t.Fatalf("Sub Dial failed: %v", err)
}
sendMessages(pub)
checkConnectionWorking := func(socket zmq4.Socket) {
for {
msg, err := socket.Recv()
if errors.Is(err, io.EOF) {
continue
}
if err != nil {
t.Fatalf("Recv failed: %v", err)
}
if string(msg.Frames[0]) != message {
t.Fatalf("invalid message received: got '%s', wanted '%s'", msg.Frames[0], message)
}
return
}
}
checkConnectionWorking(sub)
pub.Close()
pub2 := zmq4.NewPub(context.Background())
defer pub2.Close()
if err := pub2.Listen(ep); err != nil {
t.Fatalf("Sub Listen failed: %v", err)
}
sendMessages(pub2)
checkConnectionWorking(sub)
}