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

Code before:
	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.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal {
		t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
	}
}

// Tests the behavior for unary RPC when client call
// SendMsg twice. Second call to SendMsg should fail with Internal
// error.
func (s) TestUnaryRPC_ClientCallSendMsgTwice(t *testing.T) {
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatal(err)
	}
	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()

	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.Empty{}); err != nil {

Review comment (bug):
The RPC is failed on the client side, right? If so, we should use a real server instead of an `UnknownServiceHandler`.

Code after:
				StreamName:    "StreamingOutputCall",
				ServerStreams: false,
				ClientStreams: true,
			},
			wantCode: codes.Internal,
		},
	}

	for _, tc := range testCases {
		// 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()

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

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

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

// Tests the behavior of client for server-side streaming RPC when client sends zero request messages.
func (s) TestServerStreaming_ClientSendsZeroRequests(t *testing.T) {
	t.Skip("blocked on i/7286")
	// 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()