github.com/polarismesh/polaris@v1.17.8/cache/client/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 cache_client 19 20 import ( 21 "fmt" 22 "testing" 23 "time" 24 25 "github.com/golang/mock/gomock" 26 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 27 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 28 "github.com/stretchr/testify/assert" 29 30 types "github.com/polarismesh/polaris/cache/api" 31 "github.com/polarismesh/polaris/common/model" 32 "github.com/polarismesh/polaris/common/utils" 33 "github.com/polarismesh/polaris/store/mock" 34 ) 35 36 func newTestClientCache(t *testing.T) (*gomock.Controller, *mock.MockStore, *clientCache) { 37 ctl := gomock.NewController(t) 38 39 var cacheMgr types.CacheManager 40 41 storage := mock.NewMockStore(ctl) 42 rlc := NewClientCache(storage, cacheMgr) 43 storage.EXPECT().GetUnixSecond(gomock.Any()).AnyTimes().Return(time.Now().Unix(), nil) 44 var opt map[string]interface{} 45 _ = rlc.Initialize(opt) 46 return ctl, storage, rlc.(*clientCache) 47 } 48 49 func mockClients(cnt int) map[string]*model.Client { 50 ret := make(map[string]*model.Client) 51 52 for i := 0; i < cnt; i++ { 53 54 id := utils.NewUUID() 55 56 ret[id] = model.NewClient(&apiservice.Client{ 57 Host: utils.NewStringValue(fmt.Sprintf("127.0.0.%d", i+1)), 58 Type: 0, 59 Version: utils.NewStringValue("v1.0.0"), 60 Location: &apimodel.Location{ 61 Region: utils.NewStringValue("region"), 62 Zone: utils.NewStringValue("zone"), 63 Campus: utils.NewStringValue("campus"), 64 }, 65 Id: utils.NewStringValue(id), 66 Stat: []*apiservice.StatInfo{}, 67 }) 68 69 ret[id].SetValid(true) 70 } 71 72 return ret 73 } 74 75 func Test_clientCache_GetClient(t *testing.T) { 76 77 t.Run("测试正常的client实例缓存获取", func(t *testing.T) { 78 ctrl, store, clientCache := newTestClientCache(t) 79 defer ctrl.Finish() 80 81 ret := mockClients(10) 82 83 id := "" 84 for k := range ret { 85 id = k 86 break 87 } 88 89 store.EXPECT().GetMoreClients(gomock.Any(), gomock.Any()).Return(ret, nil) 90 91 err := clientCache.Update() 92 assert.NoError(t, err) 93 94 item := clientCache.GetClient(id) 95 assert.Equal(t, ret[id], item) 96 }) 97 } 98 99 func Test_clientCache_GetClientByFilter(t *testing.T) { 100 101 t.Run("测试带条件的client过滤查询", func(t *testing.T) { 102 ctrl, store, clientCache := newTestClientCache(t) 103 defer ctrl.Finish() 104 105 ret := mockClients(10) 106 107 id := "" 108 host := "" 109 for k := range ret { 110 id = k 111 host = ret[id].Proto().Host.Value 112 break 113 } 114 115 store.EXPECT().GetMoreClients(gomock.Any(), gomock.Any()).Return(ret, nil) 116 117 err := clientCache.Update() 118 assert.NoError(t, err) 119 120 total, item, err := clientCache.GetClientsByFilter(map[string]string{ 121 "id": id, 122 "host": host, 123 }, 0, 100) 124 assert.NoError(t, err) 125 assert.Equal(t, int32(1), int32(total)) 126 assert.Equal(t, int32(1), int32(len(item))) 127 assert.Equal(t, ret[id], item[0]) 128 }) 129 }