mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: add tests (#2960)
This commit is contained in:
parent
af50a80d01
commit
9e0e01b2bc
3
core/stores/cache/cachenode.go
vendored
3
core/stores/cache/cachenode.go
vendored
@ -277,5 +277,6 @@ func (c cacheNode) processCache(ctx context.Context, key, data string, v interfa
|
||||
|
||||
func (c cacheNode) setCacheWithNotFound(ctx context.Context, key string) error {
|
||||
seconds := int(math.Ceil(c.aroundDuration(c.notFoundExpiry).Seconds()))
|
||||
return c.rds.SetexCtx(ctx, key, notFoundPlaceholder, seconds)
|
||||
_, err := c.rds.SetnxExCtx(ctx, key, notFoundPlaceholder, seconds)
|
||||
return err
|
||||
}
|
||||
|
29
core/stores/cache/cachenode_test.go
vendored
29
core/stores/cache/cachenode_test.go
vendored
@ -209,6 +209,35 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
|
||||
assert.Equal(t, errDummy, err)
|
||||
}
|
||||
|
||||
func TestCacheNode_TakeNotFoundButChangedByOthers(t *testing.T) {
|
||||
store, clean, err := redistest.CreateRedis()
|
||||
assert.NoError(t, err)
|
||||
defer clean()
|
||||
|
||||
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 {
|
||||
store.Set("any", "foo")
|
||||
return errTestNotFound
|
||||
})
|
||||
assert.True(t, cn.IsNotFound(err))
|
||||
|
||||
val, err := store.Get("any")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, "foo", val)
|
||||
}
|
||||
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
|
||||
}
|
||||
|
||||
func TestCacheNode_TakeWithExpire(t *testing.T) {
|
||||
store, clean, err := redistest.CreateRedis()
|
||||
assert.Nil(t, err)
|
||||
|
Loading…
Reference in New Issue
Block a user