Skip to content

Commit 4b89735

Browse files
committed
move ctx to inside each test
1 parent 6c25072 commit 4b89735

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

metadata/metadata_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import (
3131

3232
const defaultTestTimeout = 10 * time.Second
3333

34-
var ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
35-
3634
type s struct {
3735
grpctest.Tester
3836
}
@@ -205,10 +203,11 @@ func (s) TestFromIncomingContext(t *testing.T) {
205203
md := Pairs(
206204
"X-My-Header-1", "42",
207205
)
206+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
207+
defer cancel()
208208
// Verify that we lowercase if callers directly modify md
209209
md["X-INCORRECT-UPPERCASE"] = []string{"foo"}
210-
ctx := NewIncomingContext(ctx, md)
211-
defer cancel()
210+
ctx = NewIncomingContext(ctx, md)
212211

213212
result, found := FromIncomingContext(ctx)
214213
if !found {
@@ -242,10 +241,11 @@ func (s) TestValueFromIncomingContext(t *testing.T) {
242241
"X-My-Header-2", "43-2",
243242
"x-my-header-3", "44",
244243
)
244+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
245+
defer cancel()
245246
// Verify that we lowercase if callers directly modify md
246247
md["X-INCORRECT-UPPERCASE"] = []string{"foo"}
247-
ctx := NewIncomingContext(ctx, md)
248-
defer cancel()
248+
ctx = NewIncomingContext(ctx, md)
249249

250250
for _, test := range []struct {
251251
key string
@@ -401,9 +401,10 @@ func BenchmarkFromOutgoingContext(b *testing.B) {
401401
}
402402

403403
func BenchmarkFromIncomingContext(b *testing.B) {
404-
md := Pairs("X-My-Header-1", "42")
405-
ctx := NewIncomingContext(ctx, md)
404+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
406405
defer cancel()
406+
md := Pairs("X-My-Header-1", "42")
407+
ctx = NewIncomingContext(ctx, md)
407408

408409
b.ResetTimer()
409410
for n := 0; n < b.N; n++ {
@@ -412,9 +413,10 @@ func BenchmarkFromIncomingContext(b *testing.B) {
412413
}
413414

414415
func BenchmarkValueFromIncomingContext(b *testing.B) {
415-
md := Pairs("X-My-Header-1", "42")
416-
ctx := NewIncomingContext(ctx, md)
416+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
417417
defer cancel()
418+
md := Pairs("X-My-Header-1", "42")
419+
ctx = NewIncomingContext(ctx, md)
418420

419421
b.Run("key-found", func(b *testing.B) {
420422
for n := 0; n < b.N; n++ {

0 commit comments

Comments
 (0)