|
19 | 19 | package health
|
20 | 20 |
|
21 | 21 | import (
|
| 22 | + "context" |
| 23 | + "errors" |
| 24 | + "fmt" |
22 | 25 | "sync"
|
23 | 26 | "testing"
|
24 | 27 | "time"
|
25 | 28 |
|
| 29 | + "github.com/google/go-cmp/cmp" |
| 30 | + "google.golang.org/grpc/codes" |
| 31 | + "google.golang.org/grpc/status" |
| 32 | + "google.golang.org/protobuf/testing/protocmp" |
| 33 | + |
26 | 34 | healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
27 | 35 | "google.golang.org/grpc/internal/grpctest"
|
28 | 36 | )
|
@@ -81,3 +89,65 @@ func (s) TestShutdown(t *testing.T) {
|
81 | 89 | t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
|
82 | 90 | }
|
83 | 91 | }
|
| 92 | + |
| 93 | +// TestList verifies that List() returns the health status of all the services if no. of services are within |
| 94 | +// maxAllowedLimits. |
| 95 | +func (s) TestList(t *testing.T) { |
| 96 | + s := NewServer() |
| 97 | + |
| 98 | + // Fill out status map with information |
| 99 | + const length = 3 |
| 100 | + for i := 0; i < length; i++ { |
| 101 | + s.SetServingStatus(fmt.Sprintf("%d", i), |
| 102 | + healthpb.HealthCheckResponse_ServingStatus(i)) |
| 103 | + } |
| 104 | + |
| 105 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 106 | + defer cancel() |
| 107 | + var in healthpb.HealthListRequest |
| 108 | + got, err := s.List(ctx, &in) |
| 109 | + |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("s.List(ctx, &in) returned err %v, want nil", err) |
| 112 | + } |
| 113 | + if len(got.GetStatuses()) != length+1 { |
| 114 | + t.Fatalf("len(out.GetStatuses()) = %d, want %d", |
| 115 | + len(got.GetStatuses()), length+1) |
| 116 | + } |
| 117 | + want := &healthpb.HealthListResponse{ |
| 118 | + Statuses: map[string]*healthpb.HealthCheckResponse{ |
| 119 | + "": {Status: healthpb.HealthCheckResponse_SERVING}, |
| 120 | + "0": {Status: healthpb.HealthCheckResponse_UNKNOWN}, |
| 121 | + "1": {Status: healthpb.HealthCheckResponse_SERVING}, |
| 122 | + "2": {Status: healthpb.HealthCheckResponse_NOT_SERVING}, |
| 123 | + }, |
| 124 | + } |
| 125 | + if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { |
| 126 | + t.Fatalf("Health response did not match expectation. Diff (-got, +want): %s", diff) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TestListResourceExhausted verifies that List() |
| 131 | +// returns a ResourceExhausted error if no. of services are more than |
| 132 | +// maxAllowedServices. |
| 133 | +func (s) TestListResourceExhausted(t *testing.T) { |
| 134 | + s := NewServer() |
| 135 | + |
| 136 | + // Fill out status map with service information, |
| 137 | + // 101 (100 + 1 existing) elements will trigger an error. |
| 138 | + for i := 1; i <= maxAllowedServices; i++ { |
| 139 | + s.SetServingStatus(fmt.Sprintf("%d", i), |
| 140 | + healthpb.HealthCheckResponse_SERVING) |
| 141 | + } |
| 142 | + |
| 143 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 144 | + defer cancel() |
| 145 | + var in healthpb.HealthListRequest |
| 146 | + _, err := s.List(ctx, &in) |
| 147 | + |
| 148 | + want := status.Errorf(codes.ResourceExhausted, |
| 149 | + "server health list exceeds maximum capacity: %d", maxAllowedServices) |
| 150 | + if !errors.Is(err, want) { |
| 151 | + t.Fatalf("s.List(ctx, &in) returned %v, want %v", err, want) |
| 152 | + } |
| 153 | +} |
0 commit comments