github.com/polarismesh/polaris@v1.17.8/store/boltdb/client_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 boltdb
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    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  	"github.com/polarismesh/polaris/common/model"
    31  )
    32  
    33  func Test_ConvertToClientObject(t *testing.T) {
    34  	client := &apiservice.Client{
    35  		Host:    &wrapperspb.StringValue{Value: "1"},
    36  		Type:    0,
    37  		Version: &wrapperspb.StringValue{Value: "1"},
    38  		Location: &apimodel.Location{
    39  			Region: &wrapperspb.StringValue{Value: "1"},
    40  			Zone:   &wrapperspb.StringValue{Value: "1"},
    41  			Campus: &wrapperspb.StringValue{Value: "1"},
    42  		},
    43  		Id: &wrapperspb.StringValue{Value: "1"},
    44  		Stat: []*apiservice.StatInfo{
    45  			{
    46  				Target:   &wrapperspb.StringValue{Value: "prometheus"},
    47  				Port:     &wrapperspb.UInt32Value{Value: 8080},
    48  				Path:     &wrapperspb.StringValue{Value: "/metrics"},
    49  				Protocol: &wrapperspb.StringValue{Value: "http"},
    50  			},
    51  		},
    52  	}
    53  
    54  	ret, err := convertToClientObject(model.NewClient(client))
    55  	assert.NoError(t, err)
    56  
    57  	cop, err := convertToModelClient(ret)
    58  	assert.NoError(t, err)
    59  
    60  	cop.Proto().Mtime = nil
    61  	cop.Proto().Ctime = nil
    62  
    63  	assert.Equal(t, client, cop.Proto())
    64  }
    65  
    66  func createMockClients(total int) []*model.Client {
    67  	ret := make([]*model.Client, 0, total)
    68  
    69  	for i := 0; i < total; i++ {
    70  		ret = append(ret, model.NewClient(&apiservice.Client{
    71  			Host:    &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d", i)},
    72  			Type:    0,
    73  			Version: &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d", i)},
    74  			Location: &apimodel.Location{
    75  				Region: &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d-region", i)},
    76  				Zone:   &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d-zone", i)},
    77  				Campus: &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d-campus", i)},
    78  			},
    79  			Id: &wrapperspb.StringValue{Value: fmt.Sprintf("client-%d", i)},
    80  			Stat: []*apiservice.StatInfo{
    81  				{
    82  					Target:   &wrapperspb.StringValue{Value: "prometheus"},
    83  					Port:     &wrapperspb.UInt32Value{Value: 8080},
    84  					Path:     &wrapperspb.StringValue{Value: "/metrics"},
    85  					Protocol: &wrapperspb.StringValue{Value: "http"},
    86  				},
    87  			},
    88  		}))
    89  	}
    90  
    91  	return ret
    92  }
    93  
    94  func Test_clientStore_GetMoreClients(t *testing.T) {
    95  	CreateTableDBHandlerAndRun(t, "Test_clientStore_GetMoreClients", func(t *testing.T, handler BoltHandler) {
    96  		cStore := &clientStore{handler: handler}
    97  
    98  		total := 10
    99  
   100  		mockClients := createMockClients(total)
   101  		err := cStore.BatchAddClients(mockClients)
   102  		assert.NoError(t, err, "batch create clients")
   103  
   104  		time.Sleep(time.Second)
   105  
   106  		// 首次拉取, mtime 不做处理
   107  		ret, err := cStore.GetMoreClients(time.Now().Add(-1*time.Minute), true)
   108  		assert.NoError(t, err, "get more clients")
   109  		assert.Equal(t, total, len(ret), "get more clients")
   110  
   111  		// 非次拉取, mtime 做处理
   112  		ret, err = cStore.GetMoreClients(time.Now().Add(-1*time.Minute), false)
   113  		assert.NoError(t, err, "get more clients")
   114  		assert.Equal(t, total, len(ret), "get more clients")
   115  
   116  		// 非次拉取, mtime 做处理
   117  		ret, err = cStore.GetMoreClients(time.Now().Add(10*time.Minute), false)
   118  		assert.NoError(t, err, "get more clients")
   119  		assert.Equal(t, 0, len(ret), "get more clients")
   120  	})
   121  }