|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + fmt "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "golang.org/x/net/context" |
| 9 | + grpc "google.golang.org/grpc" |
| 10 | + "google.golang.org/grpc/health/grpc_health_v1" |
| 11 | +) |
| 12 | + |
| 13 | +type mockIngester struct { |
| 14 | + IngesterClient |
| 15 | + happy bool |
| 16 | + status grpc_health_v1.HealthCheckResponse_ServingStatus |
| 17 | +} |
| 18 | + |
| 19 | +func (i mockIngester) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) { |
| 20 | + if !i.happy { |
| 21 | + return nil, fmt.Errorf("Fail") |
| 22 | + } |
| 23 | + return &grpc_health_v1.HealthCheckResponse{Status: i.status}, nil |
| 24 | +} |
| 25 | + |
| 26 | +func TestHealthCheck(t *testing.T) { |
| 27 | + tcs := []struct { |
| 28 | + ingester mockIngester |
| 29 | + hasError bool |
| 30 | + }{ |
| 31 | + {mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true}, |
| 32 | + {mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, false}, |
| 33 | + {mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true}, |
| 34 | + {mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true}, |
| 35 | + {mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_SERVING}, true}, |
| 36 | + {mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true}, |
| 37 | + } |
| 38 | + for _, tc := range tcs { |
| 39 | + err := HealthCheck(tc.ingester, 50*time.Millisecond) |
| 40 | + hasError := err != nil |
| 41 | + if hasError != tc.hasError { |
| 42 | + t.Errorf("Expected error: %t, error: %v", tc.hasError, err) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments