Skip to content

Commit 7d2f798

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#39511 from zhouhaibing089/lru-time-ut
Automatic merge from submit-queue (batch tested with PRs 34488, 39511, 39619, 38342, 39491) use fake clock in lruexpiration cache test when the system clock is extremely slow(usually see in VMs), this [check](https://github.com/kubernetes/kubernetes/blob/master/pkg/util/cache/lruexpirecache.go#L74) might still return the value. ```go if c.clock.Now().After(e.(*cacheEntry).expireTime) { go c.remove(key) return nil, false } ``` that means even we set the ttl to be 0 second, the after check might still be false(because the clock is too slow, and thus equals). the change here helps to reduce flakes.
2 parents 41689b1 + f12a6c1 commit 7d2f798

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pkg/util/cache/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ go_test(
2626
],
2727
library = ":go_default_library",
2828
tags = ["automanaged"],
29-
deps = ["//vendor:github.com/golang/groupcache/lru"],
29+
deps = [
30+
"//pkg/util/clock:go_default_library",
31+
"//vendor:github.com/golang/groupcache/lru",
32+
],
3033
)
3134

3235
filegroup(

pkg/util/cache/lruexpirecache_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"testing"
2121
"time"
2222

23+
"k8s.io/kubernetes/pkg/util/clock"
24+
2325
"github.com/golang/groupcache/lru"
2426
)
2527

@@ -43,8 +45,11 @@ func TestSimpleGet(t *testing.T) {
4345
}
4446

4547
func TestExpiredGet(t *testing.T) {
46-
c := NewLRUExpireCache(10)
47-
c.Add("short-lived", "12345", 0*time.Second)
48+
fakeClock := clock.NewFakeClock(time.Now())
49+
c := NewLRUExpireCacheWithClock(10, fakeClock)
50+
c.Add("short-lived", "12345", 1*time.Millisecond)
51+
// ensure the entry expired
52+
fakeClock.Step(2 * time.Millisecond)
4853
expectNotEntry(t, c, "short-lived")
4954
}
5055

0 commit comments

Comments
 (0)