From 8269e414168b1bc76829f50b1ede9d0cf66983d4 Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Thu, 1 Apr 2021 13:44:16 +0800 Subject: [PATCH] client-go/cache: fix the AddIfNotPresent method of the DeltaFIFO Signed-off-by: Iceber Gu Kubernetes-commit: c75bd4a535123f5032f78ffb246ed62b79617b44 --- tools/cache/delta_fifo.go | 2 +- tools/cache/delta_fifo_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/cache/delta_fifo.go b/tools/cache/delta_fifo.go index f648673e..ea25122a 100644 --- a/tools/cache/delta_fifo.go +++ b/tools/cache/delta_fifo.go @@ -340,7 +340,7 @@ func (f *DeltaFIFO) AddIfNotPresent(obj interface{}) error { if !ok { return fmt.Errorf("object must be of type deltas, but got: %#v", obj) } - id, err := f.KeyOf(deltas.Newest().Object) + id, err := f.KeyOf(deltas) if err != nil { return KeyError{obj, err} } diff --git a/tools/cache/delta_fifo_test.go b/tools/cache/delta_fifo_test.go index ec4e4ff2..92247c4b 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 ( + "errors" "fmt" "reflect" "runtime" @@ -541,6 +542,11 @@ func TestDeltaFIFO_detectLineJumpers(t *testing.T) { func TestDeltaFIFO_addIfNotPresent(t *testing.T) { f := NewDeltaFIFOWithOptions(DeltaFIFOOptions{KeyFunction: testFifoObjectKeyFunc}) + emptyDeltas := Deltas{} + if err := f.AddIfNotPresent(emptyDeltas); err == nil || !errors.Is(err, ErrZeroLengthDeltasObject) { + t.Errorf("Expected error '%v', got %v", ErrZeroLengthDeltasObject, err) + } + f.Add(mkFifoObj("b", 3)) b3 := Pop(f) f.Add(mkFifoObj("c", 4))