mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Migrate to k8s.io/utils/clock in apimachinery
This commit is contained in:
parent
859a98c035
commit
adf82f050c
@ -21,17 +21,17 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewExpiring returns an initialized expiring cache.
|
// NewExpiring returns an initialized expiring cache.
|
||||||
func NewExpiring() *Expiring {
|
func NewExpiring() *Expiring {
|
||||||
return NewExpiringWithClock(utilclock.RealClock{})
|
return NewExpiringWithClock(clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExpiringWithClock is like NewExpiring but allows passing in a custom
|
// NewExpiringWithClock is like NewExpiring but allows passing in a custom
|
||||||
// clock for testing.
|
// clock for testing.
|
||||||
func NewExpiringWithClock(clock utilclock.Clock) *Expiring {
|
func NewExpiringWithClock(clock clock.Clock) *Expiring {
|
||||||
return &Expiring{
|
return &Expiring{
|
||||||
clock: clock,
|
clock: clock,
|
||||||
cache: make(map[interface{}]entry),
|
cache: make(map[interface{}]entry),
|
||||||
@ -40,7 +40,7 @@ func NewExpiringWithClock(clock utilclock.Clock) *Expiring {
|
|||||||
|
|
||||||
// Expiring is a map whose entries expire after a per-entry timeout.
|
// Expiring is a map whose entries expire after a per-entry timeout.
|
||||||
type Expiring struct {
|
type Expiring struct {
|
||||||
clock utilclock.Clock
|
clock clock.Clock
|
||||||
|
|
||||||
// mu protects the below fields
|
// mu protects the below fields
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExpiringCache(t *testing.T) {
|
func TestExpiringCache(t *testing.T) {
|
||||||
@ -58,7 +58,7 @@ func TestExpiringCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExpiration(t *testing.T) {
|
func TestExpiration(t *testing.T) {
|
||||||
fc := &utilclock.FakeClock{}
|
fc := &testingclock.FakeClock{}
|
||||||
c := NewExpiringWithClock(fc)
|
c := NewExpiringWithClock(fc)
|
||||||
|
|
||||||
c.Set("a", "a", time.Second)
|
c.Set("a", "a", time.Second)
|
||||||
@ -104,7 +104,7 @@ func TestExpiration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGarbageCollection(t *testing.T) {
|
func TestGarbageCollection(t *testing.T) {
|
||||||
fc := &utilclock.FakeClock{}
|
fc := &testingclock.FakeClock{}
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
key, val string
|
key, val string
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func expectEntry(t *testing.T, c *LRUExpireCache, key interface{}, value interface{}) {
|
func expectEntry(t *testing.T, c *LRUExpireCache, key interface{}, value interface{}) {
|
||||||
@ -68,7 +68,7 @@ func TestSimpleRemove(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExpiredGet(t *testing.T) {
|
func TestExpiredGet(t *testing.T) {
|
||||||
fakeClock := clock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
c := NewLRUExpireCacheWithClock(10, fakeClock)
|
c := NewLRUExpireCacheWithClock(10, fakeClock)
|
||||||
c.Add("short-lived", "12345", 1*time.Millisecond)
|
c.Add("short-lived", "12345", 1*time.Millisecond)
|
||||||
// ensure the entry expired
|
// ensure the entry expired
|
||||||
|
Loading…
Reference in New Issue
Block a user