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

Code before:
		s.Shutdown()
		wg.Done()
	}()
	wg.Wait()

	s.mu.Lock()
	status = s.statusMap[testService]
	s.mu.Unlock()
	if status != healthpb.HealthCheckResponse_NOT_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
	}

	s.Resume()
	status = s.statusMap[testService]
	if status != healthpb.HealthCheckResponse_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
	}

	s.SetServingStatus(testService, healthpb.HealthCheckResponse_NOT_SERVING)
	status = s.statusMap[testService]
	if status != healthpb.HealthCheckResponse_NOT_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
	}
}

// TestList verifies successful listing of all service health statuses.
func (s) TestList(t *testing.T) {
	// Setup
	s := NewServer()
	s.mu.Lock()
	// Remove the zero value
	delete(s.statusMap, "")
	// Fill out status map with information
	for i := 1; i <= 3; i++ {
		s.statusMap[fmt.Sprintf("%d", i)] = healthpb.HealthCheckResponse_SERVING
	}
	s.mu.Unlock()

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

	// Assertions
	if err != nil {
		t.Fatalf("s.List(ctx, &in) returned err %v, want nil", err)
	}
	if len(out.GetStatuses()) != len(s.statusMap) {
		t.Fatalf("len(out.GetStatuses()) = %d, want %d", len(out.GetStatuses()), len(s.statusMap))
	}

Review comment (suggestion):
```suggestion
// TestList verifies that List() returns the health status of all the services if no. of services are within maxAllowedLimits
```

Code after:
	}()
	go func() {
		time.Sleep(300 * time.Microsecond)
		s.Shutdown()
		wg.Done()
	}()
	wg.Wait()

	s.mu.Lock()
	status = s.statusMap[testService]
	s.mu.Unlock()
	if status != healthpb.HealthCheckResponse_NOT_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
	}

	s.Resume()
	status = s.statusMap[testService]
	if status != healthpb.HealthCheckResponse_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
	}

	s.SetServingStatus(testService, healthpb.HealthCheckResponse_NOT_SERVING)
	status = s.statusMap[testService]
	if status != healthpb.HealthCheckResponse_NOT_SERVING {
		t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
	}
}

// TestList verifies that List() returns the health status of all the services if no. of services are within
// maxAllowedLimits.
func (s) TestList(t *testing.T) {
	s := NewServer()

	// Fill out status map with information
	const length = 3
	for i := 0; i < length; i++ {
		s.SetServingStatus(fmt.Sprintf("%d", i),
			healthpb.HealthCheckResponse_ServingStatus(i))
	}

	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)