github.com/polarismesh/polaris@v1.17.8/service/client_check_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package service_test
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"testing"
    24  	"time"
    25  
    26  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    27  	"github.com/stretchr/testify/assert"
    28  	"google.golang.org/protobuf/types/known/wrapperspb"
    29  )
    30  
    31  func TestClientCheck(t *testing.T) {
    32  	discoverSuit := &DiscoverTestSuit{}
    33  	if err := discoverSuit.Initialize(); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer discoverSuit.Destroy()
    37  
    38  	clientId1 := "111"
    39  	clientId2 := "222"
    40  
    41  	discoverSuit.addInstance(t, &apiservice.Instance{
    42  		Service:   wrapperspb.String("polaris.checker"),
    43  		Namespace: wrapperspb.String("Polaris"),
    44  		Host:      wrapperspb.String("127.0.0.1"),
    45  		Port:      wrapperspb.UInt32(8091),
    46  		Protocol:  wrapperspb.String("grpc"),
    47  		Metadata:  map[string]string{"polaris_service": "polaris.checker"},
    48  	})
    49  	time.Sleep(20 * time.Second)
    50  	clientIds := map[string]bool{clientId1: true, clientId2: true}
    51  	for i := 0; i < 50; i++ {
    52  		for clientId := range clientIds {
    53  			fmt.Printf("%d report client for %s, round 1\n", i, clientId)
    54  			discoverSuit.DiscoverServer().ReportClient(context.Background(),
    55  				&apiservice.Client{
    56  					Id: &wrapperspb.StringValue{Value: clientId}, Host: &wrapperspb.StringValue{Value: "127.0.0.1"}})
    57  		}
    58  		time.Sleep(1 * time.Second)
    59  	}
    60  
    61  	client1 := discoverSuit.DiscoverServer().Cache().Client().GetClient(clientId1)
    62  	assert.NotNil(t, client1)
    63  	client2 := discoverSuit.DiscoverServer().Cache().Client().GetClient(clientId2)
    64  	assert.NotNil(t, client2)
    65  
    66  	delete(clientIds, clientId2)
    67  	for i := 0; i < 50; i++ {
    68  		for clientId := range clientIds {
    69  			fmt.Printf("%d report client for %s, round 2\n", i, clientId)
    70  			discoverSuit.DiscoverServer().ReportClient(context.Background(),
    71  				&apiservice.Client{Id: &wrapperspb.StringValue{Value: clientId},
    72  					Host: &wrapperspb.StringValue{Value: "127.0.0.1"}, Type: apiservice.Client_SDK})
    73  		}
    74  		time.Sleep(1 * time.Second)
    75  	}
    76  	client1 = discoverSuit.DiscoverServer().Cache().Client().GetClient(clientId1)
    77  	assert.NotNil(t, client1)
    78  	client2 = discoverSuit.DiscoverServer().Cache().Client().GetClient(clientId2)
    79  	assert.Nil(t, client2)
    80  }