From 1d175299a2576e5dba04dd0a44cf231150ba535a Mon Sep 17 00:00:00 2001 From: Qing Ju Date: Sun, 22 Nov 2020 16:35:19 -0800 Subject: [PATCH] Fixed a harmless bug where initialPopulationCount should be based on the key length not list size in DeltaFIFO#Replace() Kubernetes-commit: bc39672c0638426979feef95baeff39d170161eb --- tools/cache/delta_fifo.go | 4 ++-- tools/cache/delta_fifo_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/cache/delta_fifo.go b/tools/cache/delta_fifo.go index 148b478d..bfa36311 100644 --- a/tools/cache/delta_fifo.go +++ b/tools/cache/delta_fifo.go @@ -572,7 +572,7 @@ func (f *DeltaFIFO) Replace(list []interface{}, resourceVersion string) error { f.populated = true // While there shouldn't be any queued deletions in the initial // population of the queue, it's better to be on the safe side. - f.initialPopulationCount = len(list) + queuedDeletions + f.initialPopulationCount = keys.Len() + queuedDeletions } return nil @@ -602,7 +602,7 @@ func (f *DeltaFIFO) Replace(list []interface{}, resourceVersion string) error { if !f.populated { f.populated = true - f.initialPopulationCount = len(list) + queuedDeletions + f.initialPopulationCount = keys.Len() + queuedDeletions } return nil diff --git a/tools/cache/delta_fifo_test.go b/tools/cache/delta_fifo_test.go index 31cbe880..9e12341d 100644 --- a/tools/cache/delta_fifo_test.go +++ b/tools/cache/delta_fifo_test.go @@ -633,6 +633,15 @@ func TestDeltaFIFO_HasSynced(t *testing.T) { }, expectedSynced: true, }, + { + // This test case won't happen in practice since a Reflector, the only producer for delta_fifo today, always passes a complete snapshot consistent in time; + // 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) }, + }, + expectedSynced: true, + }, } for i, test := range tests {