Skip to content

Commit

Permalink
cache: improve ttl test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Loong committed May 23, 2019
1 parent 7568340 commit 2a052ea
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions cache/ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ var _ = Describe("ttl store", func() {
Context("when reading and writing", func() {
It("should be able read and write value without any error", func() {
readAndWrite := func(key string, value testStruct) bool {
st := json.New(memdb.New())
if key == "" {
return true
}
cache, err := NewTTL(st, 10*time.Second)
cache, err := NewTTL(json.New(memdb.New()), time.Second)
Expect(err).NotTo(HaveOccurred())

var newValue testStruct
Expand All @@ -64,8 +63,7 @@ var _ = Describe("ttl store", func() {
Context("when iterating", func() {
It("should be able to return the correct number of values in the store", func() {
iterating := func(key string, value testStruct) bool {
st := json.New(memdb.New())
cache, err := NewTTL(st, 100*time.Millisecond)
cache, err := NewTTL(json.New(memdb.New()), 100*time.Millisecond)
Expect(err).NotTo(HaveOccurred())

// Expect the initial size to be 0.
Expand Down Expand Up @@ -100,8 +98,7 @@ var _ = Describe("ttl store", func() {

It("should be able iterate through the store", func() {
iterating := func(key string, value testStruct) bool {
st := json.New(memdb.New())
cache, err := NewTTL(st, 10*time.Second)
cache, err := NewTTL(json.New(memdb.New()), time.Second)
Expect(err).NotTo(HaveOccurred())

// Insert random number of values into the store.
Expand Down Expand Up @@ -144,8 +141,7 @@ var _ = Describe("ttl store", func() {

It("should only give us valid data when iterating", func() {
iterating := func(key string, value testStruct) bool {
st := json.New(memdb.New())
cache, err := NewTTL(st, 100*time.Millisecond)
cache, err := NewTTL(json.New(memdb.New()), 100*time.Millisecond)
Expect(err).NotTo(HaveOccurred())

// Insert random number of values into the store.
Expand All @@ -172,11 +168,11 @@ var _ = Describe("ttl store", func() {
Context("when reading and writing with data-expiration", func() {
It("should be able to store a struct with pre-defined value type", func() {
readAndWrite := func(key string, value testStruct) bool {
st := json.New(memdb.New())
if key == "" {
return true
}
cache, err := NewTTL(st, 10*time.Millisecond)

cache, err := NewTTL(json.New(memdb.New()), 100*time.Millisecond)
Expect(err).NotTo(HaveOccurred())

var newValue testStruct
Expand All @@ -186,7 +182,7 @@ var _ = Describe("ttl store", func() {
Expect(cache.Get(key, &newValue)).NotTo(HaveOccurred())
Expect(reflect.DeepEqual(value, newValue)).Should(BeTrue())

time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
Expect(cache.Get(key, &newValue)).To(Equal(ErrExpired))

return true
Expand All @@ -200,10 +196,10 @@ var _ = Describe("ttl store", func() {
if key == "" {
return true
}
st := json.New(memdb.New())
Expect(st.Insert(key, value)).NotTo(HaveOccurred())
store := json.New(memdb.New())
Expect(store.Insert(key, value)).NotTo(HaveOccurred())

cache, err := NewTTL(st, 10*time.Second)
cache, err := NewTTL(store, 10*time.Second)
Expect(err).NotTo(HaveOccurred())

var newValue testStruct
Expand Down

0 comments on commit 2a052ea

Please sign in to comment.