Skip to content

Commit

Permalink
extracted context with timeout setup to central location to improve t…
Browse files Browse the repository at this point in the history
…est readability
  • Loading branch information
hasson82 committed Jun 28, 2024
1 parent 832789b commit 9719207
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

const defaultTestTimeout = 10 * time.Second

var ctxWithTimeout, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)

type s struct {
grpctest.Tester
}
Expand Down Expand Up @@ -205,7 +207,7 @@ func (s) TestFromIncomingContext(t *testing.T) {
)
// Verify that we lowercase if callers directly modify md
md["X-INCORRECT-UPPERCASE"] = []string{"foo"}
ctx, cancel := context.WithTimeout(NewIncomingContext(context.Background(), md), defaultTestTimeout)
ctx := NewIncomingContext(ctxWithTimeout, md)
defer cancel()

result, found := FromIncomingContext(ctx)
Expand Down Expand Up @@ -242,7 +244,7 @@ func (s) TestValueFromIncomingContext(t *testing.T) {
)
// Verify that we lowercase if callers directly modify md
md["X-INCORRECT-UPPERCASE"] = []string{"foo"}
ctx, cancel := context.WithTimeout(NewIncomingContext(context.Background(), md), defaultTestTimeout)
ctx := NewIncomingContext(ctxWithTimeout, md)
defer cancel()

for _, test := range []struct {
Expand Down Expand Up @@ -400,7 +402,7 @@ func BenchmarkFromOutgoingContext(b *testing.B) {

func BenchmarkFromIncomingContext(b *testing.B) {
md := Pairs("X-My-Header-1", "42")
ctx, cancel := context.WithTimeout(NewIncomingContext(context.Background(), md), defaultTestTimeout)
ctx := NewIncomingContext(ctxWithTimeout, md)
defer cancel()

b.ResetTimer()
Expand All @@ -411,7 +413,7 @@ func BenchmarkFromIncomingContext(b *testing.B) {

func BenchmarkValueFromIncomingContext(b *testing.B) {
md := Pairs("X-My-Header-1", "42")
ctx, cancel := context.WithTimeout(NewIncomingContext(context.Background(), md), defaultTestTimeout)
ctx := NewIncomingContext(ctxWithTimeout, md)
defer cancel()

b.Run("key-found", func(b *testing.B) {
Expand Down

0 comments on commit 9719207

Please sign in to comment.