change the implementation of healthcheck and corresponding tests

This commit is contained in:
yangzhouhan
2015-06-16 16:13:51 -07:00
parent dbf1048b08
commit 272079efe3
4 changed files with 133 additions and 23 deletions

@ -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{},
}

@ -0,0 +1,13 @@
syntax = "proto3";
package grpc.health;
message HealthCheckRequest{
}
message HealthCheckResponse{
}
service HealthCheck{
rpc Check( HealthCheckRequest) returns ( HealthCheckResponse);
}

@ -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
}

@ -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 <nil>", 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 <DeadlineExcced>", 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 <unimplemented>", err)
}
}