Make the generated code return nil intead of io.EOF when everything succeeds.

This commit is contained in:
iamqizhao
2015-02-13 16:59:56 -08:00
parent fe60321708
commit 78d3bc72bf
5 changed files with 23 additions and 23 deletions

View File

@ -287,7 +287,7 @@ void PrintClientMethodImpl(google::protobuf::io::Printer* printer,
"\t}\n" "\t}\n"
"\t// Read EOF.\n" "\t// Read EOF.\n"
"\tif err := x.ClientStream.RecvProto(m); err == io.EOF {\n" "\tif err := x.ClientStream.RecvProto(m); err == io.EOF {\n"
"\t\treturn m, io.EOF\n" "\t\treturn m, nil\n"
"\t}\n" "\t}\n"
"\t// gRPC protocol violation.\n" "\t// gRPC protocol violation.\n"
"\treturn m, fmt.Errorf(\"Violate gRPC client streaming protocol: no " "\treturn m, fmt.Errorf(\"Violate gRPC client streaming protocol: no "

View File

@ -53,12 +53,20 @@ var (
serverHost = flag.String("server_host", "127.0.0.1", "The server host name") serverHost = flag.String("server_host", "127.0.0.1", "The server host name")
serverPort = flag.Int("server_port", 10000, "The server port number") serverPort = flag.Int("server_port", 10000, "The server port number")
tlsServerName = flag.String("tls_server_name", "x.test.youtube.com", "The server name use to verify the hostname returned by TLS handshake") tlsServerName = flag.String("tls_server_name", "x.test.youtube.com", "The server name use to verify the hostname returned by TLS handshake")
testCase = flag.String("test_case", "large_unary", "The RPC method to be tested: large_unary|empty_unary|client_streaming|server_streaming|ping_pong|all") testCase = flag.String("test_case", "large_unary",
`Configure different test cases. Valid options are:
empty_unary : empty (zero bytes) request and response;
large_unary : single request and (large) response;
client_streaming : request streaming with single response;
server_streaming : single request with response streaming;
ping_pong : full-duplex streaming`)
) )
var ( var (
reqSizes = []int{27182, 8, 1828, 45904} reqSizes = []int{27182, 8, 1828, 45904}
respSizes = []int{31415, 9, 2653, 58979} respSizes = []int{31415, 9, 2653, 58979}
largeReqSize = 271828
largeRespSize = 314159
) )
func newPayload(t testpb.PayloadType, size int) *testpb.Payload { func newPayload(t testpb.PayloadType, size int) *testpb.Payload {
@ -90,12 +98,10 @@ func doEmptyUnaryCall(tc testpb.TestServiceClient) {
} }
func doLargeUnaryCall(tc testpb.TestServiceClient) { func doLargeUnaryCall(tc testpb.TestServiceClient) {
argSize := 271828 pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
respSize := 314159
pl := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
req := &testpb.SimpleRequest{ req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(), ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
ResponseSize: proto.Int32(int32(respSize)), ResponseSize: proto.Int32(int32(largeRespSize)),
Payload: pl, Payload: pl,
} }
reply, err := tc.UnaryCall(context.Background(), req) reply, err := tc.UnaryCall(context.Background(), req)
@ -104,8 +110,8 @@ func doLargeUnaryCall(tc testpb.TestServiceClient) {
} }
t := reply.GetPayload().GetType() t := reply.GetPayload().GetType()
s := len(reply.GetPayload().GetBody()) s := len(reply.GetPayload().GetBody())
if t != testpb.PayloadType_COMPRESSABLE || s != respSize { if t != testpb.PayloadType_COMPRESSABLE || s != largeRespSize {
log.Fatalf("Got the reply with type %d len %d; want %d, %d", t, s, testpb.PayloadType_COMPRESSABLE, respSize) log.Fatalf("Got the reply with type %d len %d; want %d, %d", t, s, testpb.PayloadType_COMPRESSABLE, largeRespSize)
} }
} }
@ -128,8 +134,8 @@ func doClientStreaming(tc testpb.TestServiceClient) {
} }
reply, err := stream.CloseAndRecv() reply, err := stream.CloseAndRecv()
if err != io.EOF { if err != nil {
log.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, io.EOF) log.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, nil)
} }
if reply.GetAggregatedPayloadSize() != int32(sum) { if reply.GetAggregatedPayloadSize() != int32(sum) {
log.Fatalf("%v.CloseAndRecv().GetAggregatePayloadSize() = %v; want %v", stream, reply.GetAggregatedPayloadSize(), sum) log.Fatalf("%v.CloseAndRecv().GetAggregatePayloadSize() = %v; want %v", stream, reply.GetAggregatedPayloadSize(), sum)
@ -236,7 +242,7 @@ func main() {
var err error var err error
creds, err = credentials.NewClientTLSFromFile(*caFile, sn) creds, err = credentials.NewClientTLSFromFile(*caFile, sn)
if err != nil { if err != nil {
log.Fatalf("Failed to create credentials %v", err) log.Fatalf("Failed to create TLS credentials %v", err)
} }
} else { } else {
creds = credentials.NewClientTLSFromCert(nil, sn) creds = credentials.NewClientTLSFromCert(nil, sn)
@ -260,12 +266,6 @@ func main() {
doServerStreaming(tc) doServerStreaming(tc)
case "ping_pong": case "ping_pong":
doPingPong(tc) doPingPong(tc)
case "all":
doEmptyUnaryCall(tc)
doLargeUnaryCall(tc)
doClientStreaming(tc)
doServerStreaming(tc)
doPingPong(tc)
default: default:
log.Fatal("Unsupported test case: ", *testCase) log.Fatal("Unsupported test case: ", *testCase)
} }

View File

@ -498,7 +498,7 @@ func (x *testServiceStreamingInputCallClient) CloseAndRecv() (*StreamingInputCal
// gRPC protocol violation. // gRPC protocol violation.
return nil, errors.New("gRPC client streaming protocol violation: no EOF after final response") return nil, errors.New("gRPC client streaming protocol violation: no EOF after final response")
} }
return m, io.EOF return m, nil
} }
func (c *testServiceClient) FullDuplexCall(ctx context.Context, opts ...grpc.CallOption) (TestService_FullDuplexCallClient, error) { func (c *testServiceClient) FullDuplexCall(ctx context.Context, opts ...grpc.CallOption) (TestService_FullDuplexCallClient, error) {

View File

@ -537,8 +537,8 @@ func TestClientStreaming(t *testing.T) {
sum += s sum += s
} }
reply, err := stream.CloseAndRecv() reply, err := stream.CloseAndRecv()
if err != io.EOF { if err != nil {
t.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, io.EOF) t.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, nil)
} }
if reply.GetAggregatedPayloadSize() != int32(sum) { if reply.GetAggregatedPayloadSize() != int32(sum) {
t.Fatalf("%v.CloseAndRecv().GetAggregatePayloadSize() = %v; want %v", stream, reply.GetAggregatedPayloadSize(), sum) t.Fatalf("%v.CloseAndRecv().GetAggregatePayloadSize() = %v; want %v", stream, reply.GetAggregatedPayloadSize(), sum)

View File

@ -498,7 +498,7 @@ func (x *testServiceStreamingInputCallClient) CloseAndRecv() (*StreamingInputCal
// gRPC protocol violation. // gRPC protocol violation.
return nil, errors.New("gRPC client streaming protocol violation: no EOF after final response") return nil, errors.New("gRPC client streaming protocol violation: no EOF after final response")
} }
return m, io.EOF return m, nil
} }
func (c *testServiceClient) FullDuplexCall(ctx context.Context, opts ...grpc.CallOption) (TestService_FullDuplexCallClient, error) { func (c *testServiceClient) FullDuplexCall(ctx context.Context, opts ...grpc.CallOption) (TestService_FullDuplexCallClient, error) {