|
| 1 | +/* |
| 2 | +Copyright 2025 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "k8s.io/client-go/util/flowcontrol" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGetRateLimiterGetter(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + want *RateLimiterGetter |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "get the default singleton", |
| 33 | + want: defaultRateLimiterGetter, |
| 34 | + }, |
| 35 | + } |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + assert.Equalf(t, tt.want, GetRateLimiterGetter(), "GetRateLimiterGetter()") |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestRateLimiterGetter_GetRateLimiter(t *testing.T) { |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + getter *RateLimiterGetter |
| 47 | + want flowcontrol.RateLimiter |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "if qps/burst not set, use default value", |
| 51 | + getter: &RateLimiterGetter{}, |
| 52 | + want: flowcontrol.NewTokenBucketRateLimiter(5, 10), |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "SetLimits() should able to work", |
| 56 | + getter: func() *RateLimiterGetter { |
| 57 | + getter := &RateLimiterGetter{} |
| 58 | + getter.SetLimits(100, 200) |
| 59 | + return getter |
| 60 | + }(), |
| 61 | + want: flowcontrol.NewTokenBucketRateLimiter(100, 200), |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "if qps/burst invalid, use default value", |
| 65 | + getter: func() *RateLimiterGetter { |
| 66 | + getter := &RateLimiterGetter{} |
| 67 | + getter.SetLimits(-1, -1) |
| 68 | + return getter |
| 69 | + }(), |
| 70 | + want: flowcontrol.NewTokenBucketRateLimiter(5, 10), |
| 71 | + }, |
| 72 | + } |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + assert.Equalf(t, tt.want, tt.getter.GetRateLimiter("a"), "GetRateLimiter()") |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestRateLimiterGetter_GetSameRateLimiter(t *testing.T) { |
| 81 | + type args struct { |
| 82 | + key1 string |
| 83 | + key2 string |
| 84 | + } |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + args args |
| 88 | + wantSameLimiter bool |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "for a single cluster, the same limiter instance is obtained each time", |
| 92 | + args: args{ |
| 93 | + key1: "a", |
| 94 | + key2: "a", |
| 95 | + }, |
| 96 | + wantSameLimiter: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "the rate limiter is independent for each cluster", |
| 100 | + args: args{ |
| 101 | + key1: "a", |
| 102 | + key2: "b", |
| 103 | + }, |
| 104 | + wantSameLimiter: false, |
| 105 | + }, |
| 106 | + } |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + getter := &RateLimiterGetter{} |
| 110 | + rl1 := getter.GetRateLimiter(tt.args.key1) |
| 111 | + rl2 := getter.GetRateLimiter(tt.args.key2) |
| 112 | + gotSameLimiter := rl1 == rl2 |
| 113 | + assert.Equalf(t, tt.wantSameLimiter, gotSameLimiter, |
| 114 | + "key1: %v, key2: %v, wantSameLimiter: %v, rl1: %p, rl2: %p", |
| 115 | + tt.args.key1, tt.args.key2, tt.wantSameLimiter, rl1, rl2) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments