Repository: grpc/grpc-go
File: test/end2end_test.go (Go)

Code before:
	}
	defer lis.Close()

	ss := grpc.UnknownServiceHandler(func(any, grpc.ServerStream) error {
		return nil
	})

	s := grpc.NewServer(ss)
	go s.Serve(lis)
	defer s.Stop()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		t.Fatalf("grpc.NewClient(%q) failed unexpectedly: %v", lis.Addr(), err)
	}
	defer cc.Close()

	client := testgrpc.NewTestServiceClient(cc)
	if _, err = client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.Internal {
		t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
	}
}

// Tests that client will receive cardinality violation in subsequent calls to RecvMsg().
func (s) TestUnaryRPC_ClientCallRecvMsgTwice(t *testing.T) {
	e := tcpTLSEnv
	te := newTest(t, e)
	defer te.tearDown()

	te.startServer(&testServer{security: e.security})

	cc := te.clientConn()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	desc := &grpc.StreamDesc{
		StreamName:    "UnaryCall",
		ServerStreams: false,
		ClientStreams: false,
	}
	stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/UnaryCall")
	if err != nil {
		t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
	}

	if err := stream.SendMsg(&testpb.SimpleRequest{}); err != nil {
		t.Fatalf("stream.SendMsg(_) = %v, want <nil>", err)
	}


Review comment (suggestion):
```suggestion
// Tests that client will receive cardinality violations when calling RecvMsg() multiple times
// for non-streaming response streams.
```

Code after:
	}
	defer lis.Close()

	ss := grpc.UnknownServiceHandler(func(any, grpc.ServerStream) error {
		return nil
	})

	s := grpc.NewServer(ss)
	go s.Serve(lis)
	defer s.Stop()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		t.Fatalf("grpc.NewClient(%q) failed unexpectedly: %v", lis.Addr(), err)
	}
	defer cc.Close()

	client := testgrpc.NewTestServiceClient(cc)
	if _, err = client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.Internal {
		t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
	}
}

// Tests that client will receive cardinality violations when calling
// RecvMsg() multiple times for non-streaming response streams.
func (s) TestUnaryRPC_ClientCallRecvMsgTwice(t *testing.T) {
	e := tcpTLSEnv
	te := newTest(t, e)
	defer te.tearDown()

	te.startServer(&testServer{security: e.security})

	cc := te.clientConn()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	desc := &grpc.StreamDesc{
		StreamName:    "UnaryCall",
		ServerStreams: false,
		ClientStreams: false,
	}
	stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/UnaryCall")
	if err != nil {
		t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
	}

	if err := stream.SendMsg(&testpb.SimpleRequest{}); err != nil {
		t.Fatalf("stream.SendMsg(_) = %v, want <nil>", err)
	}