From 00429ab356ab4f3f691db841b3b919d00c2f130b Mon Sep 17 00:00:00 2001 From: Keisuke Ishigami Date: Sat, 14 Jun 2025 22:04:22 +0900 Subject: [PATCH] modify tests Kubernetes-commit: 2cd5dbbdaab98020b37df7125f622db7a6dd885a --- tools/cache/delta_fifo_test.go | 43 ++++++++++++++++--------------- tools/cache/fifo_test.go | 23 +++++++++-------- tools/cache/reflector_test.go | 2 +- tools/cache/the_real_fifo_test.go | 33 ++++++++++++------------ 4 files changed, 52 insertions(+), 49 deletions(-) diff --git a/tools/cache/delta_fifo_test.go b/tools/cache/delta_fifo_test.go index 9c67012c..b3e80897 100644 --- a/tools/cache/delta_fifo_test.go +++ b/tools/cache/delta_fifo_test.go @@ -17,6 +17,7 @@ limitations under the License. package cache import ( + "context" "fmt" "reflect" "runtime" @@ -86,12 +87,12 @@ func (f *DeltaFIFO) GetByKey(key string) (item interface{}, exists bool, err err // helper function to reduce stuttering func testPop(f *DeltaFIFO) testFifoObject { - return Pop(f).(Deltas).Newest().Object.(testFifoObject) + return Pop(context.Background(), f).(Deltas).Newest().Object.(testFifoObject) } // testPopIfAvailable returns `{}, false` if Pop returns a nil object func testPopIfAvailable(f *DeltaFIFO) (testFifoObject, bool) { - obj := Pop(f) + obj := Pop(context.Background(), f) if obj == nil { return testFifoObject{}, false } @@ -179,7 +180,7 @@ func TestDeltaFIFO_replaceWithDeleteDeltaIn(t *testing.T) { f.Delete(oldObj) f.Replace([]interface{}{newObj}, "") - actualDeltas := Pop(f) + actualDeltas := Pop(context.Background(), f) expectedDeltas := Deltas{ Delta{Type: Deleted, Object: oldObj}, Delta{Type: Sync, Object: newObj}, @@ -289,7 +290,7 @@ func TestDeltaFIFOW_ReplaceMakesDeletionsForObjectsOnlyInQueue(t *testing.T) { }), }) tt.operations(fWithKnownObjects) - actualDeltasWithKnownObjects := Pop(fWithKnownObjects) + actualDeltasWithKnownObjects := Pop(context.Background(), fWithKnownObjects) if !reflect.DeepEqual(tt.expectedDeltas, actualDeltasWithKnownObjects) { t.Errorf("expected %#v, got %#v", tt.expectedDeltas, actualDeltasWithKnownObjects) } @@ -302,7 +303,7 @@ func TestDeltaFIFOW_ReplaceMakesDeletionsForObjectsOnlyInQueue(t *testing.T) { KeyFunction: testFifoObjectKeyFunc, }) tt.operations(fWithoutKnownObjects) - actualDeltasWithoutKnownObjects := Pop(fWithoutKnownObjects) + actualDeltasWithoutKnownObjects := Pop(context.Background(), fWithoutKnownObjects) if !reflect.DeepEqual(tt.expectedDeltas, actualDeltasWithoutKnownObjects) { t.Errorf("expected %#v, got %#v", tt.expectedDeltas, actualDeltasWithoutKnownObjects) } @@ -402,7 +403,7 @@ func TestDeltaFIFO_transformer(t *testing.T) { } for i := 0; i < 2; i++ { - obj, err := f.Pop(func(o interface{}, isInInitialList bool) error { return nil }) + obj, err := f.Pop(context.Background(), func(o interface{}, isInInitialList bool) error { return nil }) if err != nil { t.Fatalf("got nothing on try %v?", i) } @@ -592,7 +593,7 @@ func TestDeltaFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -618,7 +619,7 @@ func TestDeltaFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -648,7 +649,7 @@ func TestDeltaFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -679,7 +680,7 @@ func TestDeltaFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -697,7 +698,7 @@ func TestDeltaFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -727,7 +728,7 @@ func TestDeltaFIFO_ReplaceMakesDeletionsReplaced(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -751,7 +752,7 @@ func TestDeltaFIFO_ReplaceDeltaType(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -773,18 +774,18 @@ func TestDeltaFIFO_UpdateResyncRace(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } } } -// pop2 captures both parameters, unlike Pop(). +// pop2 captures both parameters, unlike Pop(context.Background(), ). func pop2[T any](queue Queue) (T, bool) { var result interface{} var isList bool - queue.Pop(func(obj interface{}, isInInitialList bool) error { + queue.Pop(context.Background(), func(obj interface{}, isInInitialList bool) error { result = obj isList = isInInitialList return nil @@ -909,15 +910,15 @@ func TestDeltaFIFO_HasSynced(t *testing.T) { { actions: []func(f *DeltaFIFO){ func(f *DeltaFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *DeltaFIFO) { Pop(f) }, + func(f *DeltaFIFO) { Pop(context.Background(), f) }, }, expectedSynced: false, }, { actions: []func(f *DeltaFIFO){ func(f *DeltaFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *DeltaFIFO) { Pop(f) }, - func(f *DeltaFIFO) { Pop(f) }, + func(f *DeltaFIFO) { Pop(context.Background(), f) }, + func(f *DeltaFIFO) { Pop(context.Background(), f) }, }, expectedSynced: true, }, @@ -926,7 +927,7 @@ func TestDeltaFIFO_HasSynced(t *testing.T) { // there cannot be duplicate keys in the list or apiserver is broken. actions: []func(f *DeltaFIFO){ func(f *DeltaFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("a", 2)}, "0") }, - func(f *DeltaFIFO) { Pop(f) }, + func(f *DeltaFIFO) { Pop(context.Background(), f) }, }, expectedSynced: true, }, @@ -958,7 +959,7 @@ func TestDeltaFIFO_PopShouldUnblockWhenClosed(t *testing.T) { const jobs = 10 for i := 0; i < jobs; i++ { go func() { - f.Pop(func(obj interface{}, isInInitialList bool) error { + f.Pop(context.Background(), func(obj interface{}, isInInitialList bool) error { return nil }) c <- struct{}{} diff --git a/tools/cache/fifo_test.go b/tools/cache/fifo_test.go index 39c27c6d..327f9f2d 100644 --- a/tools/cache/fifo_test.go +++ b/tools/cache/fifo_test.go @@ -17,6 +17,7 @@ limitations under the License. package cache import ( + "context" "reflect" "runtime" "testing" @@ -97,7 +98,7 @@ func TestFIFO_basic(t *testing.T) { lastInt := int(0) lastUint := uint64(0) for i := 0; i < amount*2; i++ { - switch obj := Pop(f).(testFifoObject).val.(type) { + switch obj := Pop(context.Background(), f).(testFifoObject).val.(type) { case int: if obj <= lastInt { t.Errorf("got %v (int) out of order, last was %v", obj, lastInt) @@ -131,7 +132,7 @@ func TestFIFO_addUpdate(t *testing.T) { got := make(chan testFifoObject, 2) go func() { for { - obj := Pop(f) + obj := Pop(context.Background(), f) if obj == nil { return } @@ -162,7 +163,7 @@ func TestFIFO_addReplace(t *testing.T) { got := make(chan testFifoObject, 2) go func() { for { - obj := Pop(f) + obj := Pop(context.Background(), f) if obj == nil { return } @@ -194,21 +195,21 @@ func TestFIFO_detectLineJumpers(t *testing.T) { f.Add(mkFifoObj("foo", 13)) f.Add(mkFifoObj("zab", 30)) - if e, a := 13, Pop(f).(testFifoObject).val; a != e { + if e, a := 13, Pop(context.Background(), f).(testFifoObject).val; a != e { t.Fatalf("expected %d, got %d", e, a) } f.Add(mkFifoObj("foo", 14)) // ensure foo doesn't jump back in line - if e, a := 1, Pop(f).(testFifoObject).val; a != e { + if e, a := 1, Pop(context.Background(), f).(testFifoObject).val; a != e { t.Fatalf("expected %d, got %d", e, a) } - if e, a := 30, Pop(f).(testFifoObject).val; a != e { + if e, a := 30, Pop(context.Background(), f).(testFifoObject).val; a != e { t.Fatalf("expected %d, got %d", e, a) } - if e, a := 14, Pop(f).(testFifoObject).val; a != e { + if e, a := 14, Pop(context.Background(), f).(testFifoObject).val; a != e { t.Fatalf("expected %d, got %d", e, a) } } @@ -243,15 +244,15 @@ func TestFIFO_HasSynced(t *testing.T) { { actions: []func(f *FIFO){ func(f *FIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *FIFO) { Pop(f) }, + func(f *FIFO) { Pop(context.Background(), f) }, }, expectedSynced: false, }, { actions: []func(f *FIFO){ func(f *FIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *FIFO) { Pop(f) }, - func(f *FIFO) { Pop(f) }, + func(f *FIFO) { Pop(context.Background(), f) }, + func(f *FIFO) { Pop(context.Background(), f) }, }, expectedSynced: true, }, @@ -278,7 +279,7 @@ func TestFIFO_PopShouldUnblockWhenClosed(t *testing.T) { const jobs = 10 for i := 0; i < jobs; i++ { go func() { - f.Pop(func(obj interface{}, isInInitialList bool) error { + f.Pop(context.Background(), func(obj interface{}, isInInitialList bool) error { return nil }) c <- struct{}{} diff --git a/tools/cache/reflector_test.go b/tools/cache/reflector_test.go index 558cdcee..497cf45f 100644 --- a/tools/cache/reflector_test.go +++ b/tools/cache/reflector_test.go @@ -602,7 +602,7 @@ func TestReflectorListAndWatch(t *testing.T) { // Verify we received the right objects with the right resource versions. for _, expectedObj := range tc.expectedStore { - storeObj := Pop(s).(metav1.Object) + storeObj := Pop(context.Background(), s).(metav1.Object) assert.Equal(t, expectedObj.GetName(), storeObj.GetName()) assert.Equal(t, expectedObj.GetResourceVersion(), storeObj.GetResourceVersion()) } diff --git a/tools/cache/the_real_fifo_test.go b/tools/cache/the_real_fifo_test.go index 649ea368..7aa16863 100644 --- a/tools/cache/the_real_fifo_test.go +++ b/tools/cache/the_real_fifo_test.go @@ -17,6 +17,7 @@ limitations under the License. package cache import ( + "context" "fmt" "reflect" "runtime" @@ -38,7 +39,7 @@ const closedFIFOName = "FIFO WAS CLOSED" func popN(queue Queue, count int) []interface{} { result := []interface{}{} for i := 0; i < count; i++ { - queue.Pop(func(obj interface{}, isInInitialList bool) error { + queue.Pop(context.Background(), func(obj interface{}, isInInitialList bool) error { result = append(result, obj) return nil }) @@ -48,7 +49,7 @@ func popN(queue Queue, count int) []interface{} { // helper function to reduce stuttering func testRealFIFOPop(f *RealFIFO) testFifoObject { - val := Pop(f) + val := Pop(context.Background(), f) if val == nil { return testFifoObject{name: closedFIFOName} } @@ -404,7 +405,7 @@ func TestRealFIFO_transformer(t *testing.T) { } for i := 0; i < len(expected1); i++ { - obj, err := f.Pop(func(o interface{}, isInInitialList bool) error { return nil }) + obj, err := f.Pop(context.Background(), func(o interface{}, isInInitialList bool) error { return nil }) if err != nil { t.Fatalf("got nothing on try %v?", i) } @@ -593,7 +594,7 @@ func TestRealFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -622,7 +623,7 @@ func TestRealFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -653,7 +654,7 @@ func TestRealFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -685,7 +686,7 @@ func TestRealFIFO_ReplaceMakesDeletions(t *testing.T) { } for i, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("%d Expected %#v, got %#v", i, e, a) } @@ -707,7 +708,7 @@ func TestRealFIFO_ReplaceMakesDeletions(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -738,7 +739,7 @@ func TestRealFIFO_ReplaceMakesDeletionsReplaced(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -764,7 +765,7 @@ func TestRealFIFO_UpdateResyncRace(t *testing.T) { } for _, expected := range expectedList { - cur := Pop(f).(Deltas) + cur := Pop(context.Background(), f).(Deltas) if e, a := expected, cur; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } @@ -900,15 +901,15 @@ func TestRealFIFO_HasSynced(t *testing.T) { { actions: []func(f *RealFIFO){ func(f *RealFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *RealFIFO) { Pop(f) }, + func(f *RealFIFO) { Pop(context.Background(), f) }, }, expectedSynced: false, }, { actions: []func(f *RealFIFO){ func(f *RealFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("b", 2)}, "0") }, - func(f *RealFIFO) { Pop(f) }, - func(f *RealFIFO) { Pop(f) }, + func(f *RealFIFO) { Pop(context.Background(), f) }, + func(f *RealFIFO) { Pop(context.Background(), f) }, }, expectedSynced: true, }, @@ -917,9 +918,9 @@ func TestRealFIFO_HasSynced(t *testing.T) { // there cannot be duplicate keys in the list or apiserver is broken. actions: []func(f *RealFIFO){ func(f *RealFIFO) { f.Replace([]interface{}{mkFifoObj("a", 1), mkFifoObj("a", 2)}, "0") }, - func(f *RealFIFO) { Pop(f) }, + func(f *RealFIFO) { Pop(context.Background(), f) }, // ATTENTION: difference with delta_fifo_test, every event is delivered, so a is listed twice and must be popped twice to remove both - func(f *RealFIFO) { Pop(f) }, + func(f *RealFIFO) { Pop(context.Background(), f) }, }, expectedSynced: true, }, @@ -956,7 +957,7 @@ func TestRealFIFO_PopShouldUnblockWhenClosed(t *testing.T) { const jobs = 10 for i := 0; i < jobs; i++ { go func() { - f.Pop(func(obj interface{}, isInInitialList bool) error { + f.Pop(context.Background(), func(obj interface{}, isInInitialList bool) error { return nil }) c <- struct{}{}