k8s.io/client-go@v0.31.1/util/workqueue/default_rate_limiters_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes 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 workqueue 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 func TestItemExponentialFailureRateLimiter(t *testing.T) { 25 limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second) 26 27 if e, a := 1*time.Millisecond, limiter.When("one"); e != a { 28 t.Errorf("expected %v, got %v", e, a) 29 } 30 if e, a := 2*time.Millisecond, limiter.When("one"); e != a { 31 t.Errorf("expected %v, got %v", e, a) 32 } 33 if e, a := 4*time.Millisecond, limiter.When("one"); e != a { 34 t.Errorf("expected %v, got %v", e, a) 35 } 36 if e, a := 8*time.Millisecond, limiter.When("one"); e != a { 37 t.Errorf("expected %v, got %v", e, a) 38 } 39 if e, a := 16*time.Millisecond, limiter.When("one"); e != a { 40 t.Errorf("expected %v, got %v", e, a) 41 } 42 if e, a := 5, limiter.NumRequeues("one"); e != a { 43 t.Errorf("expected %v, got %v", e, a) 44 } 45 46 if e, a := 1*time.Millisecond, limiter.When("two"); e != a { 47 t.Errorf("expected %v, got %v", e, a) 48 } 49 if e, a := 2*time.Millisecond, limiter.When("two"); e != a { 50 t.Errorf("expected %v, got %v", e, a) 51 } 52 if e, a := 2, limiter.NumRequeues("two"); e != a { 53 t.Errorf("expected %v, got %v", e, a) 54 } 55 56 limiter.Forget("one") 57 if e, a := 0, limiter.NumRequeues("one"); e != a { 58 t.Errorf("expected %v, got %v", e, a) 59 } 60 if e, a := 1*time.Millisecond, limiter.When("one"); e != a { 61 t.Errorf("expected %v, got %v", e, a) 62 } 63 64 } 65 66 func TestItemExponentialFailureRateLimiterOverFlow(t *testing.T) { 67 limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1000*time.Second) 68 for i := 0; i < 5; i++ { 69 limiter.When("one") 70 } 71 if e, a := 32*time.Millisecond, limiter.When("one"); e != a { 72 t.Errorf("expected %v, got %v", e, a) 73 } 74 75 for i := 0; i < 1000; i++ { 76 limiter.When("overflow1") 77 } 78 if e, a := 1000*time.Second, limiter.When("overflow1"); e != a { 79 t.Errorf("expected %v, got %v", e, a) 80 } 81 82 limiter = NewItemExponentialFailureRateLimiter(1*time.Minute, 1000*time.Hour) 83 for i := 0; i < 2; i++ { 84 limiter.When("two") 85 } 86 if e, a := 4*time.Minute, limiter.When("two"); e != a { 87 t.Errorf("expected %v, got %v", e, a) 88 } 89 90 for i := 0; i < 1000; i++ { 91 limiter.When("overflow2") 92 } 93 if e, a := 1000*time.Hour, limiter.When("overflow2"); e != a { 94 t.Errorf("expected %v, got %v", e, a) 95 } 96 97 } 98 99 func TestItemFastSlowRateLimiter(t *testing.T) { 100 limiter := NewItemFastSlowRateLimiter(5*time.Millisecond, 10*time.Second, 3) 101 102 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 103 t.Errorf("expected %v, got %v", e, a) 104 } 105 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 106 t.Errorf("expected %v, got %v", e, a) 107 } 108 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 109 t.Errorf("expected %v, got %v", e, a) 110 } 111 if e, a := 10*time.Second, limiter.When("one"); e != a { 112 t.Errorf("expected %v, got %v", e, a) 113 } 114 if e, a := 10*time.Second, limiter.When("one"); e != a { 115 t.Errorf("expected %v, got %v", e, a) 116 } 117 if e, a := 5, limiter.NumRequeues("one"); e != a { 118 t.Errorf("expected %v, got %v", e, a) 119 } 120 121 if e, a := 5*time.Millisecond, limiter.When("two"); e != a { 122 t.Errorf("expected %v, got %v", e, a) 123 } 124 if e, a := 5*time.Millisecond, limiter.When("two"); e != a { 125 t.Errorf("expected %v, got %v", e, a) 126 } 127 if e, a := 2, limiter.NumRequeues("two"); e != a { 128 t.Errorf("expected %v, got %v", e, a) 129 } 130 131 limiter.Forget("one") 132 if e, a := 0, limiter.NumRequeues("one"); e != a { 133 t.Errorf("expected %v, got %v", e, a) 134 } 135 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 136 t.Errorf("expected %v, got %v", e, a) 137 } 138 139 } 140 141 func TestMaxOfRateLimiter(t *testing.T) { 142 limiter := NewMaxOfRateLimiter( 143 NewItemFastSlowRateLimiter(5*time.Millisecond, 3*time.Second, 3), 144 NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second), 145 ) 146 147 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 148 t.Errorf("expected %v, got %v", e, a) 149 } 150 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 151 t.Errorf("expected %v, got %v", e, a) 152 } 153 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 154 t.Errorf("expected %v, got %v", e, a) 155 } 156 if e, a := 3*time.Second, limiter.When("one"); e != a { 157 t.Errorf("expected %v, got %v", e, a) 158 } 159 if e, a := 3*time.Second, limiter.When("one"); e != a { 160 t.Errorf("expected %v, got %v", e, a) 161 } 162 if e, a := 5, limiter.NumRequeues("one"); e != a { 163 t.Errorf("expected %v, got %v", e, a) 164 } 165 166 if e, a := 5*time.Millisecond, limiter.When("two"); e != a { 167 t.Errorf("expected %v, got %v", e, a) 168 } 169 if e, a := 5*time.Millisecond, limiter.When("two"); e != a { 170 t.Errorf("expected %v, got %v", e, a) 171 } 172 if e, a := 2, limiter.NumRequeues("two"); e != a { 173 t.Errorf("expected %v, got %v", e, a) 174 } 175 176 limiter.Forget("one") 177 if e, a := 0, limiter.NumRequeues("one"); e != a { 178 t.Errorf("expected %v, got %v", e, a) 179 } 180 if e, a := 5*time.Millisecond, limiter.When("one"); e != a { 181 t.Errorf("expected %v, got %v", e, a) 182 } 183 184 } 185 186 func TestWithMaxWaitRateLimiter(t *testing.T) { 187 limiter := NewWithMaxWaitRateLimiter(NewStepRateLimiter(5*time.Millisecond, 1000*time.Second, 100), 500*time.Second) 188 for i := 0; i < 100; i++ { 189 if e, a := 5*time.Millisecond, limiter.When(i); e != a { 190 t.Errorf("expected %v, got %v ", e, a) 191 } 192 } 193 194 for i := 100; i < 200; i++ { 195 if e, a := 500*time.Second, limiter.When(i); e != a { 196 t.Errorf("expected %v, got %v", e, a) 197 } 198 } 199 } 200 201 var _ RateLimiter = &StepRateLimiter{} 202 203 func NewStepRateLimiter(baseDelay time.Duration, maxDelay time.Duration, threshold int) RateLimiter { 204 return &StepRateLimiter{ 205 baseDelay: baseDelay, 206 maxDelay: maxDelay, 207 threshold: threshold, 208 } 209 } 210 211 type StepRateLimiter struct { 212 count int 213 threshold int 214 baseDelay time.Duration 215 maxDelay time.Duration 216 } 217 218 func (r *StepRateLimiter) When(item interface{}) time.Duration { 219 r.count += 1 220 if r.count <= r.threshold { 221 return r.baseDelay 222 } 223 return r.maxDelay 224 } 225 226 func (r *StepRateLimiter) NumRequeues(item interface{}) int { 227 return 0 228 } 229 230 func (r *StepRateLimiter) Forget(item interface{}) { 231 }