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

Code before:
	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()

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

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

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

// Tests that a client receives a cardinality violation error for client-streaming
// RPCs if the server call SendMsg() multiple times.
func (s) TestClientStreaming_ServerHandlerSendMsgAfterSendMsg(t *testing.T) {
	ss := stubserver.StubServer{
		StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
			if err := stream.SendMsg(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Errorf("stream.SendMsg(_) = %v, want <nil>", err)
			}
			if err := stream.SendMsg(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Errorf("stream.SendMsg(_) = %v, want <nil>", err)
			}
			return nil
		},
	}
	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()
	stream, err := ss.Client.StreamingInputCall(ctx)
	if err != nil {
		t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
	}
	if err := stream.Send(&testpb.StreamingInputCallRequest{}); err != nil {
		t.Fatalf("stream.Send(_) = %v, want <nil>", err)

Review comment (suggestion):
```suggestion
// RPCs if the server calls SendMsg() multiple times.
```

Code after:
	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()

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

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

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

// Tests that a client receives a cardinality violation error for client-streaming
// RPCs if the server calls SendMsg() multiple times.
func (s) TestClientStreaming_ServerHandlerSendMsgAfterSendMsg(t *testing.T) {
	ss := stubserver.StubServer{
		StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
			if err := stream.SendMsg(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Errorf("stream.SendMsg(_) = %v, want <nil>", err)
			}
			if err := stream.SendMsg(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Errorf("stream.SendMsg(_) = %v, want <nil>", err)
			}
			return nil
		},
	}
	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()
	stream, err := ss.Client.StreamingInputCall(ctx)
	if err != nil {
		t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
	}
	if err := stream.Send(&testpb.StreamingInputCallRequest{}); err != nil {
		t.Fatalf("stream.Send(_) = %v, want <nil>", err)
	}