github.com/polarismesh/polaris@v1.17.8/cache/cache_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_test 19 20 import ( 21 "context" 22 "testing" 23 "time" 24 25 "github.com/golang/mock/gomock" 26 "github.com/stretchr/testify/assert" 27 28 "github.com/polarismesh/polaris/cache" 29 types "github.com/polarismesh/polaris/cache/api" 30 "github.com/polarismesh/polaris/store/mock" 31 ) 32 33 // TestCacheManager_Start 测试cache函数是否正常 34 func TestCacheManager_Start(t *testing.T) { 35 ctl := gomock.NewController(t) 36 storage := mock.NewMockStore(ctl) 37 storage.EXPECT().GetUnixSecond(gomock.Any()).AnyTimes().Return(time.Now().Unix(), nil) 38 defer ctl.Finish() 39 40 conf := &cache.Config{ 41 Open: true, 42 Resources: []cache.ConfigEntry{ 43 { 44 Name: "service", 45 }, 46 { 47 Name: "instance", 48 }, 49 { 50 Name: "routingConfig", 51 }, 52 { 53 Name: "rateLimitConfig", 54 }, 55 { 56 Name: "circuitBreakerConfig", 57 }, 58 { 59 Name: "l5", 60 }, 61 }, 62 } 63 cache.SetCacheConfig(conf) 64 65 t.Run("测试正常的更新缓存逻辑", func(t *testing.T) { 66 c, err := cache.TestCacheInitialize(context.Background(), &cache.Config{Open: true}, storage) 67 assert.Nil(t, err) 68 assert.NotNil(t, c) 69 beg := time.Unix(0, 0).Add(types.DefaultTimeDiff) 70 storage.EXPECT().GetUnixSecond(gomock.Any()).AnyTimes().Return(time.Now().Unix(), nil) 71 storage.EXPECT().GetMoreInstances(gomock.Any(), beg, true, false, nil).Return(nil, nil).MaxTimes(1) 72 storage.EXPECT().GetMoreInstances(gomock.Any(), beg, false, false, nil).Return(nil, nil).MaxTimes(3) 73 storage.EXPECT().GetMoreServices(beg, true, false, false).Return(nil, nil).MaxTimes(1) 74 storage.EXPECT().GetMoreServices(beg, false, false, false).Return(nil, nil).MaxTimes(3) 75 storage.EXPECT().GetRoutingConfigsForCache(beg, true).Return(nil, nil).MaxTimes(3) 76 storage.EXPECT().GetRoutingConfigsForCache(beg, false).Return(nil, nil).MaxTimes(3) 77 storage.EXPECT().GetMoreL5Routes(uint32(0)).Return(nil, nil).MaxTimes(3) 78 storage.EXPECT().GetMoreL5Policies(uint32(0)).Return(nil, nil).MaxTimes(3) 79 storage.EXPECT().GetMoreL5Sections(uint32(0)).Return(nil, nil).MaxTimes(3) 80 storage.EXPECT().GetMoreL5IPConfigs(uint32(0)).Return(nil, nil).MaxTimes(3) 81 storage.EXPECT().GetRateLimitsForCache(beg, true).Return(nil, nil).MaxTimes(1) 82 storage.EXPECT().GetRateLimitsForCache(beg, false).Return(nil, nil).MaxTimes(3) 83 storage.EXPECT().GetCircuitBreakerRulesForCache(beg, false).Return(nil, nil).MaxTimes(3) 84 storage.EXPECT().GetInstancesCountTx(gomock.Any()).Return(uint32(0), nil).MaxTimes(1) 85 86 ctx, cancel := context.WithCancel(context.Background()) 87 defer cancel() 88 89 err = c.Initialize() 90 assert.Nil(t, err) 91 92 err = c.Start(ctx) 93 assert.Nil(t, err) 94 95 // 等待cache更新 96 time.Sleep(c.GetUpdateCacheInterval() + time.Second) 97 }) 98 99 }