This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
compat_test.go
260 lines (233 loc) · 5.73 KB
/
compat_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
// Copyright 2015 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nanomsg
import (
"testing"
"time"
)
type creqTest struct {
cur uint32
tot uint32
ok bool
debug bool
addr string
sock *Socket
done chan struct{}
t *testing.T
}
type crepTest struct {
cur uint32
tot uint32
ok bool
debug bool
addr string
sock *Socket
done chan struct{}
t *testing.T
}
// For now, we take a very simple pairwise approach to req/rep. We should
// consider additional tests for raw mode multiple responders.
func (rt *creqTest) Init(t *testing.T, addr string, num uint32) bool {
var e error
if rt.sock, e = NewSocket(AF_SP, REQ); e != nil {
t.Errorf("NewSocket(): %v", e)
return false
}
if e = rt.sock.SetSendTimeout(time.Second); e != nil {
t.Errorf("Failed SetSendTimeout: %s", e)
return false
}
if e = rt.sock.SetRecvTimeout(time.Second); e != nil {
t.Errorf("Failed SetRecvTimeout: %s", e)
return false
}
rt.t = t
rt.cur = 0
rt.tot = num
rt.addr = addr
rt.done = make(chan struct{})
return true
}
func (rt *creqTest) Finish() {
rt.ok = rt.cur == rt.tot
close(rt.done)
}
func (rt *creqTest) DoTest() bool {
defer rt.Finish()
if _, err := rt.sock.Connect(rt.addr); err != nil {
rt.t.Fatalf("Failed to connect: %s", err)
}
for rt.cur < rt.tot {
var e error
var n int
m := make([]byte, 1)
m[0] = byte(rt.cur)
if rt.debug {
rt.t.Logf("Send request %d", rt.cur)
}
if n, e = rt.sock.Send(m, 0); n != 1 || e != nil {
rt.t.Errorf("%d/%d: Failed to send, %d sent, err %s", rt.cur, rt.tot, n, e)
return false
}
if rt.debug {
rt.t.Logf("Sent request %d", rt.cur)
}
m, e = rt.sock.Recv(0)
if e != nil {
rt.t.Errorf("%d/%d: Failed to recv reply: %s", rt.cur, rt.tot, e)
return false
}
if len(m) != 1 {
rt.t.Errorf("%d/%d: Got wrong length: %d != 1", rt.cur, rt.tot, len(m))
return false
}
if m[0] != byte(rt.cur) {
rt.t.Errorf("%d/%d: Got wrong reply: %d != %d", rt.cur, rt.tot, m[0], byte(rt.cur))
return false
}
if rt.debug {
rt.t.Logf("Got good reply %d", rt.cur)
}
rt.cur++
}
rt.t.Logf("Req got all %d replies", rt.cur)
return true
}
func (rt *crepTest) Init(t *testing.T, addr string, num uint32) bool {
var e error
if rt.sock, e = NewSocket(AF_SP, REP); e != nil {
t.Errorf("NewSocket(): %v", e)
return false
}
if e = rt.sock.SetSendTimeout(time.Second); e != nil {
t.Errorf("Failed SetSendTimeout: %s", e)
return false
}
if e = rt.sock.SetRecvTimeout(time.Second); e != nil {
t.Errorf("Failed SetRecvTimeout: %s", e)
return false
}
rt.t = t
rt.cur = 0
rt.tot = num
rt.addr = addr
rt.done = make(chan struct{})
return true
}
func (rt *crepTest) Finish() {
rt.sock.Close()
rt.ok = rt.cur == rt.tot
if !rt.ok {
close(rt.done)
}
}
func (rt *crepTest) DoTest() bool {
defer rt.Finish()
if _, err := rt.sock.Bind(rt.addr); err != nil {
rt.t.Errorf("Failed Bind: %s", err)
return false
}
for rt.cur < rt.tot {
var m []byte
var e error
var n int
if rt.debug {
rt.t.Logf("Wait for request %d", rt.cur)
}
if m, e = rt.sock.Recv(0); e != nil {
rt.t.Errorf("%d/%d: Failed to recv request: %s", rt.cur, rt.tot, e)
return false
}
if len(m) != 1 {
rt.t.Errorf("%d/%d: Got wrong length: %d != 1", rt.cur, rt.tot, len(m))
return false
}
if m[0] != byte(rt.cur) {
rt.t.Errorf("%d/%d: Got wrong request: %d != %d", rt.cur, rt.tot, m[0], byte(rt.cur))
return false
}
if rt.debug {
rt.t.Logf("Got good request %d", rt.cur)
}
if n, e = rt.sock.Send(m, 0); e != nil || n != 1 {
rt.t.Errorf("%d/%d: Failed to send reply: %v", rt.cur, rt.tot, e)
return false
}
if rt.debug {
rt.t.Logf("Sent reply %d", rt.cur)
}
rt.cur++
}
rt.t.Logf("Rep sent %d replies", rt.cur)
return true
}
func ReqRepCompat(t *testing.T, addr string, num uint32) {
req := &creqTest{}
rep := &crepTest{}
req.Init(t, addr, num)
rep.Init(t, addr, num)
t.Logf("Doing %d exchanges", num)
go func() {
rep.ok = rep.DoTest()
}()
go func() {
req.ok = req.DoTest()
}()
t.Logf("Waiting for tests to complete")
select {
case <-req.done:
t.Logf("Req complete")
break
case <-rep.done:
t.Logf("Rep complete")
break
}
req.sock.Close()
rep.sock.Close()
}
func TestCompatTCP(t *testing.T) {
addr := "tcp://127.0.0.1:34444"
ReqRepCompat(t, addr, 50000)
}
func TestCompatInp(t *testing.T) {
addr := "inproc:///SOMENAME"
ReqRepCompat(t, addr, 500000)
}
// Don't do this right now. There was a request at one point for send to
// timeout when no transport was present. But that semantic is diametrically
// opposed with guaranteed delivery. The only time we block or timeout is
// if there is backpressure.
func TestCompatSendTimeout(t *testing.T) {
addr := "tcp://127.0.0.1:19"
push, err := NewSocket(AF_SP, PUSH)
if err != nil {
t.Errorf("NewSocket: %v", err)
return
}
if err = push.SetSendTimeout(time.Duration(3000) * time.Millisecond); err != nil {
t.Errorf("SetSendTimeout: %v", err)
return
}
if _, err = push.Connect(addr); err != nil {
t.Errorf("Connect: %v", err)
return
}
t.Logf("Trying to send (should timeout)")
_, err = push.Send([]byte("TEST"), 0)
if err == nil {
t.Errorf("Passed, but should have timed out")
return
}
t.Logf("Expected timeout, got %v", err)
}