Skip to content

Commit

Permalink
chore: add tests (#2960)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Mar 4, 2023
1 parent dbc8f9f commit d953675
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/stores/cache/cachenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,6 @@ func (c cacheNode) processCache(ctx context.Context, key, data string, v any) er

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 changes: 29 additions & 0 deletions core/stores/cache/cachenode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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)
Expand Down

0 comments on commit d953675

Please sign in to comment.