From bd29c7cc2a37841c45e176713923d09bf77b8e5f Mon Sep 17 00:00:00 2001 From: yangzhouhan Date: Tue, 16 Jun 2015 17:11:12 -0700 Subject: [PATCH] change healthcheck end2end test --- health/health.go | 18 ++++++++---------- test/end2end_test.go | 11 +++++------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/health/health.go b/health/health.go index 2d73fb46..3089f3e4 100644 --- a/health/health.go +++ b/health/health.go @@ -1,6 +1,5 @@ -// 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 provides some utility functions to health-check a server. The implementation +// is based on protobuf. Users need to write their own implementations if other IDLs are used. package health import ( @@ -11,20 +10,19 @@ import ( healthpb "google.golang.org/grpc/health/grpc_health" ) +// HealthCheck is the client side function to health-check a server func HealthCheck(t time.Duration, cc *grpc.ClientConn) error { ctx, _ := context.WithTimeout(context.Background(), t) hc := healthpb.NewHealthCheckClient(cc) - req :=new(healthpb.HealthCheckRequest) - if _,err := hc.Check(ctx, req); err != nil { - return err - } - return nil + req := new(healthpb.HealthCheckRequest) + _, err := hc.Check(ctx, req) + return err } type HealthServer struct { } -func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest)(*healthpb.HealthCheckResponse, error){ +func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) { out := new(healthpb.HealthCheckResponse) - return out,nil + return out, nil } diff --git a/test/end2end_test.go b/test/end2end_test.go index 610f0224..b00a487f 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -306,7 +306,6 @@ func setUp(healthCheck bool, maxStream uint32, e env) (s *grpc.Server, cc *grpc. } s = grpc.NewServer(sopts...) if healthCheck { - healthpb.RegisterHealthCheckServer(s, &health.HealthServer{}) } testpb.RegisterTestServiceServer(s, &testServer{}) @@ -361,13 +360,13 @@ func testTimeoutOnDeadServer(t *testing.T, e env) { cc.Close() } -func TestHealthCheckOnSucceed(t *testing.T) { +func TestHealthCheckOnSuccess(t *testing.T) { for _, e := range listTestEnv() { - testHealthCheckOnSucceed(t, e) + testHealthCheckOnSuccess(t, e) } } -func testHealthCheckOnSucceed(t *testing.T, e env) { +func testHealthCheckOnSuccess(t *testing.T, e env) { s, cc := setUp(true, math.MaxUint32, e) defer tearDown(s, cc) if err := health.HealthCheck(1*time.Second, cc); err != nil { @@ -385,7 +384,7 @@ func testHealthCheckOnFailure(t *testing.T, e env) { s, cc := setUp(true, math.MaxUint32, e) defer tearDown(s, cc) if err := health.HealthCheck(0*time.Second, cc); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") { - t.Fatalf("HealthCheck(_)=_, %v, want ", err) + t.Fatalf("HealthCheck(_)=_, %v, want error code %d", err, codes.DeadlineExceeded) } } @@ -400,7 +399,7 @@ func testHealthCheckOff(t *testing.T, e env) { defer tearDown(s, cc) err := health.HealthCheck(1*time.Second, cc) if err != grpc.Errorf(codes.Unimplemented, "unknown service grpc.health.HealthCheck") { - t.Fatalf("HealthCheck(_)=_, %v, want ", err) + t.Fatalf("HealthCheck(_)=_, %v, want error code %d", err, codes.DeadlineExceeded) } }