From cdd14fc200cf72a610143dc3464ea9a12b09fac5 Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Wed, 21 Feb 2018 15:38:59 +0100 Subject: [PATCH] fix golint + update travis --- .travis.yml | 2 +- Makefile | 2 +- cmd/grpcurl/grpcurl.go | 6 ++++-- grpcurl.go | 4 ++-- grpcurl_test.go | 30 +++++++++++++++--------------- testing/test_server.go | 26 ++++++++++++++------------ 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/.travis.yml b/.travis.yml index 54c7d98..df24689 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,4 @@ matrix: script: # TODO: change to "make" when golint, errcheck, staticcheck pass - - make deps checkgofmt vet unused test + - make deps checkgofmt golint vet unused test diff --git a/Makefile b/Makefile index 48f25fc..20e1542 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ vet: .PHONY: errcheck: @go get github.com/kisielk/errcheck - errcheck $(PKGS) + errcheck -verbose $(PKGS) .PHONY: staticcheck staticcheck: diff --git a/cmd/grpcurl/grpcurl.go b/cmd/grpcurl/grpcurl.go index 88cbab4..930d8cc 100644 --- a/cmd/grpcurl/grpcurl.go +++ b/cmd/grpcurl/grpcurl.go @@ -244,7 +244,9 @@ func main() { refClient = nil } if cc != nil { - cc.Close() + if err := cc.Close(); err != nil { + fail(err, "Failed to close grpc Client") + } cc = nil } } @@ -343,7 +345,7 @@ func main() { } h := &handler{dec: dec, descSource: descSource} - err := grpcurl.InvokeRpc(ctx, descSource, cc, symbol, addlHeaders, h, h.getRequestData) + err := grpcurl.InvokeRPC(ctx, descSource, cc, symbol, addlHeaders, h, h.getRequestData) if err != nil { fail(err, "Error invoking method %q", symbol) } diff --git a/grpcurl.go b/grpcurl.go index d97662e..158a06f 100644 --- a/grpcurl.go +++ b/grpcurl.go @@ -276,7 +276,7 @@ type InvocationEventHandler interface { // the supplier has no more messages, it should return nil, io.EOF. type RequestMessageSupplier func() ([]byte, error) -// InvokeRpc uses te given GRPC connection to invoke the given method. The given descriptor source +// InvokeRPC uses te given GRPC connection to invoke the given method. The given descriptor source // is used to determine the type of method and the type of request and response message. The given // headers are sent as request metadata. Methods on the given event handler are called as the // invocation proceeds. @@ -291,7 +291,7 @@ type RequestMessageSupplier func() ([]byte, error) // be thread-safe. This is because the requestData function may be called from a different goroutine // than the one invoking event callbacks. (This only happens for bi-directional streaming RPCs, where // one goroutine sends request messages and another consumes the response messages). -func InvokeRpc(ctx context.Context, source DescriptorSource, cc *grpc.ClientConn, methodName string, +func InvokeRPC(ctx context.Context, source DescriptorSource, cc *grpc.ClientConn, methodName string, headers []string, handler InvocationEventHandler, requestData RequestMessageSupplier) error { md := MetadataFromHeaders(headers) diff --git a/grpcurl_test.go b/grpcurl_test.go index 453fcb2..e1de684 100644 --- a/grpcurl_test.go +++ b/grpcurl_test.go @@ -108,7 +108,7 @@ func TestServerDoesNotSupportReflection(t *testing.T) { t.Errorf("ListMethods should have returned ErrReflectionNotSupported; instead got %v", err) } - err = InvokeRpc(context.Background(), refSource, ccProtoset, "FooService/Method", nil, nil, nil) + err = InvokeRPC(context.Background(), refSource, ccProtoset, "FooService/Method", nil, nil, nil) // InvokeRpc wraps the error, so we just verify the returned error includes the right message if err == nil || !strings.Contains(err.Error(), ErrReflectionNotSupported.Error()) { t.Errorf("InvokeRpc should have returned ErrReflectionNotSupported; instead got %v", err) @@ -307,7 +307,7 @@ func TestUnaryReflect(t *testing.T) { func doTestUnary(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) { // Success h := &handler{reqMessages: []string{payload1}} - err := InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/UnaryCall", makeHeaders(codes.OK), h, h.getRequestData) + err := InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/UnaryCall", makeHeaders(codes.OK), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -320,7 +320,7 @@ func doTestUnary(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) { // Failure h = &handler{reqMessages: []string{payload1}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/UnaryCall", makeHeaders(codes.NotFound), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/UnaryCall", makeHeaders(codes.NotFound), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -339,7 +339,7 @@ func TestClientStreamReflect(t *testing.T) { func doTestClientStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) { // Success h := &handler{reqMessages: []string{payload1, payload2, payload3}} - err := InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.OK), h, h.getRequestData) + err := InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.OK), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -356,7 +356,7 @@ func doTestClientStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSour // Fail fast (server rejects as soon as possible) h = &handler{reqMessages: []string{payload1, payload2, payload3}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.InvalidArgument), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.InvalidArgument), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -365,7 +365,7 @@ func doTestClientStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSour // Fail late (server waits until stream is complete to reject) h = &handler{reqMessages: []string{payload1, payload2, payload3}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.Internal, true), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingInputCall", makeHeaders(codes.Internal, true), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -395,7 +395,7 @@ func doTestServerStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSour // Success h := &handler{reqMessages: []string{payload}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.OK), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.OK), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -418,7 +418,7 @@ func doTestServerStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSour // Fail fast (server rejects as soon as possible) h = &handler{reqMessages: []string{payload}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.Aborted), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.Aborted), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -427,7 +427,7 @@ func doTestServerStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSour // Fail late (server waits until stream is complete to reject) h = &handler{reqMessages: []string{payload}} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.AlreadyExists, true), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/StreamingOutputCall", makeHeaders(codes.AlreadyExists, true), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -448,7 +448,7 @@ func doTestHalfDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Success h := &handler{reqMessages: reqs} - err := InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.OK), h, h.getRequestData) + err := InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.OK), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -463,7 +463,7 @@ func doTestHalfDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Fail fast (server rejects as soon as possible) h = &handler{reqMessages: reqs} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.Canceled), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.Canceled), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -472,7 +472,7 @@ func doTestHalfDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Fail late (server waits until stream is complete to reject) h = &handler{reqMessages: reqs} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.DataLoss, true), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/HalfDuplexCall", makeHeaders(codes.DataLoss, true), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -504,7 +504,7 @@ func doTestFullDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Success h := &handler{reqMessages: reqs} - err := InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.OK), h, h.getRequestData) + err := InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.OK), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -535,7 +535,7 @@ func doTestFullDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Fail fast (server rejects as soon as possible) h = &handler{reqMessages: reqs} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.PermissionDenied), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.PermissionDenied), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } @@ -544,7 +544,7 @@ func doTestFullDuplexStream(t *testing.T, cc *grpc.ClientConn, source Descriptor // Fail late (server waits until stream is complete to reject) h = &handler{reqMessages: reqs} - err = InvokeRpc(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.ResourceExhausted, true), h, h.getRequestData) + err = InvokeRPC(context.Background(), source, cc, "grpc.testing.TestService/FullDuplexCall", makeHeaders(codes.ResourceExhausted, true), h, h.getRequestData) if err != nil { t.Fatalf("unexpected error during RPC: %v", err) } diff --git a/testing/test_server.go b/testing/test_server.go index 37a04d1..42fa86f 100644 --- a/testing/test_server.go +++ b/testing/test_server.go @@ -15,9 +15,10 @@ import ( "github.com/fullstorydev/grpcurl" ) +// TestServer is the testserver instance type TestServer struct{} -// One empty request followed by one empty response. +// EmptyCall is One empty request followed by one empty response. func (TestServer) EmptyCall(ctx context.Context, req *grpc_testing.Empty) (*grpc_testing.Empty, error) { headers, trailers, failEarly, failLate := processMetadata(ctx) grpc.SetHeader(ctx, headers) @@ -32,7 +33,7 @@ func (TestServer) EmptyCall(ctx context.Context, req *grpc_testing.Empty) (*grpc return req, nil } -// One request followed by one response. +// UnaryCall is One request followed by one response. // The server returns the client payload as-is. func (TestServer) UnaryCall(ctx context.Context, req *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) { headers, trailers, failEarly, failLate := processMetadata(ctx) @@ -50,7 +51,7 @@ func (TestServer) UnaryCall(ctx context.Context, req *grpc_testing.SimpleRequest }, nil } -// One request followed by a sequence of responses (streamed download). +// StreamingOutputCall is One request followed by a sequence of responses (streamed download). // The server returns the payload with client desired type and sizes. func (TestServer) StreamingOutputCall(req *grpc_testing.StreamingOutputCallRequest, str grpc_testing.TestService_StreamingOutputCallServer) error { headers, trailers, failEarly, failLate := processMetadata(str.Context()) @@ -87,7 +88,7 @@ func (TestServer) StreamingOutputCall(req *grpc_testing.StreamingOutputCallReque return nil } -// A sequence of requests followed by one response (streamed upload). +// StreamingInputCall is A sequence of requests followed by one response (streamed upload). // The server returns the aggregated size of client payload as the result. func (TestServer) StreamingInputCall(str grpc_testing.TestService_StreamingInputCallServer) error { headers, trailers, failEarly, failLate := processMetadata(str.Context()) @@ -103,10 +104,10 @@ func (TestServer) StreamingInputCall(str grpc_testing.TestService_StreamingInput return str.Context().Err() } if req, err := str.Recv(); err != nil { - if err == io.EOF { - break + if err != io.EOF { + return err } - return err + break } else { sz += len(req.Payload.Body) } @@ -121,7 +122,7 @@ func (TestServer) StreamingInputCall(str grpc_testing.TestService_StreamingInput return nil } -// A sequence of requests with each request served by the server immediately. +// FullDuplexCall is A sequence of requests with each request served by the server immediately. // As one request could lead to multiple responses, this interface // demonstrates the idea of full duplexing. func (TestServer) FullDuplexCall(str grpc_testing.TestService_FullDuplexCallServer) error { @@ -163,7 +164,7 @@ func (TestServer) FullDuplexCall(str grpc_testing.TestService_FullDuplexCallServ return nil } -// A sequence of requests followed by a sequence of responses. +// HalfDuplexCall is A sequence of requests followed by a sequence of responses. // The server buffers all the client requests and then serves them in order. A // stream of responses are returned to the client when the server starts with // first request. @@ -181,10 +182,10 @@ func (TestServer) HalfDuplexCall(str grpc_testing.TestService_HalfDuplexCallServ return str.Context().Err() } if req, err := str.Recv(); err != nil { - if err == io.EOF { - break + if err != io.EOF { + return err } - return err + break } else { reqs = append(reqs, req) } @@ -203,6 +204,7 @@ func (TestServer) HalfDuplexCall(str grpc_testing.TestService_HalfDuplexCallServ return nil } +// Metadata const const ( MetadataReplyHeaders = "reply-with-headers" MetadataReplyTrailers = "reply-with-trailers"