Skip to content

Commit 288d49f

Browse files
Rework the test to remove the need for new test accessor on RealFIFO
1 parent 43c4010 commit 288d49f

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

staging/src/k8s.io/client-go/tools/cache/reflector_test.go

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"maps"
2324
"math/rand"
2425
"net/http"
2526
"reflect"
2627
goruntime "runtime"
28+
"slices"
2729
"strconv"
2830
"sync"
2931
"sync/atomic"
@@ -2057,30 +2059,48 @@ func TestReflectorReplacesStoreOnUnsafeDelete(t *testing.T) {
20572059
}
20582060

20592061
func TestReflectorRespectStoreTransformer(t *testing.T) {
2060-
type testReflectorStore interface {
2061-
ReflectorStore
2062-
list() []interface{}
2063-
}
2064-
2065-
for name, storeBuilder := range map[string]func(counter *atomic.Int32) testReflectorStore{
2066-
"real-fifo": func(counter *atomic.Int32) testReflectorStore {
2067-
return NewRealFIFO(MetaNamespaceKeyFunc, NewStore(MetaNamespaceKeyFunc), func(i interface{}) (interface{}, error) {
2068-
counter.Add(1)
2069-
cast := i.(*v1.Pod)
2070-
cast.Spec.Hostname = "transformed"
2071-
return cast, nil
2072-
})
2073-
},
2074-
"delta-fifo": func(counter *atomic.Int32) testReflectorStore {
2075-
return NewDeltaFIFOWithOptions(DeltaFIFOOptions{
2076-
KeyFunction: MetaNamespaceKeyFunc,
2077-
Transformer: func(i interface{}) (interface{}, error) {
2062+
for name, test := range map[string]struct {
2063+
storeBuilder func(counter *atomic.Int32) ReflectorStore
2064+
items func(rs ReflectorStore) []interface{}
2065+
}{
2066+
"real-fifo": {
2067+
storeBuilder: func(counter *atomic.Int32) ReflectorStore {
2068+
return NewRealFIFO(MetaNamespaceKeyFunc, NewStore(MetaNamespaceKeyFunc), func(i interface{}) (interface{}, error) {
20782069
counter.Add(1)
20792070
cast := i.(*v1.Pod)
20802071
cast.Spec.Hostname = "transformed"
20812072
return cast, nil
2082-
},
2083-
})
2073+
})
2074+
},
2075+
items: func(rs ReflectorStore) []interface{} {
2076+
store := rs.(*RealFIFO)
2077+
objects := make(map[string]interface{})
2078+
for _, item := range store.getItems() {
2079+
key, _ := store.keyFunc(item.Object)
2080+
if item.Type == Deleted {
2081+
delete(objects, key)
2082+
} else {
2083+
objects[key] = item.Object
2084+
}
2085+
}
2086+
return slices.Collect(maps.Values(objects))
2087+
},
2088+
},
2089+
"delta-fifo": {
2090+
storeBuilder: func(counter *atomic.Int32) ReflectorStore {
2091+
return NewDeltaFIFOWithOptions(DeltaFIFOOptions{
2092+
KeyFunction: MetaNamespaceKeyFunc,
2093+
Transformer: func(i interface{}) (interface{}, error) {
2094+
counter.Add(1)
2095+
cast := i.(*v1.Pod)
2096+
cast.Spec.Hostname = "transformed"
2097+
return cast, nil
2098+
},
2099+
})
2100+
},
2101+
items: func(rs ReflectorStore) []interface{} {
2102+
return rs.(*DeltaFIFO).list()
2103+
},
20842104
},
20852105
} {
20862106
t.Run(name, func(t *testing.T) {
@@ -2113,7 +2133,7 @@ func TestReflectorRespectStoreTransformer(t *testing.T) {
21132133
}
21142134

21152135
var transformerInvoked atomic.Int32
2116-
s := storeBuilder(&transformerInvoked)
2136+
s := test.storeBuilder(&transformerInvoked)
21172137

21182138
var once sync.Once
21192139
lw := &ListWatch{
@@ -2158,10 +2178,11 @@ func TestReflectorRespectStoreTransformer(t *testing.T) {
21582178
t.Errorf("expected LastSyncResourceVersion to be %q, but got: %q", want, got)
21592179
}
21602180

2161-
if want, got := 3, len(s.list()); want != got {
2181+
informerItems := test.items(s)
2182+
if want, got := 3, len(informerItems); want != got {
21622183
t.Errorf("expected informer to contain %d objects, but got: %d", want, got)
21632184
}
2164-
for _, item := range s.list() {
2185+
for _, item := range informerItems {
21652186
cast := item.(*v1.Pod)
21662187
if cast.Spec.Hostname != "transformed" {
21672188
t.Error("Object was not transformed prior to replacement")

staging/src/k8s.io/client-go/tools/cache/the_real_fifo_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ package cache
1818

1919
import (
2020
"fmt"
21-
"maps"
2221
"reflect"
2322
"runtime"
24-
"slices"
2523
"testing"
2624
"time"
2725
)
@@ -35,21 +33,6 @@ func (f *RealFIFO) getItems() []Delta {
3533
return ret
3634
}
3735

38-
func (f *RealFIFO) list() []interface{} {
39-
f.lock.Lock()
40-
defer f.lock.Unlock()
41-
42-
objects := make(map[string]interface{})
43-
for _, item := range f.items {
44-
if item.Type == Deleted {
45-
continue
46-
}
47-
key, _ := f.keyFunc(item.Object)
48-
objects[key] = item.Object
49-
}
50-
return slices.Collect(maps.Values(objects))
51-
}
52-
5336
const closedFIFOName = "FIFO WAS CLOSED"
5437

5538
func popN(queue Queue, count int) []interface{} {

0 commit comments

Comments
 (0)