-
Notifications
You must be signed in to change notification settings - Fork 83
/
context_test.go
308 lines (263 loc) · 9.96 KB
/
context_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
// 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 (
"testing"
"time"
. "github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"
"github.com/uber/tchannel-go/testutils"
"github.com/uber/tchannel-go/testutils/goroutines"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
var cn = "hello"
func TestWrapContextForTest(t *testing.T) {
call := testutils.NewIncomingCall(cn)
ctx, cancel := NewContext(time.Second)
defer cancel()
actual := WrapContextForTest(ctx, call)
assert.Equal(t, call, CurrentCall(actual), "Incorrect call object returned.")
}
func TestNewContextTimeoutZero(t *testing.T) {
ctx, cancel := NewContextBuilder(0).Build()
defer cancel()
deadline, ok := ctx.Deadline()
assert.True(t, ok, "Context missing deadline")
assert.True(t, deadline.Sub(time.Now()) <= 0, "Deadline should be Now or earlier")
}
func TestRoutingDelegatePropagates(t *testing.T) {
WithVerifiedServer(t, nil, func(ch *Channel, hostPort string) {
peerInfo := ch.PeerInfo()
testutils.RegisterFunc(ch, "test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
return &raw.Res{
Arg3: []byte(CurrentCall(ctx).RoutingDelegate()),
}, nil
})
ctx, cancel := NewContextBuilder(time.Second).Build()
defer cancel()
_, arg3, _, err := raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, "", string(arg3), "Expected no routing delegate header")
ctx, cancel = NewContextBuilder(time.Second).SetRoutingDelegate("xpr").Build()
defer cancel()
_, arg3, _, err = raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, "xpr", string(arg3), "Expected routing delegate header to be set")
})
}
func TestRoutingKeyPropagates(t *testing.T) {
WithVerifiedServer(t, nil, func(ch *Channel, hostPort string) {
peerInfo := ch.PeerInfo()
testutils.RegisterFunc(ch, "test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
return &raw.Res{
Arg3: []byte(CurrentCall(ctx).RoutingKey()),
}, nil
})
ctx, cancel := NewContextBuilder(time.Second).Build()
defer cancel()
_, arg3, _, err := raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, "", string(arg3), "Expected no routing key header")
ctx, cancel = NewContextBuilder(time.Second).SetRoutingKey("canary").Build()
defer cancel()
_, arg3, _, err = raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, "canary", string(arg3), "Expected routing key header to be set")
})
}
func TestShardKeyPropagates(t *testing.T) {
WithVerifiedServer(t, nil, func(ch *Channel, hostPort string) {
peerInfo := ch.PeerInfo()
testutils.RegisterFunc(ch, "test", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
return &raw.Res{
Arg3: []byte(CurrentCall(ctx).ShardKey()),
}, nil
})
ctx, cancel := NewContextBuilder(time.Second).Build()
defer cancel()
_, arg3, _, err := raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, arg3, []byte(""))
ctx, cancel = NewContextBuilder(time.Second).
SetShardKey("shard").Build()
defer cancel()
_, arg3, _, err = raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "test", nil, nil)
assert.NoError(t, err, "Call failed")
assert.Equal(t, string(arg3), "shard")
})
}
func TestCurrentCallWithNilResult(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()
call := CurrentCall(ctx)
assert.Nil(t, call, "Should return nil.")
}
func getParentContext(t *testing.T) ContextWithHeaders {
ctx := context.WithValue(context.Background(), "some key", "some value")
assert.Equal(t, "some value", ctx.Value("some key"))
ctx1, _ := NewContextBuilder(time.Second).
SetParentContext(ctx).
AddHeader("header key", "header value").
Build()
assert.Equal(t, "some value", ctx1.Value("some key"))
return ctx1
}
func TestContextBuilderParentContextNoHeaders(t *testing.T) {
ctx := getParentContext(t)
assert.Equal(t, map[string]string{"header key": "header value"}, ctx.Headers())
assert.EqualValues(t, "some value", ctx.Value("some key"), "inherited from parent ctx")
}
func TestContextBuilderParentContextMergeHeaders(t *testing.T) {
ctx := getParentContext(t)
ctx.Headers()["fixed header"] = "fixed value"
// append header to parent
ctx2, _ := NewContextBuilder(time.Second).
SetParentContext(ctx).
AddHeader("header key 2", "header value 2").
Build()
assert.Equal(t, map[string]string{
"header key": "header value", // inherited
"fixed header": "fixed value", // inherited
"header key 2": "header value 2", // appended
}, ctx2.Headers())
// override parent header
ctx3, _ := NewContextBuilder(time.Second).
SetParentContext(ctx).
AddHeader("header key", "header value 2"). // override
Build()
assert.Equal(t, map[string]string{
"header key": "header value 2", // overwritten
"fixed header": "fixed value", // inherited
}, ctx3.Headers())
goroutines.VerifyNoLeaks(t, nil)
}
func TestContextBuilderParentContextReplaceHeaders(t *testing.T) {
ctx := getParentContext(t)
ctx.Headers()["fixed header"] = "fixed value"
assert.Equal(t, map[string]string{
"header key": "header value",
"fixed header": "fixed value",
}, ctx.Headers())
// replace headers with a new map
ctx2, _ := NewContextBuilder(time.Second).
SetParentContext(ctx).
SetHeaders(map[string]string{"header key": "header value 2"}).
Build()
assert.Equal(t, map[string]string{"header key": "header value 2"}, ctx2.Headers())
goroutines.VerifyNoLeaks(t, nil)
}
func TestContextWrapWithHeaders(t *testing.T) {
headers1 := map[string]string{
"k1": "v1",
}
ctx, _ := NewContextBuilder(time.Second).
SetHeaders(headers1).
Build()
assert.Equal(t, headers1, ctx.Headers(), "Headers mismatch after Build")
headers2 := map[string]string{
"k1": "v1",
}
ctx2 := WrapWithHeaders(ctx, headers2)
assert.Equal(t, headers2, ctx2.Headers(), "Headers mismatch after WrapWithHeaders")
}
func TestContextWithHeadersAsContext(t *testing.T) {
var ctx context.Context = getParentContext(t)
assert.EqualValues(t, "some value", ctx.Value("some key"), "inherited from parent ctx")
}
func TestContextBuilderParentContextSpan(t *testing.T) {
ctx := getParentContext(t)
assert.Equal(t, "some value", ctx.Value("some key"))
ctx2, _ := NewContextBuilder(time.Second).
SetParentContext(ctx).
Build()
assert.Equal(t, "some value", ctx2.Value("some key"), "key/value propagated from parent ctx")
goroutines.VerifyNoLeaks(t, nil)
}
func TestContextWrapChild(t *testing.T) {
tests := []struct {
msg string
ctxFn func() ContextWithHeaders
wantHeaders map[string]string
wantValue interface{}
}{
{
msg: "Basic context",
ctxFn: func() ContextWithHeaders {
ctxNoHeaders, _ := NewContextBuilder(time.Second).Build()
return ctxNoHeaders
},
wantHeaders: nil,
wantValue: nil,
},
{
msg: "Wrap basic context with value",
ctxFn: func() ContextWithHeaders {
ctxNoHeaders, _ := NewContextBuilder(time.Second).Build()
return Wrap(context.WithValue(ctxNoHeaders, "1", "2"))
},
wantHeaders: nil,
wantValue: "2",
},
{
msg: "Wrap context with headers and value",
ctxFn: func() ContextWithHeaders {
ctxWithHeaders, _ := NewContextBuilder(time.Second).AddHeader("h1", "v1").Build()
return Wrap(context.WithValue(ctxWithHeaders, "1", "2"))
},
wantHeaders: map[string]string{"h1": "v1"},
wantValue: "2",
},
}
for _, tt := range tests {
for _, child := range []bool{false, true} {
origCtx := tt.ctxFn()
ctx := origCtx
if child {
ctx = origCtx.Child()
}
assert.Equal(t, tt.wantValue, ctx.Value("1"), "%v: Unexpected value", tt.msg)
assert.Equal(t, tt.wantHeaders, ctx.Headers(), "%v: Unexpected headers", tt.msg)
respHeaders := map[string]string{"r": "v"}
ctx.SetResponseHeaders(respHeaders)
assert.Equal(t, respHeaders, ctx.ResponseHeaders(), "%v: Unexpected response headers", tt.msg)
if child {
// If we're working with a child context, changes to response headers
// should not affect the original context.
assert.Nil(t, origCtx.ResponseHeaders(), "%v: Child modified original context's headers", tt.msg)
}
}
}
}
func TestContextInheritParentTimeout(t *testing.T) {
deadlineAfter := time.Now().Add(time.Hour)
pctx, cancel := context.WithTimeout(context.Background(), time.Hour)
defer cancel()
ctxBuilder := &ContextBuilder{
ParentContext: pctx,
}
ctx, cancel := ctxBuilder.Build()
defer cancel()
// Ensure deadline is in the future
deadline, ok := ctx.Deadline()
require.True(t, ok, "Missing deadline")
assert.False(t, deadline.Before(deadlineAfter),
"Expected deadline to be after %v, got %v", deadlineAfter, deadline)
}