You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/cache/cachenode_test.go

236 lines
6.2 KiB
Go

package cache
4 years ago
import (
"errors"
"fmt"
4 years ago
"math/rand"
"strconv"
4 years ago
"sync"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
4 years ago
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/mathx"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/redis/redistest"
"github.com/zeromicro/go-zero/core/syncx"
4 years ago
)
4 years ago
var errTestNotFound = errors.New("not found")
4 years ago
func init() {
logx.Disable()
stat.SetReporter(nil)
}
func TestCacheNode_DelCache(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
4 years ago
assert.Nil(t, err)
3 years ago
store.Type = redis.ClusterType
4 years ago
defer clean()
4 years ago
cn := cacheNode{
4 years ago
rds: store,
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
4 years ago
errNotFound: errTestNotFound,
4 years ago
}
assert.Nil(t, cn.Del())
assert.Nil(t, cn.Del([]string{}...))
assert.Nil(t, cn.Del(make([]string, 0)...))
cn.Set("first", "one")
assert.Nil(t, cn.Del("first"))
cn.Set("first", "one")
cn.Set("second", "two")
assert.Nil(t, cn.Del("first", "second"))
4 years ago
}
3 years ago
func TestCacheNode_DelCacheWithErrors(t *testing.T) {
store, clean, err := redistest.CreateRedis()
assert.Nil(t, err)
defer clean()
3 years ago
store.Type = redis.ClusterType
cn := cacheNode{
rds: store,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
errNotFound: errTestNotFound,
}
assert.Nil(t, cn.Del("third", "fourth"))
}
4 years ago
func TestCacheNode_InvalidCache(t *testing.T) {
4 years ago
s, err := miniredis.Run()
4 years ago
assert.Nil(t, err)
4 years ago
defer s.Close()
4 years ago
cn := cacheNode{
rds: redis.New(s.Addr()),
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
4 years ago
errNotFound: errTestNotFound,
4 years ago
}
s.Set("any", "value")
var str string
assert.NotNil(t, cn.Get("any", &str))
4 years ago
assert.Equal(t, "", str)
_, err = s.Get("any")
assert.Equal(t, miniredis.ErrKeyNotFound, err)
}
4 years ago
func TestCacheNode_SetWithExpire(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
4 years ago
assert.Nil(t, err)
4 years ago
defer clean()
4 years ago
cn := cacheNode{
4 years ago
rds: store,
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
barrier: syncx.NewSingleFlight(),
4 years ago
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
errNotFound: errors.New("any"),
4 years ago
}
assert.NotNil(t, cn.SetWithExpire("key", make(chan int), time.Second))
}
func TestCacheNode_Take(t *testing.T) {
store, clean, err := redistest.CreateRedis()
assert.Nil(t, err)
defer clean()
cn := NewNode(store, syncx.NewSingleFlight(), NewStat("any"), errTestNotFound,
WithExpiry(time.Second), WithNotFoundExpiry(time.Second))
4 years ago
var str string
err = cn.Take(&str, "any", func(v interface{}) error {
*v.(*string) = "value"
return nil
})
assert.Nil(t, err)
assert.Equal(t, "value", str)
assert.Nil(t, cn.Get("any", &str))
4 years ago
val, err := store.Get("any")
4 years ago
assert.Nil(t, err)
assert.Equal(t, `"value"`, val)
}
func TestCacheNode_TakeNotFound(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
4 years ago
assert.Nil(t, err)
4 years ago
defer clean()
4 years ago
cn := cacheNode{
4 years ago
rds: store,
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
barrier: syncx.NewSingleFlight(),
4 years ago
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
4 years ago
errNotFound: errTestNotFound,
}
var str string
err = cn.Take(&str, "any", func(v interface{}) error {
return errTestNotFound
})
assert.True(t, cn.IsNotFound(err))
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
4 years ago
val, err := store.Get("any")
4 years ago
assert.Nil(t, err)
assert.Equal(t, `*`, val)
4 years ago
store.Set("any", "*")
4 years ago
err = cn.Take(&str, "any", func(v interface{}) error {
return nil
})
assert.True(t, cn.IsNotFound(err))
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
4 years ago
4 years ago
store.Del("any")
errDummy := errors.New("dummy")
4 years ago
err = cn.Take(&str, "any", func(v interface{}) error {
return errDummy
})
assert.Equal(t, errDummy, err)
}
func TestCacheNode_TakeWithExpire(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
4 years ago
assert.Nil(t, err)
4 years ago
defer clean()
4 years ago
cn := cacheNode{
4 years ago
rds: store,
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
barrier: syncx.NewSingleFlight(),
4 years ago
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
4 years ago
errNotFound: errors.New("any"),
}
var str string
err = cn.TakeWithExpire(&str, "any", func(v interface{}, expire time.Duration) error {
*v.(*string) = "value"
return nil
})
assert.Nil(t, err)
assert.Equal(t, "value", str)
assert.Nil(t, cn.Get("any", &str))
4 years ago
val, err := store.Get("any")
4 years ago
assert.Nil(t, err)
assert.Equal(t, `"value"`, val)
}
func TestCacheNode_String(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
4 years ago
assert.Nil(t, err)
4 years ago
defer clean()
4 years ago
cn := cacheNode{
4 years ago
rds: store,
4 years ago
r: rand.New(rand.NewSource(time.Now().UnixNano())),
barrier: syncx.NewSingleFlight(),
4 years ago
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
4 years ago
errNotFound: errors.New("any"),
}
4 years ago
assert.Equal(t, store.Addr, cn.String())
4 years ago
}
func TestCacheValueWithBigInt(t *testing.T) {
4 years ago
store, clean, err := redistest.CreateRedis()
assert.Nil(t, err)
4 years ago
defer clean()
cn := cacheNode{
4 years ago
rds: store,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
barrier: syncx.NewSingleFlight(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewStat("any"),
errNotFound: errors.New("any"),
}
const (
key = "key"
value int64 = 323427211229009810
)
assert.Nil(t, cn.Set(key, value))
var val interface{}
assert.Nil(t, cn.Get(key, &val))
assert.Equal(t, strconv.FormatInt(value, 10), fmt.Sprintf("%v", val))
}