|
| 1 | +package throttle |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsLimitCfgChangedSame(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + limiter *inMemoryLimiter |
| 13 | + curLimit int64 |
| 14 | + curDistribution []limitDistributionRatio |
| 15 | + |
| 16 | + want bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "same_limit_and_distribution", |
| 20 | + limiter: &inMemoryLimiter{ |
| 21 | + limit: complexLimit{ |
| 22 | + value: 100, |
| 23 | + distributions: limitDistributions{ |
| 24 | + idxByKey: map[string]int{ |
| 25 | + "A": 0, |
| 26 | + "B": 1, |
| 27 | + }, |
| 28 | + distributions: []complexDistribution{ |
| 29 | + {ratio: 0.1, limit: 10}, |
| 30 | + {ratio: 0.2, limit: 20}, |
| 31 | + }, |
| 32 | + enabled: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + curLimit: 100, |
| 37 | + curDistribution: []limitDistributionRatio{ |
| 38 | + {Ratio: 0.1, Values: []string{"A"}}, |
| 39 | + {Ratio: 0.2, Values: []string{"B"}}, |
| 40 | + }, |
| 41 | + |
| 42 | + want: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "limit_changed", |
| 46 | + limiter: &inMemoryLimiter{ |
| 47 | + limit: complexLimit{ |
| 48 | + value: 200, |
| 49 | + }, |
| 50 | + }, |
| 51 | + curLimit: 100, |
| 52 | + curDistribution: []limitDistributionRatio{}, |
| 53 | + |
| 54 | + want: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "distribution_changed", |
| 58 | + limiter: &inMemoryLimiter{ |
| 59 | + limit: complexLimit{ |
| 60 | + value: 100, |
| 61 | + distributions: limitDistributions{ |
| 62 | + idxByKey: map[string]int{ |
| 63 | + "A": 0, |
| 64 | + "B": 0, |
| 65 | + }, |
| 66 | + distributions: []complexDistribution{ |
| 67 | + {ratio: 0.1, limit: 10}, |
| 68 | + }, |
| 69 | + enabled: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + curLimit: 100, |
| 74 | + curDistribution: []limitDistributionRatio{ |
| 75 | + {Ratio: 0.1, Values: []string{"A"}}, |
| 76 | + {Ratio: 0.2, Values: []string{"B"}}, |
| 77 | + }, |
| 78 | + |
| 79 | + want: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "increase_distributions_size", |
| 83 | + limiter: &inMemoryLimiter{ |
| 84 | + limit: complexLimit{ |
| 85 | + value: 100, |
| 86 | + distributions: limitDistributions{ |
| 87 | + idxByKey: map[string]int{ |
| 88 | + "A": 0, |
| 89 | + "B": 1, |
| 90 | + "C": 0, |
| 91 | + }, |
| 92 | + distributions: []complexDistribution{ |
| 93 | + {ratio: 0.1, limit: 10}, |
| 94 | + {ratio: 0.2, limit: 20}, |
| 95 | + }, |
| 96 | + enabled: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + curLimit: 100, |
| 101 | + curDistribution: []limitDistributionRatio{ |
| 102 | + {Ratio: 0.1, Values: []string{"A"}}, |
| 103 | + {Ratio: 0.2, Values: []string{"B"}}, |
| 104 | + }, |
| 105 | + |
| 106 | + want: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "decrease_distributions_size", |
| 110 | + limiter: &inMemoryLimiter{ |
| 111 | + limit: complexLimit{ |
| 112 | + value: 100, |
| 113 | + distributions: limitDistributions{ |
| 114 | + idxByKey: map[string]int{ |
| 115 | + "A": 0, |
| 116 | + }, |
| 117 | + distributions: []complexDistribution{ |
| 118 | + {ratio: 0.1, limit: 10}, |
| 119 | + }, |
| 120 | + enabled: true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + curLimit: 100, |
| 125 | + curDistribution: []limitDistributionRatio{ |
| 126 | + {Ratio: 0.1, Values: []string{"A"}}, |
| 127 | + {Ratio: 0.2, Values: []string{"B"}}, |
| 128 | + }, |
| 129 | + |
| 130 | + want: true, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + for _, tt := range tests { |
| 135 | + tt := tt |
| 136 | + t.Run(tt.name, func(t *testing.T) { |
| 137 | + t.Parallel() |
| 138 | + |
| 139 | + got := tt.limiter.isLimitCfgChanged(tt.curLimit, tt.curDistribution) |
| 140 | + require.Equal(t, tt.want, got) |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments