client-go/cache: fix the AddIfNotPresent method of the DeltaFIFO

Signed-off-by: Iceber Gu <wei.cai-nat@daocloud.io>
This commit is contained in:
Iceber Gu 2021-04-01 13:44:16 +08:00
parent 38ff5f6884
commit c75bd4a535
2 changed files with 7 additions and 1 deletions

View File

@ -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}
}

View File

@ -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))