Skip to content

Commit f0b1915

Browse files
authored
Merge pull request kubernetes#85386 from mikedanese/fixstresstest
cache.Expiring: fix stress test: it's not doing anything
2 parents be4683e + 7af4249 commit f0b1915

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

staging/src/k8s.io/apimachinery/pkg/util/cache/expiring.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func (c *Expiring) Set(key interface{}, val interface{}, ttl time.Duration) {
102102

103103
heap.Push(&c.heap, &expiringHeapEntry{
104104
key: key,
105-
generation: c.generation,
106105
expiry: expiry,
106+
generation: c.generation,
107107
})
108108
}
109109

@@ -158,13 +158,13 @@ func (c *Expiring) gc(now time.Time) {
158158

159159
type expiringHeapEntry struct {
160160
key interface{}
161-
generation uint64
162161
expiry time.Time
162+
generation uint64
163163
}
164164

165-
// expiringHeap is a min-heap ordered by expiration time of it's entries. The
166-
// expiring cache uses this as a priority queue efficiently organize entries to
167-
// be garbage collected once they expire.
165+
// expiringHeap is a min-heap ordered by expiration time of its entries. The
166+
// expiring cache uses this as a priority queue to efficiently organize entries
167+
// which will be garbage collected once they expire.
168168
type expiringHeap []*expiringHeapEntry
169169

170170
var _ heap.Interface = &expiringHeap{}

staging/src/k8s.io/apimachinery/pkg/util/cache/expiring_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func TestStressExpiringCache(t *testing.T) {
264264
for i := 0; i < 256; i++ {
265265
wg.Add(1)
266266
go func() {
267+
defer wg.Done()
267268
rand := rand.New(rand.NewSource(rand.Int63()))
268269
for {
269270
select {
@@ -273,11 +274,18 @@ func TestStressExpiringCache(t *testing.T) {
273274
}
274275
key := keys[rand.Intn(numKeys)]
275276
if _, ok := cache.Get(key); !ok {
276-
cache.Set(key, struct{}{}, time.Second)
277+
cache.Set(key, struct{}{}, 50*time.Millisecond)
277278
}
278279
}
279280
}()
280281
}
281282

282-
wg.Done()
283+
wg.Wait()
284+
285+
// trigger a GC with a set and check the cache size.
286+
time.Sleep(60 * time.Millisecond)
287+
cache.Set("trigger", "gc", time.Second)
288+
if cache.Len() != 1 {
289+
t.Errorf("unexpected cache size: got=%d, want=1", cache.Len())
290+
}
283291
}

0 commit comments

Comments
 (0)