mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 22:01:06 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			152 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			152 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| /*
 | |
| Copyright 2016 The Kubernetes Authors.
 | |
| 
 | |
| Licensed under the Apache License, Version 2.0 (the "License");
 | |
| you may not use this file except in compliance with the License.
 | |
| You may obtain a copy of the License at
 | |
| 
 | |
|     http://www.apache.org/licenses/LICENSE-2.0
 | |
| 
 | |
| Unless required by applicable law or agreed to in writing, software
 | |
| distributed under the License is distributed on an "AS IS" BASIS,
 | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | |
| See the License for the specific language governing permissions and
 | |
| limitations under the License.
 | |
| */
 | |
| 
 | |
| package workqueue
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| 	"time"
 | |
| )
 | |
| 
 | |
| func TestItemExponentialFailureRateLimiter(t *testing.T) {
 | |
| 	limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second)
 | |
| 
 | |
| 	if e, a := 1*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 100*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 1*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 1*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	if e, a := 1*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 2, limiter.NumRequeues("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	limiter.Forget("one")
 | |
| 	if e, a := 0, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 1*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| }
 | |
| 
 | |
| func TestItemFastSlowRateLimiter(t *testing.T) {
 | |
| 	limiter := NewItemFastSlowRateLimiter(5*time.Millisecond, 10*time.Second, 3)
 | |
| 
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 2, limiter.NumRequeues("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	limiter.Forget("one")
 | |
| 	if e, a := 0, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| }
 | |
| 
 | |
| func TestMaxOfRateLimiter(t *testing.T) {
 | |
| 	limiter := NewMaxOfRateLimiter(
 | |
| 		NewItemFastSlowRateLimiter(5*time.Millisecond, 3*time.Second, 3),
 | |
| 		NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second),
 | |
| 	)
 | |
| 
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 100*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 3*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 3*time.Second, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 10*time.Millisecond, limiter.When("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 2, limiter.NumRequeues("two"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| 	limiter.Forget("one")
 | |
| 	if e, a := 0, limiter.NumRequeues("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
 | |
| 		t.Errorf("expected %v, got %v", e, a)
 | |
| 	}
 | |
| 
 | |
| }
 |