github.com/polarismesh/polaris@v1.17.8/plugin/ratelimit/token/resource_limiter_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 token 19 20 import ( 21 "fmt" 22 "testing" 23 "time" 24 25 . "github.com/smartystreets/goconvey/convey" 26 27 "github.com/polarismesh/polaris/plugin" 28 ) 29 30 // TestNewResourceRatelimit 测试新建 31 func TestNewResourceRatelimit(t *testing.T) { 32 Convey("测试新建一个资源限制器", t, func() { 33 Convey("config为空", func() { 34 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, nil) 35 So(limiter, ShouldNotBeNil) 36 So(err, ShouldBeNil) 37 }) 38 Convey("不开启限制器", func() { 39 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 40 Open: false, 41 }) 42 So(limiter, ShouldNotBeNil) 43 So(err, ShouldBeNil) 44 So(limiter.allow("11111"), ShouldBeTrue) 45 }) 46 Convey("开启了限制器,global为空", func() { 47 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 48 Open: true, 49 }) 50 So(limiter, ShouldBeNil) 51 So(err, ShouldNotBeNil) 52 t.Logf("%s", err.Error()) 53 }) 54 Convey("开启了限制器,global其他参数不合法", func() { 55 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 56 Open: true, 57 Global: &BucketRatelimit{}, 58 }) 59 So(limiter, ShouldBeNil) 60 So(err, ShouldNotBeNil) 61 62 limiter, err = newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 63 Open: true, 64 Global: &BucketRatelimit{true, 10, 10}, 65 }) 66 So(limiter, ShouldBeNil) 67 So(err, ShouldNotBeNil) 68 69 limiter, err = newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 70 Open: true, 71 Global: &BucketRatelimit{true, 10, 10}, 72 MaxResourceCacheAmount: -1, 73 }) 74 So(limiter, ShouldBeNil) 75 So(err, ShouldNotBeNil) 76 }) 77 Convey("正常新建限制器", func() { 78 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 79 Open: true, 80 Global: &BucketRatelimit{true, 10, 5}, 81 MaxResourceCacheAmount: 10, 82 }) 83 So(limiter, ShouldNotBeNil) 84 So(err, ShouldBeNil) 85 }) 86 Convey("白名单正常解析", func() { 87 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 88 Open: true, 89 Global: &BucketRatelimit{true, 10, 5}, 90 MaxResourceCacheAmount: 10, 91 WhiteList: []string{"1", "2", "3"}, 92 }) 93 So(limiter, ShouldNotBeNil) 94 So(err, ShouldBeNil) 95 So(len(limiter.whiteList), ShouldEqual, 3) 96 }) 97 }) 98 } 99 100 // TestResourceAllow 测试allow 101 func TestResourceAllow(t *testing.T) { 102 Convey("测试allow", t, func() { 103 Convey("正常限流", func() { 104 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 105 Open: true, 106 Global: &BucketRatelimit{true, 5, 5}, 107 MaxResourceCacheAmount: 2, 108 }) 109 So(err, ShouldBeNil) 110 cnt := 0 111 for i := 0; i <= limiter.config.Global.Rate*2; i++ { 112 if ok := limiter.allow("12345"); ok { 113 cnt++ 114 } 115 } 116 So(cnt, ShouldEqual, limiter.config.Global.Rate) 117 // 其他key,可以通过 118 So(limiter.allow("67890"), ShouldBeTrue) 119 120 // 1秒之后,12345又可以通过 121 time.Sleep(time.Second + time.Millisecond*10) 122 So(limiter.allow("12345"), ShouldBeTrue) 123 So(limiter.allow("67890"), ShouldBeTrue) 124 So(limiter.allow("13579"), ShouldBeTrue) 125 }) 126 Convey("max-resource测试", func() { 127 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 128 Open: true, 129 Global: &BucketRatelimit{true, 5, 5}, 130 MaxResourceCacheAmount: 2, 131 }) 132 So(err, ShouldBeNil) 133 cnt := 0 134 for i := 0; i < limiter.config.Global.Rate*20; i++ { 135 if ok := limiter.allow(fmt.Sprintf("key-%d", i)); ok { 136 cnt++ 137 } 138 } 139 // 不同key,全部通过 140 So(cnt, ShouldEqual, limiter.config.Global.Rate*20) 141 }) 142 Convey("白名单测试", func() { 143 limiter, err := newResourceRatelimit(plugin.InstanceRatelimit, &ResourceLimitConfig{ 144 Open: true, 145 Global: &BucketRatelimit{true, 5, 5}, 146 MaxResourceCacheAmount: 1024, 147 WhiteList: []string{"1000", "1001", "1002"}, 148 }) 149 So(err, ShouldBeNil) 150 151 cnt := 0 152 for i := 0; i < limiter.config.Global.Rate*3; i++ { 153 if ok := limiter.allow("1003"); ok { 154 cnt++ 155 } 156 } 157 So(cnt, ShouldEqual, limiter.config.Global.Rate) 158 159 cnt = 0 160 for i := 0; i < limiter.config.Global.Rate*30; i++ { 161 if ok := limiter.allow("1002"); ok { 162 cnt++ 163 } 164 } 165 So(cnt, ShouldEqual, limiter.config.Global.Rate*30) 166 }) 167 }) 168 }