forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbound_test.go
221 lines (174 loc) · 7.49 KB
/
inbound_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
// Copyright (c) 2015 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 tchannel_test
import (
"strings"
"testing"
"time"
. "github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"
"github.com/uber/tchannel-go/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
func TestActiveCallReq(t *testing.T) {
t.Skip("Test skipped due to unreliable way to test for protocol errors")
ctx, cancel := NewContext(time.Second)
defer cancel()
// Note: This test cannot use log verification as the duplicate ID causes a log.
// It does not use a verified server, as it leaks a message exchange due to the
// modification of IDs in the relay.
opts := testutils.NewOpts().DisableLogVerification()
testutils.WithServer(t, opts, func(ch *Channel, hostPort string) {
gotCall := make(chan struct{})
unblock := make(chan struct{})
testutils.RegisterFunc(ch, "blocked", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
gotCall <- struct{}{}
<-unblock
return &raw.Res{}, nil
})
relayFunc := func(outgoing bool, frame *Frame) *Frame {
if outgoing && frame.Header.ID == 3 {
frame.Header.ID = 2
}
return frame
}
relayHostPort, closeRelay := testutils.FrameRelay(t, hostPort, relayFunc)
defer closeRelay()
firstComplete := make(chan struct{})
go func() {
// This call will block until we close unblock.
raw.Call(ctx, ch, relayHostPort, ch.PeerInfo().ServiceName, "blocked", nil, nil)
close(firstComplete)
}()
// Wait for the first call to be received by the server
<-gotCall
// Make a new call, which should fail
_, _, _, err := raw.Call(ctx, ch, relayHostPort, ch.PeerInfo().ServiceName, "blocked", nil, nil)
assert.Error(t, err, "Expect error")
assert.True(t, strings.Contains(err.Error(), "already active"),
"expected already active error, got %v", err)
close(unblock)
<-firstComplete
})
}
func TestInboundConnection(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()
// Disable relay since relays hide host:port on outbound calls.
opts := testutils.NewOpts().NoRelay()
testutils.WithTestServer(t, opts, func(t testing.TB, ts *testutils.TestServer) {
s2 := ts.NewServer(nil)
ts.RegisterFunc("test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
c, _ := InboundConnection(CurrentCall(ctx))
assert.Equal(t, s2.PeerInfo().HostPort, c.RemotePeerInfo().HostPort, "Unexpected host port")
return &raw.Res{}, nil
})
_, _, _, err := raw.Call(ctx, s2, ts.HostPort(), ts.ServiceName(), "test", nil, nil)
require.NoError(t, err, "Call failed")
})
}
func TestInboundConnection_CallOptions(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()
testutils.WithTestServer(t, nil, func(t testing.TB, server *testutils.TestServer) {
server.RegisterFunc("test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
assert.Equal(t, "client", CurrentCall(ctx).CallerName(), "Expected caller name to be passed through")
return &raw.Res{}, nil
})
backendName := server.ServiceName()
proxyCh := server.NewServer(&testutils.ChannelOpts{ServiceName: "proxy"})
defer proxyCh.Close()
subCh := proxyCh.GetSubChannel(backendName)
subCh.SetHandler(HandlerFunc(func(ctx context.Context, inbound *InboundCall) {
outbound, err := proxyCh.BeginCall(ctx, server.HostPort(), backendName, inbound.MethodString(), inbound.CallOptions())
require.NoError(t, err, "Create outbound call failed")
arg2, arg3, _, err := raw.WriteArgs(outbound, []byte("hello"), []byte("world"))
require.NoError(t, err, "Write outbound call failed")
require.NoError(t, raw.WriteResponse(inbound.Response(), &raw.Res{
Arg2: arg2,
Arg3: arg3,
}), "Write response failed")
}))
clientCh := server.NewClient(&testutils.ChannelOpts{
ServiceName: "client",
})
defer clientCh.Close()
_, _, _, err := raw.Call(ctx, clientCh, proxyCh.PeerInfo().HostPort, backendName, "test", nil, nil)
require.NoError(t, err, "Call through proxy failed")
})
}
func TestCallOptionsPropogated(t *testing.T) {
const handler = "handler"
giveCallOpts := CallOptions{
Format: JSON,
CallerName: "test-caller-name",
ShardKey: "test-shard-key",
RoutingKey: "test-routing-key",
RoutingDelegate: "test-routing-delegate",
}
var gotCallOpts *CallOptions
testutils.WithTestServer(t, nil, func(t testing.TB, ts *testutils.TestServer) {
ts.Register(HandlerFunc(func(ctx context.Context, inbound *InboundCall) {
gotCallOpts = inbound.CallOptions()
err := raw.WriteResponse(inbound.Response(), &raw.Res{})
assert.NoError(t, err, "write response failed")
}), handler)
ctx, cancel := NewContext(testutils.Timeout(time.Second))
defer cancel()
call, err := ts.Server().BeginCall(ctx, ts.HostPort(), ts.ServiceName(), handler, &giveCallOpts)
require.NoError(t, err, "could not call test server")
_, _, _, err = raw.WriteArgs(call, nil, nil)
require.NoError(t, err, "could not write args")
assert.Equal(t, &giveCallOpts, gotCallOpts)
})
}
func TestBlackhole(t *testing.T) {
ctx, cancel := NewContext(testutils.Timeout(time.Hour))
testutils.WithTestServer(t, nil, func(t testing.TB, server *testutils.TestServer) {
serviceName := server.ServiceName()
handlerName := "test-handler"
server.Register(HandlerFunc(func(ctx context.Context, inbound *InboundCall) {
// cancel client context in handler so the client can return after being blackholed
defer cancel()
c, _ := InboundConnection(inbound)
require.NotNil(t, c)
state := c.IntrospectState(&IntrospectionOptions{})
require.Equal(t, 1, state.InboundExchange.Count, "expected exactly one inbound exchange")
// blackhole request
inbound.Response().Blackhole()
// give time for exchange to cleanup
require.True(t, testutils.WaitFor(10*time.Millisecond, func() bool {
state = c.IntrospectState(&IntrospectionOptions{})
return state.InboundExchange.Count == 0
}),
"expected no inbound exchanges",
)
}), handlerName)
clientCh := server.NewClient(nil)
defer clientCh.Close()
_, _, _, err := raw.Call(ctx, clientCh, server.HostPort(), serviceName, handlerName, nil, nil)
require.Error(t, err, "expected call error")
errCode := GetSystemErrorCode(err)
// Providing 'got: %q' is necessary since SystemErrCode is a type alias of byte; testify's
// failed test ouput would otherwise print out hex codes.
assert.Equal(t, ErrCodeCancelled, errCode, "expected cancelled error code, got: %q", errCode)
})
}