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

Code before:
	defer cc.Close()

	desc := &grpc.StreamDesc{
		StreamName:    "FullDuplexCall",
		ServerStreams: true,
		ClientStreams: false,
	}

	stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/FullDuplexCall")
	if err != nil {
		t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
	}

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

	if err := stream.SendMsg(&testpb.Empty{}); status.Code(err) != codes.Internal {
		t.Errorf("stream.SendMsg() = %v, want error %v", status.Code(err), codes.Internal)
	}
}

// Tests the behavior for unary RPC when server call
// RecvMsg twice. Second call to RecvMsg should fail with Internal
// error.
func (s) TestUnaryRPC_ServerCallRecvMsgTwice(t *testing.T) {
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatal(err)
	}
	defer lis.Close()

	s := grpc.NewServer()
	serviceDesc := grpc.ServiceDesc{
		ServiceName: "grpc.testing.TestService",
		HandlerType: (*any)(nil),
		Methods:     []grpc.MethodDesc{},
		Streams: []grpc.StreamDesc{
			{
				StreamName: "UnaryCall",
				Handler: func(_ any, stream grpc.ServerStream) error {
					err := stream.RecvMsg(&testpb.Empty{})
					if err != nil {
						t.Errorf("stream.RecvMsg() = %v, want <nil>", err)
					}

					if err = stream.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal {
						t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
					}
					return nil
				},

Review comment (suggestion):
Can this be combined with `TestServerStreaming_ServerCallRecvMsgTwice` into a single table drive test? They seem to be exactly the same except for a couple of lines. Similarly for the `ClientCallSendMsgTwice` tests.

Code after:
	if err != nil {
		t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
	}

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

	if err := stream.SendMsg(&testpb.Empty{}); status.Code(err) != codes.Internal {
		t.Errorf("stream.SendMsg() = %v, want error %v", status.Code(err), codes.Internal)
	}
}

// Tests the behavior for server-side streaming RPC when client misbehaves as Bidi-streaming
// and sends multiple messages.
func (s) TestServerStreaming_ClientSendsMultipleMessages(t *testing.T) {
	// The initial call to recvMsg made by the generated code, will return the error.
	ss := stubserver.StubServer{}
	if err := ss.Start(nil); err != nil {
		t.Fatal("Error starting server:", err)
	}
	defer ss.Stop()

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

	// Making the client bi-di to bypass the client side checks that stop a non-streaming client
	// from sending multiple messages.
	desc := &grpc.StreamDesc{
		StreamName:    "StreamingOutputCall",
		ServerStreams: true,
		ClientStreams: true,
	}

	stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/StreamingOutputCall")
	if err != nil {
		t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
	}

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

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