Refactor util clock into it's own pkg

This commit is contained in:
Harry Zhang
2016-05-25 23:08:56 -04:00
committed by Harry Zhang
parent 1c72ba6810
commit cb14b35bde
38 changed files with 127 additions and 125 deletions

View File

@@ -21,7 +21,7 @@ import (
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/clock"
)
// ExpirationCache implements the store interface
@@ -38,7 +38,7 @@ import (
type ExpirationCache struct {
cacheStorage ThreadSafeStore
keyFunc KeyFunc
clock util.Clock
clock clock.Clock
expirationPolicy ExpirationPolicy
// expirationLock is a write lock used to guarantee that we don't clobber
// newly inserted objects because of a stale expiration timestamp comparison
@@ -58,7 +58,7 @@ type TTLPolicy struct {
Ttl time.Duration
// Clock used to calculate ttl expiration
Clock util.Clock
Clock clock.Clock
}
// IsExpired returns true if the given object is older than the ttl, or it can't
@@ -202,7 +202,7 @@ func NewTTLStore(keyFunc KeyFunc, ttl time.Duration) Store {
return &ExpirationCache{
cacheStorage: NewThreadSafeStore(Indexers{}, Indices{}),
keyFunc: keyFunc,
clock: util.RealClock{},
expirationPolicy: &TTLPolicy{ttl, util.RealClock{}},
clock: clock.RealClock{},
expirationPolicy: &TTLPolicy{ttl, clock.RealClock{}},
}
}

View File

@@ -17,7 +17,7 @@ limitations under the License.
package cache
import (
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/clock"
"k8s.io/kubernetes/pkg/util/sets"
)
@@ -43,7 +43,7 @@ func (p *FakeExpirationPolicy) IsExpired(obj *timestampedEntry) bool {
return !p.NeverExpire.Has(key)
}
func NewFakeExpirationStore(keyFunc KeyFunc, deletedKeys chan<- string, expirationPolicy ExpirationPolicy, cacheClock util.Clock) Store {
func NewFakeExpirationStore(keyFunc KeyFunc, deletedKeys chan<- string, expirationPolicy ExpirationPolicy, cacheClock clock.Clock) Store {
cacheStorage := NewThreadSafeStore(Indexers{}, Indices{})
return &ExpirationCache{
cacheStorage: &fakeThreadSafeMap{cacheStorage, deletedKeys},

View File

@@ -21,7 +21,7 @@ import (
"testing"
"time"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/clock"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/wait"
)
@@ -37,7 +37,7 @@ func TestTTLExpirationBasic(t *testing.T) {
return obj.(*timestampedEntry).obj.(testStoreObject).id, nil
},
},
util.RealClock{},
clock.RealClock{},
)
err := ttlStore.Add(testObj)
if err != nil {
@@ -71,7 +71,7 @@ func TestReAddExpiredItem(t *testing.T) {
},
}
ttlStore := NewFakeExpirationStore(
testStoreKeyFunc, deleteChan, exp, util.RealClock{})
testStoreKeyFunc, deleteChan, exp, clock.RealClock{})
testKey := "foo"
testObj := testStoreObject{id: testKey, val: "bar"}
err := ttlStore.Add(testObj)
@@ -133,7 +133,7 @@ func TestTTLList(t *testing.T) {
return obj.(*timestampedEntry).obj.(testStoreObject).id, nil
},
},
util.RealClock{},
clock.RealClock{},
)
for _, obj := range testObjs {
err := ttlStore.Add(obj)
@@ -167,7 +167,7 @@ func TestTTLPolicy(t *testing.T) {
exactlyOnTTL := fakeTime.Add(-ttl)
expiredTime := fakeTime.Add(-(ttl + 1))
policy := TTLPolicy{ttl, util.NewFakeClock(fakeTime)}
policy := TTLPolicy{ttl, clock.NewFakeClock(fakeTime)}
fakeTimestampedEntry := &timestampedEntry{obj: struct{}{}, timestamp: exactlyOnTTL}
if policy.IsExpired(fakeTimestampedEntry) {
t.Errorf("TTL cache should not expire entries exactly on ttl")