github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/health/server_internal_test.go (about)

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