Skip to content

Commit

Permalink
test: use real grpc.Server in call tests (#5213)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars committed Mar 2, 2022
1 parent 6b15b13 commit 87b0a8d
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 297 deletions.
297 changes: 0 additions & 297 deletions call_test.go
Expand Up @@ -127,8 +127,6 @@ type server struct {
channelzID *channelz.Identifier
}

type ctxKey string

func newTestServer() *server {
return &server{
startedErr: make(chan error, 1),
Expand Down Expand Up @@ -211,298 +209,3 @@ func (s *server) stop() {
s.conns = nil
s.mu.Unlock()
}

func setUp(t *testing.T, port int, maxStreams uint32) (*server, *ClientConn) {
return setUpWithOptions(t, port, maxStreams)
}

func setUpWithOptions(t *testing.T, port int, maxStreams uint32, dopts ...DialOption) (*server, *ClientConn) {
server := newTestServer()
go server.start(t, port, maxStreams)
server.wait(t, 2*time.Second)
addr := "localhost:" + server.port
dopts = append(dopts, WithBlock(), WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(addr, dopts...)
if err != nil {
t.Fatalf("Failed to create ClientConn: %v", err)
}
return server, cc
}

func (s) TestUnaryClientInterceptor(t *testing.T) {
parentKey := ctxKey("parentKey")

interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("interceptor should have %v in context", parentKey)
}
return invoker(ctx, method, req, reply, cc, opts...)
}

server, cc := setUpWithOptions(t, 0, math.MaxUint32, WithUnaryInterceptor(interceptor))
defer func() {
cc.Close()
server.stop()
}()

var reply string
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
parentCtx := context.WithValue(ctx, ctxKey("parentKey"), 0)
if err := cc.Invoke(parentCtx, "/foo/bar", &expectedRequest, &reply); err != nil || reply != expectedResponse {
t.Fatalf("grpc.Invoke(_, _, _, _, _) = %v, want <nil>", err)
}
}

func (s) TestChainUnaryClientInterceptor(t *testing.T) {
var (
parentKey = ctxKey("parentKey")
firstIntKey = ctxKey("firstIntKey")
secondIntKey = ctxKey("secondIntKey")
)

firstInt := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("first interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) != nil {
t.Fatalf("first interceptor should not have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
t.Fatalf("first interceptor should not have %v in context", secondIntKey)
}
firstCtx := context.WithValue(ctx, firstIntKey, 1)
err := invoker(firstCtx, method, req, reply, cc, opts...)
*(reply.(*string)) += "1"
return err
}

secondInt := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("second interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) == nil {
t.Fatalf("second interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
t.Fatalf("second interceptor should not have %v in context", secondIntKey)
}
secondCtx := context.WithValue(ctx, secondIntKey, 2)
err := invoker(secondCtx, method, req, reply, cc, opts...)
*(reply.(*string)) += "2"
return err
}

lastInt := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("last interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) == nil {
t.Fatalf("last interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) == nil {
t.Fatalf("last interceptor should have %v in context", secondIntKey)
}
err := invoker(ctx, method, req, reply, cc, opts...)
*(reply.(*string)) += "3"
return err
}

server, cc := setUpWithOptions(t, 0, math.MaxUint32, WithChainUnaryInterceptor(firstInt, secondInt, lastInt))
defer func() {
cc.Close()
server.stop()
}()

var reply string
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
parentCtx := context.WithValue(ctx, ctxKey("parentKey"), 0)
if err := cc.Invoke(parentCtx, "/foo/bar", &expectedRequest, &reply); err != nil || reply != expectedResponse+"321" {
t.Fatalf("grpc.Invoke(_, _, _, _, _) = %v, want <nil>", err)
}
}

func (s) TestChainOnBaseUnaryClientInterceptor(t *testing.T) {
var (
parentKey = ctxKey("parentKey")
baseIntKey = ctxKey("baseIntKey")
)

baseInt := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("base interceptor should have %v in context", parentKey)
}
if ctx.Value(baseIntKey) != nil {
t.Fatalf("base interceptor should not have %v in context", baseIntKey)
}
baseCtx := context.WithValue(ctx, baseIntKey, 1)
return invoker(baseCtx, method, req, reply, cc, opts...)
}

chainInt := func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
if ctx.Value(parentKey) == nil {
t.Fatalf("chain interceptor should have %v in context", parentKey)
}
if ctx.Value(baseIntKey) == nil {
t.Fatalf("chain interceptor should have %v in context", baseIntKey)
}
return invoker(ctx, method, req, reply, cc, opts...)
}

server, cc := setUpWithOptions(t, 0, math.MaxUint32, WithUnaryInterceptor(baseInt), WithChainUnaryInterceptor(chainInt))
defer func() {
cc.Close()
server.stop()
}()

var reply string
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
parentCtx := context.WithValue(ctx, ctxKey("parentKey"), 0)
if err := cc.Invoke(parentCtx, "/foo/bar", &expectedRequest, &reply); err != nil || reply != expectedResponse {
t.Fatalf("grpc.Invoke(_, _, _, _, _) = %v, want <nil>", err)
}
}

func (s) TestChainStreamClientInterceptor(t *testing.T) {
var (
parentKey = ctxKey("parentKey")
firstIntKey = ctxKey("firstIntKey")
secondIntKey = ctxKey("secondIntKey")
)

firstInt := func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
if ctx.Value(parentKey) == nil {
t.Fatalf("first interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) != nil {
t.Fatalf("first interceptor should not have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
t.Fatalf("first interceptor should not have %v in context", secondIntKey)
}
firstCtx := context.WithValue(ctx, firstIntKey, 1)
return streamer(firstCtx, desc, cc, method, opts...)
}

secondInt := func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
if ctx.Value(parentKey) == nil {
t.Fatalf("second interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) == nil {
t.Fatalf("second interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
t.Fatalf("second interceptor should not have %v in context", secondIntKey)
}
secondCtx := context.WithValue(ctx, secondIntKey, 2)
return streamer(secondCtx, desc, cc, method, opts...)
}

lastInt := func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
if ctx.Value(parentKey) == nil {
t.Fatalf("last interceptor should have %v in context", parentKey)
}
if ctx.Value(firstIntKey) == nil {
t.Fatalf("last interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) == nil {
t.Fatalf("last interceptor should have %v in context", secondIntKey)
}
return streamer(ctx, desc, cc, method, opts...)
}

server, cc := setUpWithOptions(t, 0, math.MaxUint32, WithChainStreamInterceptor(firstInt, secondInt, lastInt))
defer func() {
cc.Close()
server.stop()
}()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
parentCtx := context.WithValue(ctx, ctxKey("parentKey"), 0)
_, err := cc.NewStream(parentCtx, &StreamDesc{}, "/foo/bar")
if err != nil {
t.Fatalf("grpc.NewStream(_, _, _) = %v, want <nil>", err)
}
}

func (s) TestInvoke(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if err := cc.Invoke(ctx, "/foo/bar", &expectedRequest, &reply); err != nil || reply != expectedResponse {
t.Fatalf("grpc.Invoke(_, _, _, _, _) = %v, want <nil>", err)
}
cc.Close()
server.stop()
}

func (s) TestInvokeLargeErr(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
req := "hello"
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
err := cc.Invoke(ctx, "/foo/bar", &req, &reply)
if _, ok := status.FromError(err); !ok {
t.Fatalf("grpc.Invoke(_, _, _, _, _) receives non rpc error.")
}
if status.Code(err) != codes.Internal || len(errorDesc(err)) != sizeLargeErr {
t.Fatalf("grpc.Invoke(_, _, _, _, _) = %v, want an error of code %d and desc size %d", err, codes.Internal, sizeLargeErr)
}
cc.Close()
server.stop()
}

// TestInvokeErrorSpecialChars checks that error messages don't get mangled.
func (s) TestInvokeErrorSpecialChars(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
req := "weird error"
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
err := cc.Invoke(ctx, "/foo/bar", &req, &reply)
if _, ok := status.FromError(err); !ok {
t.Fatalf("grpc.Invoke(_, _, _, _, _) receives non rpc error.")
}
if got, want := errorDesc(err), weirdError; got != want {
t.Fatalf("grpc.Invoke(_, _, _, _, _) error = %q, want %q", got, want)
}
cc.Close()
server.stop()
}

// TestInvokeCancel checks that an Invoke with a canceled context is not sent.
func (s) TestInvokeCancel(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
req := "canceled"
for i := 0; i < 100; i++ {
ctx, cancel := context.WithCancel(context.Background())
cancel()
cc.Invoke(ctx, "/foo/bar", &req, &reply)
}
if canceled != 0 {
t.Fatalf("received %d of 100 canceled requests", canceled)
}
cc.Close()
server.stop()
}

// TestInvokeCancelClosedNonFail checks that a canceled non-failfast RPC
// on a closed client will terminate.
func (s) TestInvokeCancelClosedNonFailFast(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
cc.Close()
req := "hello"
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := cc.Invoke(ctx, "/foo/bar", &req, &reply, WaitForReady(true)); err == nil {
t.Fatalf("canceled invoke on closed connection should fail")
}
server.stop()
}

0 comments on commit 87b0a8d

Please sign in to comment.