diff --git a/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring.go b/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring.go index 712bb5dc85d..84b4f588497 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring.go @@ -102,8 +102,8 @@ func (c *Expiring) Set(key interface{}, val interface{}, ttl time.Duration) { heap.Push(&c.heap, &expiringHeapEntry{ key: key, - generation: c.generation, expiry: expiry, + generation: c.generation, }) } @@ -158,13 +158,13 @@ func (c *Expiring) gc(now time.Time) { type expiringHeapEntry struct { key interface{} - generation uint64 expiry time.Time + generation uint64 } -// expiringHeap is a min-heap ordered by expiration time of it's entries. The -// expiring cache uses this as a priority queue efficiently organize entries to -// be garbage collected once they expire. +// expiringHeap is a min-heap ordered by expiration time of its entries. The +// expiring cache uses this as a priority queue to efficiently organize entries +// which will be garbage collected once they expire. type expiringHeap []*expiringHeapEntry var _ heap.Interface = &expiringHeap{} diff --git a/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring_test.go b/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring_test.go index 4e0b8f83d81..53f8707651a 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/cache/expiring_test.go @@ -264,6 +264,7 @@ func TestStressExpiringCache(t *testing.T) { for i := 0; i < 256; i++ { wg.Add(1) go func() { + defer wg.Done() rand := rand.New(rand.NewSource(rand.Int63())) for { select { @@ -273,11 +274,18 @@ func TestStressExpiringCache(t *testing.T) { } key := keys[rand.Intn(numKeys)] if _, ok := cache.Get(key); !ok { - cache.Set(key, struct{}{}, time.Second) + cache.Set(key, struct{}{}, 50*time.Millisecond) } } }() } - wg.Done() + wg.Wait() + + // trigger a GC with a set and check the cache size. + time.Sleep(60 * time.Millisecond) + cache.Set("trigger", "gc", time.Second) + if cache.Len() != 1 { + t.Errorf("unexpected cache size: got=%d, want=1", cache.Len()) + } }