Merge pull request #104874 from wojtek-t/migrate_clock_1

Unify towards k8s.io/utils/clock - part 1

Kubernetes-commit: 047a6b9f861b2cc9dd2eea77da752ac398e7546f
This commit is contained in:
Kubernetes Publisher 2021-09-13 19:09:20 -07:00
commit 2ed3e42c43
9 changed files with 29 additions and 27 deletions

View File

@ -54,6 +54,7 @@ import (
restclientwatch "k8s.io/client-go/rest/watch" restclientwatch "k8s.io/client-go/rest/watch"
"k8s.io/client-go/util/flowcontrol" "k8s.io/client-go/util/flowcontrol"
utiltesting "k8s.io/client-go/util/testing" utiltesting "k8s.io/client-go/util/testing"
testingclock "k8s.io/utils/clock/testing"
) )
func TestNewRequestSetsAccept(t *testing.T) { func TestNewRequestSetsAccept(t *testing.T) {
@ -1563,7 +1564,7 @@ func TestBackoffLifecycle(t *testing.T) {
// which are used in the server implementation returning StatusOK above. // which are used in the server implementation returning StatusOK above.
seconds := []int{0, 1, 2, 4, 8, 0, 1, 2, 4, 0} seconds := []int{0, 1, 2, 4, 8, 0, 1, 2, 4, 0}
request := c.Verb("POST").Prefix("backofftest").Suffix("abc") request := c.Verb("POST").Prefix("backofftest").Suffix("abc")
clock := clock.FakeClock{} clock := testingclock.FakeClock{}
request.backoff = &URLBackoff{ request.backoff = &URLBackoff{
// Use a fake backoff here to avoid flakes and speed the test up. // Use a fake backoff here to avoid flakes and speed the test up.
Backoff: flowcontrol.NewFakeBackOff( Backoff: flowcontrol.NewFakeBackOff(

View File

@ -20,7 +20,8 @@ import (
"sync" "sync"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" "k8s.io/utils/clock"
testingclock "k8s.io/utils/clock/testing"
"k8s.io/utils/integer" "k8s.io/utils/integer"
) )
@ -37,7 +38,7 @@ type Backoff struct {
perItemBackoff map[string]*backoffEntry perItemBackoff map[string]*backoffEntry
} }
func NewFakeBackOff(initial, max time.Duration, tc *clock.FakeClock) *Backoff { func NewFakeBackOff(initial, max time.Duration, tc *testingclock.FakeClock) *Backoff {
return &Backoff{ return &Backoff{
perItemBackoff: map[string]*backoffEntry{}, perItemBackoff: map[string]*backoffEntry{},
Clock: tc, Clock: tc,

View File

@ -20,12 +20,12 @@ import (
"testing" "testing"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" testingclock "k8s.io/utils/clock/testing"
) )
func TestSlowBackoff(t *testing.T) { func TestSlowBackoff(t *testing.T) {
id := "_idSlow" id := "_idSlow"
tc := clock.NewFakeClock(time.Now()) tc := testingclock.NewFakeClock(time.Now())
step := time.Second step := time.Second
maxDuration := 50 * step maxDuration := 50 * step
@ -51,7 +51,7 @@ func TestSlowBackoff(t *testing.T) {
func TestBackoffReset(t *testing.T) { func TestBackoffReset(t *testing.T) {
id := "_idReset" id := "_idReset"
tc := clock.NewFakeClock(time.Now()) tc := testingclock.NewFakeClock(time.Now())
step := time.Second step := time.Second
maxDuration := step * 5 maxDuration := step * 5
b := NewFakeBackOff(step, maxDuration, tc) b := NewFakeBackOff(step, maxDuration, tc)
@ -77,7 +77,7 @@ func TestBackoffReset(t *testing.T) {
func TestBackoffHighWaterMark(t *testing.T) { func TestBackoffHighWaterMark(t *testing.T) {
id := "_idHiWaterMark" id := "_idHiWaterMark"
tc := clock.NewFakeClock(time.Now()) tc := testingclock.NewFakeClock(time.Now())
step := time.Second step := time.Second
maxDuration := 5 * step maxDuration := 5 * step
b := NewFakeBackOff(step, maxDuration, tc) b := NewFakeBackOff(step, maxDuration, tc)
@ -99,7 +99,7 @@ func TestBackoffHighWaterMark(t *testing.T) {
func TestBackoffGC(t *testing.T) { func TestBackoffGC(t *testing.T) {
id := "_idGC" id := "_idGC"
tc := clock.NewFakeClock(time.Now()) tc := testingclock.NewFakeClock(time.Now())
step := time.Second step := time.Second
maxDuration := 5 * step maxDuration := 5 * step
@ -127,7 +127,7 @@ func TestBackoffGC(t *testing.T) {
func TestIsInBackOffSinceUpdate(t *testing.T) { func TestIsInBackOffSinceUpdate(t *testing.T) {
id := "_idIsInBackOffSinceUpdate" id := "_idIsInBackOffSinceUpdate"
tc := clock.NewFakeClock(time.Now()) tc := testingclock.NewFakeClock(time.Now())
step := time.Second step := time.Second
maxDuration := 10 * step maxDuration := 10 * step
b := NewFakeBackOff(step, maxDuration, tc) b := NewFakeBackOff(step, maxDuration, tc)

View File

@ -21,8 +21,8 @@ import (
"sync" "sync"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock"
utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/utils/clock"
) )
// DelayingInterface is an Interface that can Add an item at a later time. This makes it easier to // DelayingInterface is an Interface that can Add an item at a later time. This makes it easier to
@ -51,11 +51,11 @@ func NewNamedDelayingQueue(name string) DelayingInterface {
// NewDelayingQueueWithCustomClock constructs a new named workqueue // NewDelayingQueueWithCustomClock constructs a new named workqueue
// with ability to inject real or fake clock for testing purposes // with ability to inject real or fake clock for testing purposes
func NewDelayingQueueWithCustomClock(clock clock.Clock, name string) DelayingInterface { func NewDelayingQueueWithCustomClock(clock clock.WithTicker, name string) DelayingInterface {
return newDelayingQueue(clock, NewNamed(name), name) return newDelayingQueue(clock, NewNamed(name), name)
} }
func newDelayingQueue(clock clock.Clock, q Interface, name string) *delayingType { func newDelayingQueue(clock clock.WithTicker, q Interface, name string) *delayingType {
ret := &delayingType{ ret := &delayingType{
Interface: q, Interface: q,
clock: clock, clock: clock,

View File

@ -23,12 +23,12 @@ import (
"testing" "testing"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
testingclock "k8s.io/utils/clock/testing"
) )
func TestSimpleQueue(t *testing.T) { func TestSimpleQueue(t *testing.T) {
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewDelayingQueueWithCustomClock(fakeClock, "") q := NewDelayingQueueWithCustomClock(fakeClock, "")
first := "foo" first := "foo"
@ -70,7 +70,7 @@ func TestSimpleQueue(t *testing.T) {
} }
func TestDeduping(t *testing.T) { func TestDeduping(t *testing.T) {
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewDelayingQueueWithCustomClock(fakeClock, "") q := NewDelayingQueueWithCustomClock(fakeClock, "")
first := "foo" first := "foo"
@ -129,7 +129,7 @@ func TestDeduping(t *testing.T) {
} }
func TestAddTwoFireEarly(t *testing.T) { func TestAddTwoFireEarly(t *testing.T) {
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewDelayingQueueWithCustomClock(fakeClock, "") q := NewDelayingQueueWithCustomClock(fakeClock, "")
first := "foo" first := "foo"
@ -178,7 +178,7 @@ func TestAddTwoFireEarly(t *testing.T) {
} }
func TestCopyShifting(t *testing.T) { func TestCopyShifting(t *testing.T) {
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewDelayingQueueWithCustomClock(fakeClock, "") q := NewDelayingQueueWithCustomClock(fakeClock, "")
first := "foo" first := "foo"
@ -216,7 +216,7 @@ func TestCopyShifting(t *testing.T) {
} }
func BenchmarkDelayingQueue_AddAfter(b *testing.B) { func BenchmarkDelayingQueue_AddAfter(b *testing.B) {
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewDelayingQueueWithCustomClock(fakeClock, "") q := NewDelayingQueueWithCustomClock(fakeClock, "")
// Add items // Add items

View File

@ -20,7 +20,7 @@ import (
"sync" "sync"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" "k8s.io/utils/clock"
) )
// This file provides abstractions for setting the provider (e.g., prometheus) // This file provides abstractions for setting the provider (e.g., prometheus)

View File

@ -21,7 +21,7 @@ import (
"testing" "testing"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" testingclock "k8s.io/utils/clock/testing"
) )
type testMetrics struct { type testMetrics struct {
@ -40,7 +40,7 @@ func TestMetricShutdown(t *testing.T) {
m := &testMetrics{ m := &testMetrics{
updateCalled: ch, updateCalled: ch,
} }
c := clock.NewFakeClock(time.Now()) c := testingclock.NewFakeClock(time.Now())
q := newQueue(c, m, time.Millisecond) q := newQueue(c, m, time.Millisecond)
for !c.HasWaiters() { for !c.HasWaiters() {
// Wait for the go routine to call NewTicker() // Wait for the go routine to call NewTicker()
@ -170,7 +170,7 @@ func (m *testMetricsProvider) NewRetriesMetric(name string) CounterMetric {
func TestMetrics(t *testing.T) { func TestMetrics(t *testing.T) {
mp := testMetricsProvider{} mp := testMetricsProvider{}
t0 := time.Unix(0, 0) t0 := time.Unix(0, 0)
c := clock.NewFakeClock(t0) c := testingclock.NewFakeClock(t0)
mf := queueMetricsFactory{metricsProvider: &mp} mf := queueMetricsFactory{metricsProvider: &mp}
m := mf.newQueueMetrics("test", c) m := mf.newQueueMetrics("test", c)
q := newQueue(c, m, time.Millisecond) q := newQueue(c, m, time.Millisecond)

View File

@ -20,7 +20,7 @@ import (
"sync" "sync"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" "k8s.io/utils/clock"
) )
type Interface interface { type Interface interface {
@ -47,7 +47,7 @@ func NewNamed(name string) *Type {
) )
} }
func newQueue(c clock.Clock, metrics queueMetrics, updatePeriod time.Duration) *Type { func newQueue(c clock.WithTicker, metrics queueMetrics, updatePeriod time.Duration) *Type {
t := &Type{ t := &Type{
clock: c, clock: c,
dirty: set{}, dirty: set{},
@ -92,7 +92,7 @@ type Type struct {
metrics queueMetrics metrics queueMetrics
unfinishedWorkUpdatePeriod time.Duration unfinishedWorkUpdatePeriod time.Duration
clock clock.Clock clock clock.WithTicker
} }
type empty struct{} type empty struct{}

View File

@ -20,13 +20,13 @@ import (
"testing" "testing"
"time" "time"
"k8s.io/apimachinery/pkg/util/clock" testingclock "k8s.io/utils/clock/testing"
) )
func TestRateLimitingQueue(t *testing.T) { func TestRateLimitingQueue(t *testing.T) {
limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second) limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second)
queue := NewRateLimitingQueue(limiter).(*rateLimitingType) queue := NewRateLimitingQueue(limiter).(*rateLimitingType)
fakeClock := clock.NewFakeClock(time.Now()) fakeClock := testingclock.NewFakeClock(time.Now())
delayingQueue := &delayingType{ delayingQueue := &delayingType{
Interface: New(), Interface: New(),
clock: fakeClock, clock: fakeClock,