From 272079efe3ffad8369e6876bada4bac25f856cfe Mon Sep 17 00:00:00 2001 From: yangzhouhan Date: Tue, 16 Jun 2015 16:13:51 -0700 Subject: [PATCH] change the implementation of healthcheck and corresponding tests --- health/grpc_health/health.pb.go | 103 ++++++++++++++++++++++++++++++++ health/grpc_health/health.proto | 13 ++++ health/health.go | 18 +++--- test/end2end_test.go | 22 +++---- 4 files changed, 133 insertions(+), 23 deletions(-) create mode 100644 health/grpc_health/health.pb.go create mode 100644 health/grpc_health/health.proto diff --git a/health/grpc_health/health.pb.go b/health/grpc_health/health.pb.go new file mode 100644 index 00000000..068597f8 --- /dev/null +++ b/health/grpc_health/health.pb.go @@ -0,0 +1,103 @@ +// Code generated by protoc-gen-go. +// source: health.proto +// DO NOT EDIT! + +/* +Package grpc_health is a generated protocol buffer package. + +It is generated from these files: + health.proto + +It has these top-level messages: + HealthCheckRequest + HealthCheckResponse +*/ +package grpc_health + +import proto "github.com/golang/protobuf/proto" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal + +type HealthCheckRequest struct { +} + +func (m *HealthCheckRequest) Reset() { *m = HealthCheckRequest{} } +func (m *HealthCheckRequest) String() string { return proto.CompactTextString(m) } +func (*HealthCheckRequest) ProtoMessage() {} + +type HealthCheckResponse struct { +} + +func (m *HealthCheckResponse) Reset() { *m = HealthCheckResponse{} } +func (m *HealthCheckResponse) String() string { return proto.CompactTextString(m) } +func (*HealthCheckResponse) ProtoMessage() {} + +func init() { +} + +// Client API for HealthCheck service + +type HealthCheckClient interface { + Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) +} + +type healthCheckClient struct { + cc *grpc.ClientConn +} + +func NewHealthCheckClient(cc *grpc.ClientConn) HealthCheckClient { + return &healthCheckClient{cc} +} + +func (c *healthCheckClient) Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) { + out := new(HealthCheckResponse) + err := grpc.Invoke(ctx, "/grpc.health.HealthCheck/Check", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for HealthCheck service + +type HealthCheckServer interface { + Check(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error) +} + +func RegisterHealthCheckServer(s *grpc.Server, srv HealthCheckServer) { + s.RegisterService(&_HealthCheck_serviceDesc, srv) +} + +func _HealthCheck_Check_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) { + in := new(HealthCheckRequest) + if err := codec.Unmarshal(buf, in); err != nil { + return nil, err + } + out, err := srv.(HealthCheckServer).Check(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + +var _HealthCheck_serviceDesc = grpc.ServiceDesc{ + ServiceName: "grpc.health.HealthCheck", + HandlerType: (*HealthCheckServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Check", + Handler: _HealthCheck_Check_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, +} diff --git a/health/grpc_health/health.proto b/health/grpc_health/health.proto new file mode 100644 index 00000000..40ae67d9 --- /dev/null +++ b/health/grpc_health/health.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package grpc.health; + +message HealthCheckRequest{ +} + +message HealthCheckResponse{ +} + +service HealthCheck{ + rpc Check( HealthCheckRequest) returns ( HealthCheckResponse); +} diff --git a/health/health.go b/health/health.go index 232b80ce..2d73fb46 100644 --- a/health/health.go +++ b/health/health.go @@ -1,17 +1,21 @@ +// health is depended on the code generated by protobuf +// For the users who want to implement their own stuff, +// should rewrite this service. package health -//import proto "github.com/golang/protobuf/proto" import ( "time" "golang.org/x/net/context" "google.golang.org/grpc" + healthpb "google.golang.org/grpc/health/grpc_health" ) -func HealthCheck(t time.Duration, cc *grpc.ClientConn, req interface{}, rep interface{}) error { +func HealthCheck(t time.Duration, cc *grpc.ClientConn) error { ctx, _ := context.WithTimeout(context.Background(), t) - //out := - if err := grpc.Invoke(ctx, "health.HealthCheck/HealthCheck", req, rep, cc); err != nil { + hc := healthpb.NewHealthCheckClient(cc) + req :=new(healthpb.HealthCheckRequest) + if _,err := hc.Check(ctx, req); err != nil { return err } return nil @@ -20,9 +24,7 @@ func HealthCheck(t time.Duration, cc *grpc.ClientConn, req interface{}, rep inte type HealthServer struct { } -func (s *HealthServer) HealthCheck(ctx context.Context, in *HealthCheckRequest)(*HealthCheckResponse, error){ - out := new(HealthCheckResponse) +func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest)(*healthpb.HealthCheckResponse, error){ + out := new(healthpb.HealthCheckResponse) return out,nil } - - diff --git a/test/end2end_test.go b/test/end2end_test.go index d0e3f539..610f0224 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -51,9 +51,10 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/health" + healthpb "google.golang.org/grpc/health/grpc_health" "google.golang.org/grpc/metadata" testpb "google.golang.org/grpc/test/grpc_testing" - "google.golang.org/grpc/health" ) var ( @@ -306,7 +307,7 @@ func setUp(healthCheck bool, maxStream uint32, e env) (s *grpc.Server, cc *grpc. s = grpc.NewServer(sopts...) if healthCheck { - health.RegisterHealthCheckServer(s,&health.HealthServer{}) + healthpb.RegisterHealthCheckServer(s, &health.HealthServer{}) } testpb.RegisterTestServiceServer(s, &testServer{}) go s.Serve(lis) @@ -369,10 +370,7 @@ func TestHealthCheckOnSucceed(t *testing.T) { func testHealthCheckOnSucceed(t *testing.T, e env) { s, cc := setUp(true, math.MaxUint32, e) defer tearDown(s, cc) - in := new(health.HealthCheckRequest) - out := new(health.HealthCheckResponse) - //hc := health.NewHealthCheckMessage() - if err := health.HealthCheck(1*time.Second, cc, in, out); err != nil { + if err := health.HealthCheck(1*time.Second, cc); err != nil { t.Fatalf("HealthCheck(_)=_, %v, want ", err) } } @@ -386,10 +384,7 @@ func TestHealthCheckOnFailure(t *testing.T) { func testHealthCheckOnFailure(t *testing.T, e env) { s, cc := setUp(true, math.MaxUint32, e) defer tearDown(s, cc) - in := new(health.HealthCheckRequest) - out := new(health.HealthCheckResponse) - //hc := health.NewHealthCheckMessage() - if err := health.HealthCheck(0*time.Second, cc, in, out); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") { + if err := health.HealthCheck(0*time.Second, cc); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") { t.Fatalf("HealthCheck(_)=_, %v, want ", err) } } @@ -403,11 +398,8 @@ func TestHealthCheckOff(t *testing.T) { func testHealthCheckOff(t *testing.T, e env) { s, cc := setUp(false, math.MaxUint32, e) defer tearDown(s, cc) - in := new(health.HealthCheckRequest) - out := new(health.HealthCheckResponse) - //hc :=health.NewHealthCheckMessage() - err := health.HealthCheck(1 * time.Second, cc, in, out) - if err != grpc.Errorf(codes.Unimplemented, "unknown service health.HealthCheck") { + err := health.HealthCheck(1*time.Second, cc) + if err != grpc.Errorf(codes.Unimplemented, "unknown service grpc.health.HealthCheck") { t.Fatalf("HealthCheck(_)=_, %v, want ", err) } }