Skip to content

Commit

Permalink
fix: enhance test coverage (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: viney-shih <[email protected]>
  • Loading branch information
VineyAT and viney-shih committed Jun 18, 2022
1 parent 498e3c9 commit 6913893
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
10 changes: 7 additions & 3 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
var (
// errSelfEvent indicates event triggered by itself.
errSelfEvent = errors.New("event triggered by itself")
// errNoEventType indicates no event types
errNoEventType = errors.New("no event types")
)

// eventType is an enumeration of events used to communicate with each other via Pubsub.
Expand Down Expand Up @@ -95,13 +97,13 @@ func (mb *messageBroker) send(ctx context.Context, e event) error {

func (mb *messageBroker) listen(
ctx context.Context, types []eventType, cb func(context.Context, *event, error),
) {
) error {
if !mb.registered() {
return
return nil
}

if len(types) == 0 {
return
return errNoEventType
}

topics := make([]string, len(types))
Expand Down Expand Up @@ -134,4 +136,6 @@ func (mb *messageBroker) listen(
cb(ctx, &e, nil)
}
}()

return nil
}
31 changes: 31 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,34 @@ func (s *eventSuite) TestSubscribedEventsHandlerWithDel() {
s.Require().NoError(err)
s.Require().Equal([]Value{{}}, val)
}

func (s *eventSuite) TestUnnormalEvent() {
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

// nothing happened due to no handling on such event
s.Require().NoError(s.rds.Pub(mockEventCTX, "not-existed", nil))

// TODO: handle this error in the future
// invalid json format.
s.Require().NoError(s.rds.Pub(mockEventCTX, EventTypeEvict.Topic(), []byte("")))
}

func (s *eventSuite) TestListenNoEvents() {
mb := newMessageBroker(mockEventUUID, s.rds)
s.Require().Equal(errNoEventType, mb.listen(mockEventCTX, []eventType{}, func(ctx context.Context, e *event, err error) {}))
}
37 changes: 37 additions & 0 deletions factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -248,3 +249,39 @@ func (s *factorySuite) TestNewCacheWithOnlyUnmarshal() {
},
})
}

func (s *factorySuite) TestGetPrefixAndKey() {
tests := []struct {
Desc string
CacheKey string
ExpPfx string
ExpKey string
}{
{
Desc: "invalid cache key without delimiter",
CacheKey: "12345",
ExpPfx: "12345",
ExpKey: "",
},
{
Desc: "invalid cache key with only one delimiter",
CacheKey: fmt.Sprintf("%s%s%s", "123", cacheDelim, "abc"),
ExpPfx: "abc",
ExpKey: "",
},
{
Desc: "normal case",
CacheKey: getCacheKey("prefix", "key"),
ExpPfx: "prefix",
ExpKey: "key",
},
}

for _, t := range tests {
pfx, key := getPrefixAndKey(t.CacheKey)
s.Require().Equal(t.ExpPfx, pfx, t.Desc)
s.Require().Equal(t.ExpKey, key, t.Desc)

s.TearDownTest()
}
}

0 comments on commit 6913893

Please sign in to comment.