Skip to content

Commit

Permalink
add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 20, 2019
1 parent 07a4d66 commit 89ce2a2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lcw

import (
"errors"
"fmt"
"math/rand"
"strings"
Expand Down Expand Up @@ -33,6 +34,20 @@ func TestCache_Get(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "result", res.(string))
assert.Equal(t, int32(1), atomic.LoadInt32(&coldCalls), "cache hit")

_, err = lc.Get("key-2", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result2", errors.New("some error")
})
assert.NotNil(t, err)
assert.Equal(t, int32(2), atomic.LoadInt32(&coldCalls), "cache hit")

_, err = lc.Get("key-2", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result2", errors.New("some error")
})
assert.NotNil(t, err)
assert.Equal(t, int32(3), atomic.LoadInt32(&coldCalls), "cache hit")
}

func TestCache_Peek(t *testing.T) {
Expand Down Expand Up @@ -200,6 +215,34 @@ func TestCache_MaxCacheSizeParallel(t *testing.T) {
t.Log("size", lc.currentSize)
}

func TestCache_MaxKeySize(t *testing.T) {
lc, err := NewCache(MaxKeySize(5))
require.Nil(t, err)

res, err := lc.Get("key", func() (Value, error) {
return "value", nil
})
assert.Nil(t, err)
assert.Equal(t, "value", res.(string))

res, err = lc.Get("key", func() (Value, error) {
return "valueXXX", nil
})
assert.Nil(t, err)
assert.Equal(t, "value", res.(string), "cached")

res, err = lc.Get("key1234", func() (Value, error) {
return "value", nil
})
assert.Nil(t, err)
assert.Equal(t, "value", res.(string))

res, err = lc.Get("key1234", func() (Value, error) {
return "valueXYZ", nil
})
assert.Nil(t, err)
assert.Equal(t, "valueXYZ", res.(string), "not cached")
}
func TestCache_Parallel(t *testing.T) {
var coldCalls int32
lc, err := NewCache()
Expand Down Expand Up @@ -265,6 +308,25 @@ func TestCache_Invalidate(t *testing.T) {
assert.Equal(t, "result-xxx", res.(string), "not from the cache")
}

func TestCache_Purge(t *testing.T) {
var coldCalls int32
lc, err := NewCache()
require.Nil(t, err)

// fill cache
for i := 0; i < 1000; i++ {
_, err := lc.Get(fmt.Sprintf("key-%d", i), func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return fmt.Sprintf("result-%d", i), nil
})
require.Nil(t, err)
}
assert.Equal(t, int32(1000), atomic.LoadInt32(&coldCalls))
assert.Equal(t, 1000, lc.backend.Len())

lc.Purge()
assert.Equal(t, 0, lc.backend.Len(), "all keys removed")
}
func TestCache_BadOptions(t *testing.T) {
_, err := NewCache(MaxCacheSize(-1))
assert.EqualError(t, err, "failed to set cache option: negative max cache size")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module github.com/go-pkgz/lcw
require (
github.com/hashicorp/golang-lru v0.5.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 comments on commit 89ce2a2

Please sign in to comment.