From 435f5775331928506e6702b2c9118451280d9ed6 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 15 Oct 2020 10:39:09 +0530 Subject: [PATCH] benchmark/ health/ metadata/ reflection/ stats/ --- benchmark/primitives/context_test.go | 6 ++++-- health/client_test.go | 6 +++++- metadata/metadata_test.go | 25 ++++++++++++++++------ reflection/serverreflection_test.go | 7 +++++- stats/stats_test.go | 32 ++++++++++++++++++++-------- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/benchmark/primitives/context_test.go b/benchmark/primitives/context_test.go index c94acd74597c..faae50703e7d 100644 --- a/benchmark/primitives/context_test.go +++ b/benchmark/primitives/context_test.go @@ -24,6 +24,8 @@ import ( "time" ) +const defaultTestTimeout = 10 * time.Second + func BenchmarkCancelContextErrNoErr(b *testing.B) { ctx, cancel := context.WithCancel(context.Background()) for i := 0; i < b.N; i++ { @@ -72,7 +74,7 @@ func BenchmarkCancelContextChannelGotErr(b *testing.B) { } func BenchmarkTimerContextErrNoErr(b *testing.B) { - ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour) + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) for i := 0; i < b.N; i++ { if err := ctx.Err(); err != nil { b.Fatal("error") @@ -92,7 +94,7 @@ func BenchmarkTimerContextErrGotErr(b *testing.B) { } func BenchmarkTimerContextChannelNoErr(b *testing.B) { - ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour) + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) for i := 0; i < b.N; i++ { select { case <-ctx.Done(): diff --git a/health/client_test.go b/health/client_test.go index fa218afada72..ba933f95b84f 100644 --- a/health/client_test.go +++ b/health/client_test.go @@ -28,6 +28,8 @@ import ( "google.golang.org/grpc/connectivity" ) +const defaultTestTimeout = 10 * time.Second + func (s) TestClientHealthCheckBackoff(t *testing.T) { const maxRetries = 5 @@ -51,7 +53,9 @@ func (s) TestClientHealthCheckBackoff(t *testing.T) { } defer func() { backoffFunc = oldBackoffFunc }() - clientHealthCheck(context.Background(), newStream, func(connectivity.State, error) {}, "test") + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + clientHealthCheck(ctx, newStream, func(connectivity.State, error) {}, "test") if !reflect.DeepEqual(got, want) { t.Fatalf("Backoff durations for %v retries are %v. (expected: %v)", maxRetries, got, want) diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 84845d5b1278..f1fb5f6d324e 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -23,10 +23,13 @@ import ( "reflect" "strconv" "testing" + "time" "google.golang.org/grpc/internal/grpctest" ) +const defaultTestTimeout = 10 * time.Second + type s struct { grpctest.Tester } @@ -168,7 +171,9 @@ func (s) TestAppend(t *testing.T) { func (s) TestAppendToOutgoingContext(t *testing.T) { // Pre-existing metadata - ctx := NewOutgoingContext(context.Background(), Pairs("k1", "v1", "k2", "v2")) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + ctx := NewOutgoingContext(tCtx, Pairs("k1", "v1", "k2", "v2")) ctx = AppendToOutgoingContext(ctx, "k1", "v3") ctx = AppendToOutgoingContext(ctx, "k1", "v4") md, ok := FromOutgoingContext(ctx) @@ -181,7 +186,7 @@ func (s) TestAppendToOutgoingContext(t *testing.T) { } // No existing metadata - ctx = AppendToOutgoingContext(context.Background(), "k1", "v1") + ctx = AppendToOutgoingContext(tCtx, "k1", "v1") md, ok = FromOutgoingContext(ctx) if !ok { t.Errorf("Expected MD to exist in ctx, but got none") @@ -193,7 +198,8 @@ func (s) TestAppendToOutgoingContext(t *testing.T) { } func (s) TestAppendToOutgoingContext_Repeated(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() for i := 0; i < 100; i = i + 2 { ctx1 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i)) @@ -213,7 +219,9 @@ func (s) TestAppendToOutgoingContext_Repeated(t *testing.T) { func (s) TestAppendToOutgoingContext_FromKVSlice(t *testing.T) { const k, v = "a", "b" kv := []string{k, v} - ctx := AppendToOutgoingContext(context.Background(), kv...) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + ctx := AppendToOutgoingContext(tCtx, kv...) md, _ := FromOutgoingContext(ctx) if md[k][0] != v { t.Fatalf("md[%q] = %q; want %q", k, md[k], v) @@ -230,7 +238,8 @@ func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) { // TODO: Add in N=1-100 tests once Go1.6 support is removed. const num = 10 for n := 0; n < b.N; n++ { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() for i := 0; i < num; i++ { md, _ := FromOutgoingContext(ctx) NewOutgoingContext(ctx, Join(Pairs("k1", "v1", "k2", "v2"), md)) @@ -241,8 +250,9 @@ func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) { // Newer/faster approach to adding metadata to context func BenchmarkAppendToOutgoingContext(b *testing.B) { const num = 10 + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() for n := 0; n < b.N; n++ { - ctx := context.Background() for i := 0; i < num; i++ { ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2") } @@ -250,7 +260,8 @@ func BenchmarkAppendToOutgoingContext(b *testing.B) { } func BenchmarkFromOutgoingContext(b *testing.B) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() ctx = NewOutgoingContext(ctx, MD{"k3": {"v3", "v4"}}) ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2") diff --git a/reflection/serverreflection_test.go b/reflection/serverreflection_test.go index 55d1840fdc3a..24070141c2f2 100644 --- a/reflection/serverreflection_test.go +++ b/reflection/serverreflection_test.go @@ -25,6 +25,7 @@ import ( "reflect" "sort" "testing" + "time" "github.com/golang/protobuf/proto" dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" @@ -51,6 +52,8 @@ var ( fdProto2Ext2Byte []byte ) +const defaultTestTimeout = 10 * time.Second + type x struct { grpctest.Tester } @@ -209,7 +212,9 @@ func (x) TestReflectionEnd2end(t *testing.T) { defer conn.Close() c := rpb.NewServerReflectionClient(conn) - stream, err := c.ServerReflectionInfo(context.Background(), grpc.WaitForReady(true)) + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + stream, err := c.ServerReflectionInfo(ctx, grpc.WaitForReady(true)) if err != nil { t.Fatalf("cannot get ServerReflectionInfo: %v", err) } diff --git a/stats/stats_test.go b/stats/stats_test.go index d047d48bc5e2..875a57eeddfc 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -37,6 +37,8 @@ import ( "google.golang.org/grpc/status" ) +const defaultTestTimeout = 10 * time.Second + type s struct { grpctest.Tester } @@ -281,8 +283,10 @@ func (te *test) doUnaryCall(c *rpcConfig) (*testpb.SimpleRequest, *testpb.Simple } else { req = &testpb.SimpleRequest{Id: errorID} } - ctx := metadata.NewOutgoingContext(context.Background(), testMetadata) - resp, err = tc.UnaryCall(ctx, req, grpc.WaitForReady(!c.failfast)) + + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + resp, err = tc.UnaryCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast)) return req, resp, err } @@ -293,7 +297,9 @@ func (te *test) doFullDuplexCallRoundtrip(c *rpcConfig) ([]*testpb.SimpleRequest err error ) tc := testpb.NewTestServiceClient(te.clientConn()) - stream, err := tc.FullDuplexCall(metadata.NewOutgoingContext(context.Background(), testMetadata), grpc.WaitForReady(!c.failfast)) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + stream, err := tc.FullDuplexCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast)) if err != nil { return reqs, resps, err } @@ -332,7 +338,9 @@ func (te *test) doClientStreamCall(c *rpcConfig) ([]*testpb.SimpleRequest, *test err error ) tc := testpb.NewTestServiceClient(te.clientConn()) - stream, err := tc.ClientStreamCall(metadata.NewOutgoingContext(context.Background(), testMetadata), grpc.WaitForReady(!c.failfast)) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + stream, err := tc.ClientStreamCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast)) if err != nil { return reqs, resp, err } @@ -367,7 +375,9 @@ func (te *test) doServerStreamCall(c *rpcConfig) (*testpb.SimpleRequest, []*test startID = errorID } req = &testpb.SimpleRequest{Id: startID} - stream, err := tc.ServerStreamCall(metadata.NewOutgoingContext(context.Background(), testMetadata), req, grpc.WaitForReady(!c.failfast)) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + stream, err := tc.ServerStreamCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast)) if err != nil { return req, resps, err } @@ -1286,7 +1296,9 @@ func (s) TestClientStatsFullDuplexRPCError(t *testing.T) { func (s) TestTags(t *testing.T) { b := []byte{5, 2, 4, 3, 1} - ctx := stats.SetTags(context.Background(), b) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + ctx := stats.SetTags(tCtx, b) if tg := stats.OutgoingTags(ctx); !reflect.DeepEqual(tg, b) { t.Errorf("OutgoingTags(%v) = %v; want %v", ctx, tg, b) } @@ -1294,7 +1306,7 @@ func (s) TestTags(t *testing.T) { t.Errorf("Tags(%v) = %v; want nil", ctx, tg) } - ctx = stats.SetIncomingTags(context.Background(), b) + ctx = stats.SetIncomingTags(tCtx, b) if tg := stats.Tags(ctx); !reflect.DeepEqual(tg, b) { t.Errorf("Tags(%v) = %v; want %v", ctx, tg, b) } @@ -1305,7 +1317,9 @@ func (s) TestTags(t *testing.T) { func (s) TestTrace(t *testing.T) { b := []byte{5, 2, 4, 3, 1} - ctx := stats.SetTrace(context.Background(), b) + tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) + defer cancel() + ctx := stats.SetTrace(tCtx, b) if tr := stats.OutgoingTrace(ctx); !reflect.DeepEqual(tr, b) { t.Errorf("OutgoingTrace(%v) = %v; want %v", ctx, tr, b) } @@ -1313,7 +1327,7 @@ func (s) TestTrace(t *testing.T) { t.Errorf("Trace(%v) = %v; want nil", ctx, tr) } - ctx = stats.SetIncomingTrace(context.Background(), b) + ctx = stats.SetIncomingTrace(tCtx, b) if tr := stats.Trace(ctx); !reflect.DeepEqual(tr, b) { t.Errorf("Trace(%v) = %v; want %v", ctx, tr, b) }