Merge pull request #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.
This commit is contained in:
Kubernetes Submit Queue 2017-01-10 16:07:12 -08:00 committed by GitHub
commit 7d2f798052
2 changed files with 11 additions and 3 deletions

View File

@ -26,7 +26,10 @@ go_test(
], ],
library = ":go_default_library", library = ":go_default_library",
tags = ["automanaged"], tags = ["automanaged"],
deps = ["//vendor:github.com/golang/groupcache/lru"], deps = [
"//pkg/util/clock:go_default_library",
"//vendor:github.com/golang/groupcache/lru",
],
) )
filegroup( filegroup(

View File

@ -20,6 +20,8 @@ import (
"testing" "testing"
"time" "time"
"k8s.io/kubernetes/pkg/util/clock"
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
) )
@ -43,8 +45,11 @@ func TestSimpleGet(t *testing.T) {
} }
func TestExpiredGet(t *testing.T) { func TestExpiredGet(t *testing.T) {
c := NewLRUExpireCache(10) fakeClock := clock.NewFakeClock(time.Now())
c.Add("short-lived", "12345", 0*time.Second) c := NewLRUExpireCacheWithClock(10, fakeClock)
c.Add("short-lived", "12345", 1*time.Millisecond)
// ensure the entry expired
fakeClock.Step(2 * time.Millisecond)
expectNotEntry(t, c, "short-lived") expectNotEntry(t, c, "short-lived")
} }