You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.1 KiB

  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package health
  19. import (
  20. "sync"
  21. "testing"
  22. "time"
  23. healthpb "google.golang.org/grpc/health/grpc_health_v1"
  24. )
  25. func TestShutdown(t *testing.T) {
  26. const testService = "tteesstt"
  27. s := NewServer()
  28. s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
  29. status := s.statusMap[testService]
  30. if status != healthpb.HealthCheckResponse_SERVING {
  31. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
  32. }
  33. var wg sync.WaitGroup
  34. wg.Add(2)
  35. // Run SetServingStatus and Shutdown in parallel.
  36. go func() {
  37. for i := 0; i < 1000; i++ {
  38. s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
  39. time.Sleep(time.Microsecond)
  40. }
  41. wg.Done()
  42. }()
  43. go func() {
  44. time.Sleep(300 * time.Microsecond)
  45. s.Shutdown()
  46. wg.Done()
  47. }()
  48. wg.Wait()
  49. s.mu.Lock()
  50. status = s.statusMap[testService]
  51. s.mu.Unlock()
  52. if status != healthpb.HealthCheckResponse_NOT_SERVING {
  53. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
  54. }
  55. s.Resume()
  56. status = s.statusMap[testService]
  57. if status != healthpb.HealthCheckResponse_SERVING {
  58. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
  59. }
  60. s.SetServingStatus(testService, healthpb.HealthCheckResponse_NOT_SERVING)
  61. status = s.statusMap[testService]
  62. if status != healthpb.HealthCheckResponse_NOT_SERVING {
  63. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
  64. }
  65. }