|
|
|
@ -34,8 +34,10 @@ func init() {
|
|
|
|
|
|
|
|
|
|
func TestCacheNode_DelCache(t *testing.T) {
|
|
|
|
|
t.Run("del cache", func(t *testing.T) {
|
|
|
|
|
store := redistest.CreateRedis(t)
|
|
|
|
|
store.Type = redis.ClusterType
|
|
|
|
|
r, err := miniredis.Run()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer r.Close()
|
|
|
|
|
store := redis.New(r.Addr(), redis.Cluster())
|
|
|
|
|
|
|
|
|
|
cn := cacheNode{
|
|
|
|
|
rds: store,
|
|
|
|
@ -166,40 +168,99 @@ func TestCacheNode_TakeBadRedis(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCacheNode_TakeNotFound(t *testing.T) {
|
|
|
|
|
store := redistest.CreateRedis(t)
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
|
|
|
store := redistest.CreateRedis(t)
|
|
|
|
|
|
|
|
|
|
cn := cacheNode{
|
|
|
|
|
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: errTestNotFound,
|
|
|
|
|
}
|
|
|
|
|
var str string
|
|
|
|
|
err := cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return errTestNotFound
|
|
|
|
|
cn := cacheNode{
|
|
|
|
|
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: errTestNotFound,
|
|
|
|
|
}
|
|
|
|
|
var str string
|
|
|
|
|
err := cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return errTestNotFound
|
|
|
|
|
})
|
|
|
|
|
assert.True(t, cn.IsNotFound(err))
|
|
|
|
|
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
|
|
|
|
|
val, err := store.Get("any")
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, `*`, val)
|
|
|
|
|
|
|
|
|
|
store.Set("any", "*")
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
assert.True(t, cn.IsNotFound(err))
|
|
|
|
|
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
|
|
|
|
|
|
|
|
|
|
store.Del("any")
|
|
|
|
|
errDummy := errors.New("dummy")
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return errDummy
|
|
|
|
|
})
|
|
|
|
|
assert.Equal(t, errDummy, err)
|
|
|
|
|
})
|
|
|
|
|
assert.True(t, cn.IsNotFound(err))
|
|
|
|
|
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
|
|
|
|
|
val, err := store.Get("any")
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, `*`, val)
|
|
|
|
|
|
|
|
|
|
store.Set("any", "*")
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return nil
|
|
|
|
|
t.Run("not found with redis error", func(t *testing.T) {
|
|
|
|
|
r, err := miniredis.Run()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer r.Close()
|
|
|
|
|
store, err := redis.NewRedis(redis.RedisConf{
|
|
|
|
|
Host: r.Addr(),
|
|
|
|
|
Type: redis.NodeType,
|
|
|
|
|
})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
cn := cacheNode{
|
|
|
|
|
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: errTestNotFound,
|
|
|
|
|
}
|
|
|
|
|
var str string
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
r.SetError("mock error")
|
|
|
|
|
return errTestNotFound
|
|
|
|
|
})
|
|
|
|
|
assert.True(t, cn.IsNotFound(err))
|
|
|
|
|
})
|
|
|
|
|
assert.True(t, cn.IsNotFound(err))
|
|
|
|
|
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
store.Del("any")
|
|
|
|
|
errDummy := errors.New("dummy")
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
return errDummy
|
|
|
|
|
func TestCacheNode_TakeCtxWithRedisError(t *testing.T) {
|
|
|
|
|
t.Run("not found with redis error", func(t *testing.T) {
|
|
|
|
|
r, err := miniredis.Run()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer r.Close()
|
|
|
|
|
store, err := redis.NewRedis(redis.RedisConf{
|
|
|
|
|
Host: r.Addr(),
|
|
|
|
|
Type: redis.NodeType,
|
|
|
|
|
})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
cn := cacheNode{
|
|
|
|
|
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: errTestNotFound,
|
|
|
|
|
}
|
|
|
|
|
var str string
|
|
|
|
|
err = cn.Take(&str, "any", func(v any) error {
|
|
|
|
|
str = "foo"
|
|
|
|
|
r.SetError("mock error")
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
assert.Equal(t, errDummy, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCacheNode_TakeNotFoundButChangedByOthers(t *testing.T) {
|
|
|
|
|