Repository: grpc/grpc-go
File: health/server_internal_test.go (Go)

Code before:
	for i := 1; i <= length; i++ {
		s.SetServingStatus(fmt.Sprintf("%d", i),
			healthpb.HealthCheckResponse_SERVING)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	var in healthpb.HealthListRequest
	got, err := s.List(ctx, &in)

	if err != nil {
		t.Fatalf("s.List(ctx, &in) returned err %v, want nil", err)
	}
	if len(got.GetStatuses()) != length {
		t.Fatalf("len(out.GetStatuses()) = %d, want %d",
			len(got.GetStatuses()), length)
	}
	want := &healthpb.HealthListResponse{
		Statuses: map[string]*healthpb.HealthCheckResponse{
			"1": {Status: healthpb.HealthCheckResponse_SERVING},
			"2": {Status: healthpb.HealthCheckResponse_SERVING},
			"3": {Status: healthpb.HealthCheckResponse_SERVING},
		},
	}
	if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
		t.Fatalf("....: %s", diff)
	}
}

// TestListResourceExhausted verifies that List(
// ) returns a ResourceExhausted error if no. of services are more than
// maxAllowedServices.
func (s) TestListResourceExhausted(t *testing.T) {
	s := NewServer()
	s.mu.Lock()
	// Remove the zero value
	delete(s.statusMap, "")
	s.mu.Unlock()

	// Fill out status map with service information, 101 elements will trigger an error.
	for i := 1; i <= maxAllowedServices+1; i++ {
		s.SetServingStatus(fmt.Sprintf("%d", i),
			healthpb.HealthCheckResponse_SERVING)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	var in healthpb.HealthListRequest
	_, err := s.List(ctx, &in)

	want := status.Errorf(codes.ResourceExhausted,

Review comment (suggestion):
Here I meant to write an error string that explains what happened, not literally `"...."`. :)

Something like `"Health response did not match expectation.  Diff (-got, +want): %s"`.

Code after:
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	var in healthpb.HealthListRequest
	got, err := s.List(ctx, &in)

	if err != nil {
		t.Fatalf("s.List(ctx, &in) returned err %v, want nil", err)
	}
	if len(got.GetStatuses()) != length+1 {
		t.Fatalf("len(out.GetStatuses()) = %d, want %d",
			len(got.GetStatuses()), length+1)
	}
	want := &healthpb.HealthListResponse{
		Statuses: map[string]*healthpb.HealthCheckResponse{
			"":  {Status: healthpb.HealthCheckResponse_SERVING},
			"0": {Status: healthpb.HealthCheckResponse_UNKNOWN},
			"1": {Status: healthpb.HealthCheckResponse_SERVING},
			"2": {Status: healthpb.HealthCheckResponse_NOT_SERVING},
		},
	}
	if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
		t.Fatalf("Health response did not match expectation.  Diff (-got, +want): %s", diff)
	}
}

// TestListResourceExhausted verifies that List()
// returns a ResourceExhausted error if no. of services are more than
// maxAllowedServices.
func (s) TestListResourceExhausted(t *testing.T) {
	s := NewServer()

	// Fill out status map with service information,
	// 101 (100 + 1 existing) elements will trigger an error.
	for i := 1; i <= maxAllowedServices; i++ {
		s.SetServingStatus(fmt.Sprintf("%d", i),
			healthpb.HealthCheckResponse_SERVING)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	var in healthpb.HealthListRequest
	_, err := s.List(ctx, &in)

	want := status.Errorf(codes.ResourceExhausted,
		"server health list exceeds maximum capacity: %d", maxAllowedServices)
	if !errors.Is(err, want) {
		t.Fatalf("s.List(ctx, &in) returned %v, want %v", err, want)
	}
}