forked from yarpc/yab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
424 lines (391 loc) · 10.9 KB
/
transport_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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"fmt"
"os"
"testing"
"time"
"github.com/yarpc/yab/encoding"
"github.com/yarpc/yab/transport"
"github.com/opentracing/opentracing-go"
opentracing_ext "github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"
"golang.org/x/net/context"
)
func TestParsePeer(t *testing.T) {
tests := []struct {
peer string
protocol string
host string
}{
{"1.1.1.1:1", "", "1.1.1.1:1"},
{"some.host:1234", "", "some.host:1234"},
{"1.1.1.1", "", "1.1.1.1"},
{"ftp://1.1.1.1", "ftp", "1.1.1.1"},
{"http://1.1.1.1", "http", "1.1.1.1"},
{"https://1.1.1.1", "https", "1.1.1.1"},
{"http://1.1.1.1:8080", "http", "1.1.1.1:8080"},
{"grpc://1.1.1.1:8080", "grpc", "1.1.1.1:8080"},
{"://asd", "", "://asd"},
}
for _, tt := range tests {
t.Run(tt.peer, func(t *testing.T) {
protocol, host := parsePeer(tt.peer)
assert.Equal(t, tt.protocol, protocol, "unexpected protocol for %q", tt.peer)
assert.Equal(t, tt.host, host, "unexpected host for %q", tt.peer)
})
}
}
func TestEnsureSameProtocol(t *testing.T) {
tests := []struct {
msg string
peers []string
want string
wantErr bool
}{
{
msg: "host:ports without transport",
peers: []string{"1.1.1.1:1234", "2.2.2.2:1234"},
want: "",
},
{
msg: "only hosts",
peers: []string{"1.1.1.1", "2.2.2.2"},
want: "",
},
{
msg: "http urls",
peers: []string{"http://1.1.1.1", "http://2.2.2.2:8080"},
want: "http",
},
{
msg: "grpc urls",
peers: []string{"grpc://1.1.1.1", "grpc://2.2.2.2:8080"},
want: "grpc",
},
{
msg: "mix of http and https",
peers: []string{"https://1.1.1.1", "http://2.2.2.2:8080"},
wantErr: true,
},
{
msg: "mix of host:ports and hosts",
peers: []string{"1.1.1.1:1234", "1.1.1.1"},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
got, err := ensureSameProtocol(tt.peers)
if tt.wantErr {
assert.Error(t, err, "Expect error for %v", tt.peers)
return
}
if assert.NoError(t, err, "Expect no error for %v", tt.peers) {
assert.Equal(t, tt.want, got, "Wrong protocol for %v", tt.peers)
}
})
}
}
func TestGetHosts(t *testing.T) {
peers := []string{
"1.1.1.1",
"1.1.1.2:2222",
"http://1.1.1.3/foo/bar",
"http://1.1.1.4:8080",
"ftp://1.1.1.5/",
}
want := []string{
"1.1.1.1",
"1.1.1.2:2222",
"1.1.1.3",
"1.1.1.4:8080",
"1.1.1.5",
}
assert.Equal(t, want, getHosts(peers))
}
func TestLoadTransportPeers(t *testing.T) {
tests := []struct {
msg string
opts TransportOptions
wantScheme string
wantPeers []string
errMsg string
}{
{
msg: "no peers specified",
opts: TransportOptions{},
errMsg: errPeerRequired.Error(),
},
{
msg: "inline peers with ip:port",
opts: TransportOptions{Peers: []string{"1.1.1.1:1"}},
wantScheme: "",
wantPeers: []string{"1.1.1.1:1"},
},
{
msg: "inline peers with localhost:port",
opts: TransportOptions{Peers: []string{"localhost:1234"}},
wantScheme: "",
wantPeers: []string{"localhost:1234"},
},
{
msg: "valid peerlist",
opts: TransportOptions{PeerList: "testdata/valid_peerlist.json"},
wantPeers: []string{"1.1.1.1:1", "2.2.2.2:2"},
},
{
msg: "unknown peerlist URL",
opts: TransportOptions{PeerList: "unknown://foo"},
errMsg: "no peer provider available for scheme",
},
{
msg: "invalid peerlist URL",
opts: TransportOptions{PeerList: ":://foo.html"},
errMsg: "could not parse peer provider URL",
},
{
msg: "invalid peer list",
opts: TransportOptions{PeerList: "testdata/invalid.json"},
errMsg: "peer list should be YAML, JSON, or newline delimited strings",
},
{
msg: "empty peer list",
opts: TransportOptions{PeerList: "testdata/empty.txt"},
errMsg: "specified peer list is empty",
},
{
msg: "both peers and peer list specified",
opts: TransportOptions{Peers: []string{"1.1.1.1:1"}, PeerList: "testdata/valid_peerlist.json"},
errMsg: errPeerOptions.Error(),
},
{
msg: "URL peer list",
opts: TransportOptions{Peers: []string{"http://1.1.1.1"}},
wantScheme: "http",
wantPeers: []string{"http://1.1.1.1"},
},
{
msg: "URL and host:port in peer list",
opts: TransportOptions{Peers: []string{"1.1.1.1:1", "http://1.1.1.1"}},
errMsg: "found mixed protocols",
},
}
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
scheme, peers, err := loadTransportPeers(tt.opts)
if tt.errMsg != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errMsg, "Unexpected error")
return
}
require.NoError(t, err)
assert.Equal(t, tt.wantScheme, scheme, "unexpected scheme")
assert.Equal(t, tt.wantPeers, peers, "unexpected scheme")
})
}
}
func TestGetTransport(t *testing.T) {
tests := []struct {
msg string
opts TransportOptions
resolved resolvedProtocolEncoding
noTracer bool
errMsg string
}{
{
msg: "TChannel Thrift ip:port without service",
opts: TransportOptions{
CallerName: "caller",
Peers: []string{"1.1.1.1:1"},
},
resolved: _resolvedTChannelThrift,
errMsg: errServiceRequired.Error(),
},
{
msg: "TChannel Thrift ip:port without caller",
opts: TransportOptions{
ServiceName: "svc",
Peers: []string{"1.1.1.1:1"},
},
resolved: _resolvedTChannelThrift,
errMsg: errCallerRequired.Error(),
},
{
msg: "TChannel Thrift ip:port without tracer",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"1.1.1.1:1"},
},
resolved: _resolvedTChannelThrift,
noTracer: true,
errMsg: errTracerRequired.Error(),
},
{
msg: "TChannel Thrift ip:port",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"1.1.1.1:1"},
},
resolved: _resolvedTChannelThrift,
},
{
msg: "TChannel Thrift localhost:port",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"localhost:1234"},
},
resolved: _resolvedTChannelThrift,
},
{
msg: "TChannel Thrift URL",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"tchannel://localhost:1234"},
},
resolved: _resolvedTChannelThrift,
},
{
msg: "gRPC URL",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"grpc://localhost:1234"},
},
resolved: resolvedProtocolEncoding{protocol: transport.GRPC, enc: encoding.Protobuf},
},
{
msg: "HTTP JSON URL",
opts: TransportOptions{
ServiceName: "svc",
CallerName: "caller",
Peers: []string{"http://1.1.1.1"},
},
resolved: resolvedProtocolEncoding{protocol: transport.HTTP, enc: encoding.JSON},
},
}
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
var tracer opentracing.Tracer
if !tt.noTracer {
tracer = opentracing.NoopTracer{}
}
transport, err := getTransport(tt.opts, tt.resolved, tracer)
if tt.errMsg != "" {
require.Error(t, err, "getTransport(%v) should fail", tt.opts)
assert.Contains(t, err.Error(), tt.errMsg, "Unexpected error for getTransport(%v)", tt.opts)
return
}
require.NoError(t, err, "getTransport(%v) should not fail", tt.opts)
require.NotNil(t, transport, "getTransport(%v) didn't get transport", tt.opts)
assert.Equal(t, tt.resolved.protocol, transport.Protocol(), "incorrect protocol")
})
}
}
func TestGetTransportCallerName(t *testing.T) {
tests := []struct {
caller string
want string
benchmark bool
wantErr bool
}{
{
caller: "",
want: "yab-" + os.Getenv("USER"),
},
{
caller: "override",
want: "override",
},
{
benchmark: true,
caller: "",
want: "yab-" + os.Getenv("USER"),
},
}
for _, tt := range tests {
server := newServer(t)
defer server.shutdown()
server.register("test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
assert.Equal(t, tt.want, tchannel.CurrentCall(ctx).CallerName(), "Caller name mismatch")
return &raw.Res{}, nil
})
opts := TransportOptions{
ServiceName: server.ch.ServiceName(),
Peers: []string{server.hostPort()},
CallerName: tt.caller,
}
tchan, err := getTransport(opts, resolvedProtocolEncoding{protocol: transport.TChannel, enc: encoding.Raw}, opentracing.NoopTracer{})
if tt.wantErr {
assert.Error(t, err, fmt.Sprintf("Expect fail: %+v", tt))
continue
}
if err != nil {
continue
}
ctx, cancel := tchannel.NewContext(time.Second)
defer cancel()
_, err = tchan.Call(ctx, &transport.Request{
Method: "test",
})
assert.NoError(t, err, "Expect to succeed: %+v", tt)
}
}
func TestGetTransportTraceEnabled(t *testing.T) {
tracer, closer := getTestTracer(t, "foo")
defer closer.Close()
s := newServer(t, withTracer(tracer))
defer s.shutdown()
s.register("test", methods.traceEnabled())
tests := []struct {
trace bool
traceEnabled byte
}{
{false, 0},
{true, 1},
}
opts := TransportOptions{
ServiceName: s.ch.ServiceName(),
CallerName: "qux",
Peers: []string{s.hostPort()},
}
for _, tt := range tests {
ctx, cancel := tchannel.NewContext(time.Second)
defer cancel()
if tt.trace {
span := tracer.StartSpan("test")
opentracing_ext.SamplingPriority.Set(span, 1)
ctx = opentracing.ContextWithSpan(ctx, span)
}
tchan, err := getTransport(opts, _resolvedTChannelRaw, tracer)
require.NoError(t, err, "getTransport failed")
res, err := tchan.Call(ctx, &transport.Request{Method: "test"})
require.NoError(t, err, "transport.Call failed")
assert.Equal(t, tt.traceEnabled, res.Body[0], "TraceEnabled mismatch")
}
}