Skip to content

Commit

Permalink
benchmark/
Browse files Browse the repository at this point in the history
health/
metadata/
reflection/
stats/
  • Loading branch information
gauravgahlot committed Oct 19, 2020
1 parent 385152a commit 80784df
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
5 changes: 4 additions & 1 deletion benchmark/primitives/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -139,7 +141,8 @@ func BenchmarkContextWithValue(b *testing.B) {
{"newContextWithGlobalKey", newContextWithGlobalKey},
}

pCtx := context.Background()
pCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
for _, bench := range benches {
b.Run(bench.name, func(b *testing.B) {
for j := 0; j < b.N; j++ {
Expand Down
6 changes: 5 additions & 1 deletion health/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"google.golang.org/grpc/connectivity"
)

const defaultTestTimeout = 10 * time.Second

func (s) TestClientHealthCheckBackoff(t *testing.T) {
const maxRetries = 5

Expand All @@ -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)
Expand Down
25 changes: 18 additions & 7 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -242,15 +251,17 @@ func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) {
func BenchmarkAppendToOutgoingContext(b *testing.B) {
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++ {
ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
}
}
}

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")

Expand Down
7 changes: 6 additions & 1 deletion reflection/serverreflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"reflect"
"sort"
"testing"
"time"

"github.com/golang/protobuf/proto"
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
Expand All @@ -51,6 +52,8 @@ var (
fdProto2Ext2Byte []byte
)

const defaultTestTimeout = 10 * time.Second

type x struct {
grpctest.Tester
}
Expand Down Expand Up @@ -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)
}
Expand Down
31 changes: 23 additions & 8 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"google.golang.org/grpc/status"
)

const defaultTestTimeout = 10 * time.Second

type s struct {
grpctest.Tester
}
Expand Down Expand Up @@ -281,7 +283,10 @@ func (te *test) doUnaryCall(c *rpcConfig) (*testpb.SimpleRequest, *testpb.Simple
} else {
req = &testpb.SimpleRequest{Id: errorID}
}
ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)

tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
ctx := metadata.NewOutgoingContext(tCtx, testMetadata)
resp, err = tc.UnaryCall(ctx, req, grpc.WaitForReady(!c.failfast))
return req, resp, err
}
Expand All @@ -293,7 +298,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
}
Expand Down Expand Up @@ -332,7 +339,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
}
Expand Down Expand Up @@ -367,7 +376,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
}
Expand Down Expand Up @@ -1286,15 +1297,17 @@ 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)
}
if tg := stats.Tags(ctx); tg != nil {
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)
}
Expand All @@ -1305,15 +1318,17 @@ 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)
}
if tr := stats.Trace(ctx); tr != nil {
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)
}
Expand Down

0 comments on commit 80784df

Please sign in to comment.