-
Notifications
You must be signed in to change notification settings - Fork 57
/
rep_test.go
170 lines (133 loc) · 3.06 KB
/
rep_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
// 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"
"sync"
"testing"
"github.com/go-zeromq/zmq4"
)
func TestIssue99(t *testing.T) {
var (
wg sync.WaitGroup
outMsg zmq4.Msg
inMsg zmq4.Msg
ok = make(chan int)
)
ep, err := EndPoint("tcp")
if err != nil {
t.Fatalf("could not find endpoint: %+v", err)
}
requester := func() {
defer wg.Done()
defer close(ok)
req := zmq4.NewReq(context.Background())
defer req.Close()
err := req.Dial(ep)
if err != nil {
t.Errorf("could not dial: %+v", err)
return
}
// Test message w/ 3 frames
outMsg = zmq4.NewMsgFromString([]string{"ZERO", "Hello!", "World!"})
err = req.Send(outMsg)
if err != nil {
t.Errorf("failed to send: %+v", err)
return
}
inMsg, err = req.Recv()
if err != nil {
t.Errorf("failed to recv: %+v", err)
return
}
}
responder := func() {
defer wg.Done()
rep := zmq4.NewRep(context.Background())
defer rep.Close()
err := rep.Listen(ep)
if err != nil {
t.Errorf("could not dial: %+v", err)
return
}
// Wait for next request from client
msg, err := rep.Recv()
if err != nil {
t.Errorf("could not recv request: %+v", err)
return
}
// Send reply back to client
err = rep.Send(msg)
if err != nil {
t.Errorf("could not send reply: %+v", err)
return
}
<-ok
}
wg.Add(2)
go requester()
go responder()
wg.Wait()
if want, got := len(outMsg.Frames), len(inMsg.Frames); want != got {
t.Fatalf("message length mismatch: got=%d, want=%d", got, want)
}
}
func TestCancellation(t *testing.T) {
// if the context is cancelled during a rep.Send both the requester and the responder should get an error
var wg sync.WaitGroup
ep, err := EndPoint("tcp")
if err != nil {
t.Fatalf("could not find endpoint: %+v", err)
}
responderStarted := make(chan bool)
requester := func() {
defer wg.Done()
<-responderStarted
req := zmq4.NewReq(context.Background())
defer req.Close()
err := req.Dial(ep)
if err != nil {
t.Errorf("could not dial: %+v", err)
return
}
err = req.Send(zmq4.NewMsgString("ping"))
if err != nil {
t.Errorf("could not send: %+v", err)
return
}
msg, err := req.Recv()
if err == nil {
t.Errorf("requester should have gotten an error, but got: %+v", msg)
}
}
responder := func() {
defer wg.Done()
repCtx, cancel := context.WithCancel(context.Background())
defer cancel()
rep := zmq4.NewRep(repCtx)
defer rep.Close()
err := rep.Listen(ep)
if err != nil {
t.Errorf("could not dial: %+v", err)
return
}
responderStarted <- true
_, err = rep.Recv()
if err != nil {
t.Errorf("could not recv: %+v", err)
return
}
// cancel the context right before sending the response
cancel()
err = rep.Send(zmq4.NewMsgString("pong"))
if !errors.Is(err, context.Canceled) {
t.Errorf("context should be cancelled: %+v", err)
}
}
wg.Add(2)
go requester()
go responder()
wg.Wait()
}