2020-01-10 01:12:27 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"runtime"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
2020-01-19 16:20:33 +00:00
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
2020-01-10 01:12:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache_LRU(t *testing.T) {
|
|
|
|
cache := New(Options{Capacity: 2})
|
|
|
|
check := newChecker(t, cache)
|
|
|
|
|
|
|
|
check("a", 1)
|
|
|
|
check("a", 1)
|
|
|
|
check("b", 2)
|
|
|
|
check("a", 2)
|
|
|
|
check("c", 3)
|
|
|
|
check("b", 4)
|
|
|
|
check("c", 4)
|
|
|
|
check("a", 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_Expires(t *testing.T) {
|
|
|
|
cache := New(Options{Capacity: 2, Expiration: time.Nanosecond})
|
|
|
|
check := newChecker(t, cache)
|
|
|
|
|
|
|
|
check("a", 1)
|
2020-01-19 16:20:33 +00:00
|
|
|
time.Sleep(time.Second)
|
2020-01-10 01:12:27 +00:00
|
|
|
check("a", 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_Fuzz(t *testing.T) {
|
2020-01-19 16:20:33 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-01-10 01:12:27 +00:00
|
|
|
cache := New(Options{Capacity: 2, Expiration: 100 * time.Millisecond})
|
|
|
|
keys := "abcdefghij"
|
|
|
|
|
|
|
|
var ops uint64
|
|
|
|
procs := runtime.GOMAXPROCS(-1)
|
2020-01-19 16:20:33 +00:00
|
|
|
|
2020-01-10 01:12:27 +00:00
|
|
|
for i := 0; i < procs; i++ {
|
2020-01-19 16:20:33 +00:00
|
|
|
ctx.Go(func() error {
|
2020-01-10 01:12:27 +00:00
|
|
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
for {
|
|
|
|
if atomic.AddUint64(&ops, 1) > 1000000 {
|
2020-01-19 16:20:33 +00:00
|
|
|
return nil
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldErr := rng.Intn(10) == 0
|
|
|
|
ran := false
|
|
|
|
kidx := rng.Intn(len(keys))
|
|
|
|
key := keys[kidx : kidx+1]
|
|
|
|
|
|
|
|
value, err := cache.Get(key, func() (interface{}, error) {
|
|
|
|
ran = true
|
|
|
|
if shouldErr {
|
|
|
|
return nil, errs.New("random error")
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if ran {
|
|
|
|
if shouldErr && err == nil {
|
2020-01-19 16:20:33 +00:00
|
|
|
return errs.New("should have errored and did not")
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
if !shouldErr && err != nil {
|
2020-01-19 16:20:33 +00:00
|
|
|
return errs.New("should not have errored but did")
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if value != key && !(ran && shouldErr) {
|
2020-01-19 16:20:33 +00:00
|
|
|
return errs.New("expected %q but got %q", key, value)
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 16:20:33 +00:00
|
|
|
})
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 16:20:33 +00:00
|
|
|
ctx.Wait()
|
2020-01-10 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// helper
|
|
|
|
//
|
|
|
|
|
|
|
|
type checker struct {
|
|
|
|
t *testing.T
|
|
|
|
cache *ExpiringLRU
|
|
|
|
calls int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newChecker(t *testing.T, cache *ExpiringLRU) func(string, int) {
|
|
|
|
return (&checker{t: t, cache: cache}).Check
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) makeCallback(v interface{}) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
c.calls++
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) Check(key string, calls int) {
|
|
|
|
value, err := c.cache.Get(key, c.makeCallback(key))
|
|
|
|
require.Equal(c.t, c.calls, calls)
|
|
|
|
require.Equal(c.t, value, key)
|
|
|
|
require.NoError(c.t, err)
|
|
|
|
}
|