Skip to content

Commit

Permalink
fix test case, enhance coverage rate (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
viney-shih committed Jun 10, 2022
1 parent 69a995d commit 7428dc0
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ repos:
# Go Test
#
- id: go-test-mod
args: [-v, -race]
# - id: go-test-pkg
- id: go-test-repo-mod
args: [-v, -race]
# - id: go-test-repo-pkg
#
# Go Vet
Expand Down Expand Up @@ -116,9 +118,9 @@ repos:
- id: go-fmt-repo
args: [-w]
- id: go-imports # replaces go-fmt
args: [-w]
args: [--local, github.com/viney-shih, -w]
- id: go-imports-repo # replaces go-fmt-repo
args: [-w]
args: [--local, github.com/viney-shih, -w]
#
# Style Checkers
#
Expand Down
5 changes: 1 addition & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,7 @@ func (c *cache) evictRemoteKeys(ctx context.Context, keys ...string) error {

return c.mb.send(ctx, event{
Type: EventTypeEvict,
Body: eventBody{
FID: c.mb.fid,
Keys: keys,
},
Body: eventBody{Keys: keys},
})
}

Expand Down
2 changes: 2 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (s *cacheSuite) TearDownTest() {
_ = s.ring.ForEachShard(mockCacheCTX, func(ctx context.Context, client *redis.Client) error {
return client.FlushDB(ctx).Err()
})

s.factory.Close()
}

func TestCacheSuite(t *testing.T) {
Expand Down
56 changes: 56 additions & 0 deletions empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cache

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/suite"
)

const (
mockEmptyPfx = "empty-pfx"
mockEmptyKey = "empty-key"
)

var (
mockEmptyCTX = context.Background()
)

type emptySuite struct {
suite.Suite
}

func (s *emptySuite) SetupSuite() {
}

func (s *emptySuite) TearDownSuite() {}

func (s *emptySuite) SetupTest() {}

func (s *emptySuite) TearDownTest() {
// prevent registering twice
ClearPrefix()
}

func TestEmptySuite(t *testing.T) {
suite.Run(t, new(emptySuite))
}

func (s *emptySuite) TestEmptyAdapter() {
f := NewFactory(NewEmpty(), NewEmpty())
c := f.NewCache([]Setting{
{
Prefix: mockEmptyPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})

var intf interface{}
s.Require().Equal(ErrCacheMiss, c.Get(mockEmptyCTX, mockEmptyPfx, mockEmptyKey, &intf))
s.Require().NoError(c.Set(mockEmptyCTX, mockEmptyPfx, mockEmptyKey, 123))
s.Require().NoError(c.Del(mockEmptyCTX, mockEmptyPfx, mockEmptyKey))
}
1 change: 1 addition & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (mb *messageBroker) send(ctx context.Context, e event) error {
return nil
}

e.Body.FID = mb.fid
bs, err := json.Marshal(e.Body)
if err != nil {
return err
Expand Down
136 changes: 136 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cache

import (
"context"
"testing"
"time"

"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/suite"
)

const (
mockEventPfx = "event-pfx"
mockEventKey = "event-key"
mockEventUUID = "event-from-others"
)

var (
mockEventCTX = context.Background()
)

type eventSuite struct {
suite.Suite

factory *factory
rds *rds
lfu *tinyLFU
ring *redis.Ring
mb *messageBroker
}

func (s *eventSuite) SetupSuite() {
s.ring = redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"server1": ":6379",
},
})
}

func (s *eventSuite) TearDownSuite() {}

func (s *eventSuite) SetupTest() {
s.rds = NewRedis(s.ring).(*rds)
s.lfu = NewTinyLFU(10000).(*tinyLFU)
s.mb = newMessageBroker(mockEventUUID, s.rds)
s.factory = NewFactory(s.rds, s.lfu, WithPubSub(s.rds)).(*factory)
}

func (s *eventSuite) TearDownTest() {
// prevent registering twice
ClearPrefix()
// flush redis
_ = s.ring.ForEachShard(mockCacheCTX, func(ctx context.Context, client *redis.Client) error {
return client.FlushDB(ctx).Err()
})

s.mb.close() // this makes sure only one (mb or factory) can trigger Close() once without panic
s.factory.Close()
}

func TestEventSuite(t *testing.T) {
suite.Run(t, new(eventSuite))
}

func (s *eventSuite) TestSubscribedEventsHandlerWithSet() {
c := s.factory.NewCache([]Setting{
{
Prefix: mockEventPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})

// Set() will trigger eviction in other machines
s.Require().NoError(c.Set(mockEventCTX, mockEventPfx, mockEventKey, 100))
time.Sleep(time.Millisecond * 100)
val, err := s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val) // make sure the local value existed without impacted

// trigger invalid event type, ignore it directly
// TODO: handling error messages forwarding in the future
s.Require().NoError(s.mb.send(mockEventCTX, event{Type: EventTypeNone}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val)

// trigger evict event without keys, nothing happend
s.Require().NoError(s.mb.send(mockEventCTX, event{
Type: EventTypeEvict,
Body: eventBody{Keys: []string{}},
}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val)

// simulate eviction from other machines
s.Require().NoError(s.mb.send(mockEventCTX, event{
Type: EventTypeEvict,
Body: eventBody{Keys: []string{getCacheKey(mockEventPfx, mockEventKey)}},
}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{}}, val) // local value evicted
}

func (s *eventSuite) TestSubscribedEventsHandlerWithDel() {
c := s.factory.NewCache([]Setting{
{
Prefix: mockEventPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})

// Set() will trigger eviction in other machines
s.Require().NoError(c.Set(mockEventCTX, mockEventPfx, mockEventKey, 100))
time.Sleep(time.Millisecond * 100)
val, err := s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val) // make sure the local value existed without impacted

// Del is the same behavior as Set(), but the value is killed by itself.
s.Require().NoError(c.Del(mockEventCTX, mockEventPfx, mockEventKey))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{}}, val)
}
10 changes: 7 additions & 3 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
var (
// usedPrefixs records the prefixes registered before
usedPrefixs = map[string]struct{}{}

// decoupling
uuidString = uuid.New().String
)

func newFactory(sharedCache Adapter, localCache Adapter, options ...ServiceOptions) Factory {
Expand All @@ -36,7 +39,7 @@ func newFactory(sharedCache Adapter, localCache Adapter, options ...ServiceOptio
unmarshalFunc = o.unmarshalFunc
}

id := uuid.New().String()
id := uuidString()
f := &factory{
id: id,
sharedCache: sharedCache,
Expand Down Expand Up @@ -172,9 +175,10 @@ func (f *factory) subscribedEventsHandler() func(ctx context.Context, e *event,

switch e.Type {
case EventTypeEvict:
if f.localCache != nil {
keys := e.Body.Keys
if f.localCache != nil && len(keys) > 0 {
// evict local caches
f.localCache.Del(ctx, e.Body.Keys...)
f.localCache.Del(ctx, keys...)
}
}
}
Expand Down
Loading

0 comments on commit 7428dc0

Please sign in to comment.