Move test double queue to test file and unexport

This commit is contained in:
Timo Reimann 2022-12-14 22:08:18 +01:00
parent 208af2d1d8
commit 0fcf42f321
2 changed files with 32 additions and 56 deletions

View File

@ -47,7 +47,6 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/cloud-provider/api"
queuetesting "k8s.io/cloud-provider/controllers/service/testing"
fakecloud "k8s.io/cloud-provider/fake"
servicehelper "k8s.io/cloud-provider/service/helpers"
featuregatetesting "k8s.io/component-base/featuregate/testing"
@ -2285,7 +2284,7 @@ func TestServiceQueueDelay(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
controller, cloud, client := newController()
queue := &queuetesting.SpyWorkQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-service-queue-delay")}
queue := &spyWorkQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-service-queue-delay")}
controller.serviceQueue = queue
cloud.Err = tc.lbCloudErr
@ -2323,7 +2322,7 @@ func TestServiceQueueDelay(t *testing.T) {
}
if tc.wantRetryDelay > 0 {
items := queue.GetItems()
items := queue.getItems()
if len(items) != 1 {
t.Fatalf("got %d item(s), want 1", len(items))
}
@ -2396,3 +2395,33 @@ func (l *fakeServiceLister) Get(name string) (*v1.Service, error) {
func (l *fakeServiceLister) Services(_ string) corelisters.ServiceNamespaceLister {
return l
}
// spyWorkQueue implements a work queue and adds the ability to inspect processed
// items for testing purposes.
type spyWorkQueue struct {
workqueue.RateLimitingInterface
items []spyQueueItem
}
// spyQueueItem represents an item that was being processed.
type spyQueueItem struct {
Key interface{}
// Delay represents the delayed duration if and only if AddAfter was invoked.
Delay time.Duration
}
// AddAfter is like workqueue.RateLimitingInterface.AddAfter but records the
// added key and delay internally.
func (f *spyWorkQueue) AddAfter(key interface{}, delay time.Duration) {
f.items = append(f.items, spyQueueItem{
Key: key,
Delay: delay,
})
f.RateLimitingInterface.AddAfter(key, delay)
}
// getItems returns all items that were recorded.
func (f *spyWorkQueue) getItems() []spyQueueItem {
return f.items
}

View File

@ -1,53 +0,0 @@
/*
Copyright 2022 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 testing
import (
"time"
"k8s.io/client-go/util/workqueue"
)
// SpyWorkQueue implements a work queue and adds the ability to inspect processed
// items for testing purposes.
type SpyWorkQueue struct {
workqueue.RateLimitingInterface
items []SpyQueueItem
}
// SpyQueueItem represents an item that was being processed.
type SpyQueueItem struct {
Key interface{}
// Delay represents the delayed duration if and only if AddAfter was invoked.
Delay time.Duration
}
// AddAfter is like workqueue.RateLimitingInterface.AddAfter but records the
// added key and delay internally.
func (f *SpyWorkQueue) AddAfter(key interface{}, delay time.Duration) {
f.items = append(f.items, SpyQueueItem{
Key: key,
Delay: delay,
})
f.RateLimitingInterface.AddAfter(key, delay)
}
// GetItems returns all items that were recorded.
func (f *SpyWorkQueue) GetItems() []SpyQueueItem {
return f.items
}