From b88ba1459744a9ab980dc5c8feb7e7a4ca2f3c3c Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 5 Feb 2021 13:32:56 +0800 Subject: [PATCH] fixes issue #425 (#438) --- core/collection/ring.go | 1 + core/collection/ring_test.go | 22 +++++++++++----------- core/stores/cache/cache.go | 5 +++++ core/stores/cache/cache_test.go | 8 ++++++-- core/stores/cache/cachenode.go | 5 +++++ core/stores/cache/cachenode_test.go | 8 ++++---- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/core/collection/ring.go b/core/collection/ring.go index 1c2ab763..6028191a 100644 --- a/core/collection/ring.go +++ b/core/collection/ring.go @@ -21,6 +21,7 @@ func NewRing(n int) *Ring { func (r *Ring) Add(v interface{}) { r.lock.Lock() defer r.lock.Unlock() + r.elements[r.index%len(r.elements)] = v r.index++ } diff --git a/core/collection/ring_test.go b/core/collection/ring_test.go index 8f44c1fd..70a126bb 100644 --- a/core/collection/ring_test.go +++ b/core/collection/ring_test.go @@ -31,17 +31,6 @@ func TestRingMore(t *testing.T) { assert.ElementsMatch(t, []interface{}{6, 7, 8, 9, 10}, elements) } -func BenchmarkRingAdd(b *testing.B) { - ring := NewRing(500) - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - for i := 0; i < b.N; i++ { - ring.Add(i) - } - } - }) -} - func TestRingAdd(t *testing.T) { ring := NewRing(5051) wg := sync.WaitGroup{} @@ -57,3 +46,14 @@ func TestRingAdd(t *testing.T) { wg.Wait() assert.Equal(t, 5050, len(ring.Take())) } + +func BenchmarkRingAdd(b *testing.B) { + ring := NewRing(500) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + for i := 0; i < b.N; i++ { + ring.Add(i) + } + } + }) +} diff --git a/core/stores/cache/cache.go b/core/stores/cache/cache.go index 27c9958c..80adcef3 100644 --- a/core/stores/cache/cache.go +++ b/core/stores/cache/cache.go @@ -14,6 +14,7 @@ type ( Cache interface { DelCache(keys ...string) error GetCache(key string, v interface{}) error + IsNotFound(err error) bool SetCache(key string, v interface{}) error SetCacheWithExpire(key string, v interface{}, expire time.Duration) error Take(v interface{}, key string, query func(v interface{}) error) error @@ -91,6 +92,10 @@ func (cc cacheCluster) GetCache(key string, v interface{}) error { return c.(Cache).GetCache(key, v) } +func (cc cacheCluster) IsNotFound(err error) bool { + return err == cc.errNotFound +} + func (cc cacheCluster) SetCache(key string, v interface{}) error { c, ok := cc.dispatcher.Get(key) if !ok { diff --git a/core/stores/cache/cache_test.go b/core/stores/cache/cache_test.go index 985cd57e..345f4d25 100644 --- a/core/stores/cache/cache_test.go +++ b/core/stores/cache/cache_test.go @@ -42,6 +42,10 @@ func (mc *mockedNode) GetCache(key string, v interface{}) error { return mc.errNotFound } +func (mc *mockedNode) IsNotFound(err error) bool { + return err == mc.errNotFound +} + func (mc *mockedNode) SetCache(key string, v interface{}) error { data, err := json.Marshal(v) if err != nil { @@ -117,7 +121,7 @@ func TestCache_SetDel(t *testing.T) { } for i := 0; i < total; i++ { var v int - assert.Equal(t, errPlaceholder, c.GetCache(fmt.Sprintf("key/%d", i), &v)) + assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v))) assert.Equal(t, 0, v) } } @@ -155,7 +159,7 @@ func TestCache_OneNode(t *testing.T) { } for i := 0; i < total; i++ { var v int - assert.Equal(t, errPlaceholder, c.GetCache(fmt.Sprintf("key/%d", i), &v)) + assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v))) assert.Equal(t, 0, v) } } diff --git a/core/stores/cache/cachenode.go b/core/stores/cache/cachenode.go index 0daef833..e66a35d9 100644 --- a/core/stores/cache/cachenode.go +++ b/core/stores/cache/cachenode.go @@ -82,6 +82,11 @@ func (c cacheNode) GetCache(key string, v interface{}) error { } } +// IsNotFound checks if the given error is the defined errNotFound. +func (c cacheNode) IsNotFound(err error) bool { + return err == c.errNotFound +} + // SetCache sets the cache with key and v, using c.expiry. func (c cacheNode) SetCache(key string, v interface{}) error { return c.SetCacheWithExpire(key, v, c.aroundDuration(c.expiry)) diff --git a/core/stores/cache/cachenode_test.go b/core/stores/cache/cachenode_test.go index ca8d4bb0..1e274222 100644 --- a/core/stores/cache/cachenode_test.go +++ b/core/stores/cache/cachenode_test.go @@ -115,8 +115,8 @@ func TestCacheNode_TakeNotFound(t *testing.T) { err = cn.Take(&str, "any", func(v interface{}) error { return errTestNotFound }) - assert.Equal(t, errTestNotFound, err) - assert.Equal(t, errTestNotFound, cn.GetCache("any", &str)) + assert.True(t, cn.IsNotFound(err)) + assert.True(t, cn.IsNotFound(cn.GetCache("any", &str))) val, err := store.Get("any") assert.Nil(t, err) assert.Equal(t, `*`, val) @@ -125,8 +125,8 @@ func TestCacheNode_TakeNotFound(t *testing.T) { err = cn.Take(&str, "any", func(v interface{}) error { return nil }) - assert.Equal(t, errTestNotFound, err) - assert.Equal(t, errTestNotFound, cn.GetCache("any", &str)) + assert.True(t, cn.IsNotFound(err)) + assert.True(t, cn.IsNotFound(cn.GetCache("any", &str))) store.Del("any") var errDummy = errors.New("dummy")