From 974ba5c9aa5303e6753173aba8dc8d0f56779e49 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Wed, 4 May 2022 16:19:51 +0800 Subject: [PATCH] test: add codecov (#1863) --- core/stores/cache/cachenode.go | 18 +++++++++++++----- core/stores/cache/cachenode_test.go | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/stores/cache/cachenode.go b/core/stores/cache/cachenode.go index adb93938..69b5bf3d 100644 --- a/core/stores/cache/cachenode.go +++ b/core/stores/cache/cachenode.go @@ -123,7 +123,8 @@ func (c cacheNode) SetWithExpire(key string, val interface{}, expire time.Durati } // SetWithExpireCtx sets the cache with key and v, using given expire. -func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error { +func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interface{}, + expire time.Duration) error { data, err := jsonx.Marshal(val) if err != nil { return err @@ -145,7 +146,8 @@ func (c cacheNode) Take(val interface{}, key string, query func(val interface{}) // TakeCtx takes the result from cache first, if not found, // query from DB and set cache using c.expiry, then return the result. -func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error { +func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string, + query func(val interface{}) error) error { return c.doTake(ctx, val, key, query, func(v interface{}) error { return c.SetCtx(ctx, key, v) }) @@ -153,13 +155,15 @@ func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string, que // TakeWithExpire takes the result from cache first, if not found, // query from DB and set cache using given expire, then return the result. -func (c cacheNode) TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error { +func (c cacheNode) TakeWithExpire(val interface{}, key string, query func(val interface{}, + expire time.Duration) error) error { return c.TakeWithExpireCtx(context.Background(), val, key, query) } // TakeWithExpireCtx takes the result from cache first, if not found, // query from DB and set cache using given expire, then return the result. -func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, query func(val interface{}, expire time.Duration) error) error { +func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, + query func(val interface{}, expire time.Duration) error) error { expire := c.aroundDuration(c.expiry) return c.doTake(ctx, val, key, func(v interface{}) error { return query(v, expire) @@ -239,7 +243,11 @@ func (c cacheNode) doTake(ctx context.Context, v interface{}, key string, return nil } - // got the result from previous ongoing query + // got the result from previous ongoing query. + // why not call IncrementTotal at the beginning of this function? + // because a shared error is returned, and we don't want to count. + // for example, if the db is down, the query will be failed, we count + // the shared errors with one db failure. c.stat.IncrementTotal() c.stat.IncrementHit() diff --git a/core/stores/cache/cachenode_test.go b/core/stores/cache/cachenode_test.go index db8cc54b..bd6ee355 100644 --- a/core/stores/cache/cachenode_test.go +++ b/core/stores/cache/cachenode_test.go @@ -88,7 +88,7 @@ func TestCacheNode_InvalidCache(t *testing.T) { assert.Equal(t, miniredis.ErrKeyNotFound, err) } -func TestCacheNode_Take(t *testing.T) { +func TestCacheNode_SetWithExpire(t *testing.T) { store, clean, err := redistest.CreateRedis() assert.Nil(t, err) defer clean() @@ -100,8 +100,18 @@ func TestCacheNode_Take(t *testing.T) { lock: new(sync.Mutex), unstableExpiry: mathx.NewUnstable(expiryDeviation), stat: NewStat("any"), - errNotFound: errTestNotFound, + errNotFound: errors.New("any"), } + 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)) var str string err = cn.Take(&str, "any", func(v interface{}) error { *v.(*string) = "value"