server_internal_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "google.golang.org/grpc/internal/grpctest"
  25. )
  26. type s struct {
  27. grpctest.Tester
  28. }
  29. func Test(t *testing.T) {
  30. grpctest.RunSubTests(t, s{})
  31. }
  32. func (s) TestShutdown(t *testing.T) {
  33. const testService = "tteesstt"
  34. s := NewServer()
  35. s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
  36. status := s.statusMap[testService]
  37. if status != healthpb.HealthCheckResponse_SERVING {
  38. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
  39. }
  40. var wg sync.WaitGroup
  41. wg.Add(2)
  42. // Run SetServingStatus and Shutdown in parallel.
  43. go func() {
  44. for i := 0; i < 1000; i++ {
  45. s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
  46. time.Sleep(time.Microsecond)
  47. }
  48. wg.Done()
  49. }()
  50. go func() {
  51. time.Sleep(300 * time.Microsecond)
  52. s.Shutdown()
  53. wg.Done()
  54. }()
  55. wg.Wait()
  56. s.mu.Lock()
  57. status = s.statusMap[testService]
  58. s.mu.Unlock()
  59. if status != healthpb.HealthCheckResponse_NOT_SERVING {
  60. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
  61. }
  62. s.Resume()
  63. status = s.statusMap[testService]
  64. if status != healthpb.HealthCheckResponse_SERVING {
  65. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
  66. }
  67. s.SetServingStatus(testService, healthpb.HealthCheckResponse_NOT_SERVING)
  68. status = s.statusMap[testService]
  69. if status != healthpb.HealthCheckResponse_NOT_SERVING {
  70. t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
  71. }
  72. }