Skip to content

Commit

Permalink
Add unit tests for custom item rate limiter (knative#12733)
Browse files Browse the repository at this point in the history
* Add unit tests for custom item rate limiter

* Remove test for already covered scenarios
  • Loading branch information
norman465 authored Mar 18, 2022
1 parent ed92cdd commit f1c08d8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 40 deletions.
42 changes: 5 additions & 37 deletions pkg/reconciler/revision/background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"testing"
"time"

"go.uber.org/atomic"
logtesting "knative.dev/pkg/logging/testing"
"knative.dev/pkg/ptr"

Expand Down Expand Up @@ -199,39 +198,6 @@ func TestResolveInBackground(t *testing.T) {
}
}

func TestRateLimitGlobal(t *testing.T) {
logger := logtesting.TestLogger(t)

var resolves atomic.Int32
var resolver resolveFunc = func(_ context.Context, _ string, _ k8schain.Options, _ sets.String) (string, error) {
resolves.Inc()
return "", errors.New("failed")
}

queue := workqueue.NewRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(1*time.Second, 5*time.Second))
subject := newBackgroundResolver(logger, resolver, queue, func(types.NamespacedName) {})

stop := make(chan struct{})
done := subject.Start(stop, 1)

defer func() {
close(stop)
<-done
}()

for i := 0; i < 5; i++ {
name := fmt.Sprint("rev", i)
subject.Resolve(logger, rev(name, name+"img1", name+"img2"), k8schain.Options{ServiceAccountName: "san"}, sets.NewString("skip"), 0)
}

// Rate limit base rate in this test is 1 second, so this should give time for at most 1 resolve.
time.Sleep(500 * time.Millisecond)

if r := resolves.Load(); r > 1 {
t.Fatalf("Expected resolves to be rate limited, but was called %d times", r)
}
}

func TestRateLimitPerItem(t *testing.T) {
logger := logtesting.TestLogger(t)

Expand All @@ -244,7 +210,7 @@ func TestRateLimitPerItem(t *testing.T) {
}

baseDelay := 50 * time.Millisecond
queue := workqueue.NewRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(baseDelay, 5*time.Second))
queue := workqueue.NewRateLimitingQueue(newItemExponentialFailureRateLimiter(baseDelay, 5*time.Second))

enqueue := make(chan struct{})
subject := newBackgroundResolver(logger, resolver, queue, func(types.NamespacedName) {
Expand Down Expand Up @@ -276,7 +242,8 @@ func TestRateLimitPerItem(t *testing.T) {
}

latency := time.Since(start)
expected := time.Duration(math.Pow(2, float64(i))) * baseDelay
// no delay on first resolve
expected := (time.Duration(math.Pow(2, float64(i-1))) * baseDelay)
if latency < expected {
t.Fatalf("latency = %s, want at least %s", latency, expected)
}
Expand Down Expand Up @@ -305,7 +272,8 @@ func TestRateLimitPerItem(t *testing.T) {
}

<-enqueue
if took := time.Since(start); took > baseDelay*2*2 {
// no delay on first resolve
if took := time.Since(start); took > (baseDelay * 2) {
t.Fatal("Expected Forget to remove revision from rate limiter, but took", took)
}
})
Expand Down
3 changes: 0 additions & 3 deletions pkg/reconciler/revision/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import (
//
// Copyright 2016 The Kubernetes Authors.
// From: https://github.com/kubernetes/client-go/blob/master/util/workqueue/default_rate_limiters.go
//
// TODO - drop this file when we pick up the following upstream change
// https://github.com/kubernetes/kubernetes/pull/108343
type itemExponentialFailureRateLimiter struct {
failuresLock sync.Mutex
failures map[interface{}]int
Expand Down
100 changes: 100 additions & 0 deletions pkg/reconciler/revision/rate_limiter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright 2022 The Knative 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 revision

import (
"testing"
"time"
)

// Copyright 2016 The Kubernetes Authors.
// From: https://github.com/kubernetes/client-go/blob/master/util/workqueue/default_rate_limiters_test.go

func TestItemExponentialFailureRateLimiter(t *testing.T) {
limiter := newItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second)

if e, a := 0*time.Millisecond, limiter.When("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)
}
if e, a := 2*time.Millisecond, limiter.When("one"); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := 4*time.Millisecond, limiter.When("one"); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := 8*time.Millisecond, 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 := 0*time.Millisecond, limiter.When("two"); 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 := 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 := 0*time.Millisecond, limiter.When("one"); e != a {
t.Errorf("expected %v, got %v", e, a)
}

}

func TestItemExponentialFailureRateLimiterOverFlow(t *testing.T) {
limiter := newItemExponentialFailureRateLimiter(1*time.Millisecond, 1000*time.Second)
for i := 0; i < 5; i++ {
limiter.When("one")
}
if e, a := 16*time.Millisecond, limiter.When("one"); e != a {
t.Errorf("expected %v, got %v", e, a)
}

for i := 0; i < 1000; i++ {
limiter.When("overflow1")
}
if e, a := 1000*time.Second, limiter.When("overflow1"); e != a {
t.Errorf("expected %v, got %v", e, a)
}

limiter = newItemExponentialFailureRateLimiter(1*time.Minute, 1000*time.Hour)
for i := 0; i < 2; i++ {
limiter.When("two")
}
if e, a := 2*time.Minute, limiter.When("two"); e != a {
t.Errorf("expected %v, got %v", e, a)
}

for i := 0; i < 1000; i++ {
limiter.When("overflow2")
}
if e, a := 1000*time.Hour, limiter.When("overflow2"); e != a {
t.Errorf("expected %v, got %v", e, a)
}

}

0 comments on commit f1c08d8

Please sign in to comment.