Skip to content

Commit

Permalink
test: add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshenzzg committed Mar 7, 2024
1 parent 24f0d64 commit 47cba5b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ var _ = Describe("Cache", func() {
Expect(ret).To(Equal(map[int]*object{}))
}
})

It("with will skip elements that remote MSet error", func() {
if cache.CacheType() == TypeRemote {
codecErrCache := New(WithName("codecErr"),
WithRemote(&mockGoRedisMGetMSetErrAdapter{}))
cacheT := NewT[int, *object](codecErrCache)
ids := []int{1, 2, 3}
// 1st marshal error, but return origin load func data
ret := cacheT.MGet(context.Background(), "key", ids,
func(ctx context.Context, ids []int) (map[int]*object, error) {
return map[int]*object{1: {Str: "str1", Num: 1}, 2: {Str: "str2", Num: 2}}, nil
})
Expect(ret).To(Equal(map[int]*object{1: {Str: "str1", Num: 1}, 2: {Str: "str2", Num: 2}}))
// 2nd cache hit placeholder "*", then return miss
ret = cacheT.MGet(context.Background(), "key", ids, nil)
Expect(ret).To(Equal(map[int]*object{}))
}
})
})

Describe("Once func", func() {
Expand Down Expand Up @@ -738,3 +756,40 @@ func (mockEncode) Unmarshal(data []byte, v interface{}) error {
func (mockEncode) Name() string {
return mockMarshalErr
}

var _ remote.Remote = (*mockGoRedisMGetMSetErrAdapter)(nil)

type mockGoRedisMGetMSetErrAdapter struct {
}

func (m mockGoRedisMGetMSetErrAdapter) SetEX(ctx context.Context, key string, value any, expire time.Duration) error {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) SetNX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) SetXX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) Get(ctx context.Context, key string) (val string, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) Del(ctx context.Context, key string) (val int64, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) MGet(ctx context.Context, keys ...string) (map[string]any, error) {
return nil, errors.New("any")
}

func (m mockGoRedisMGetMSetErrAdapter) MSet(ctx context.Context, value map[string]any, expire time.Duration) error {
return errors.New("any")
}

func (m mockGoRedisMGetMSetErrAdapter) Nil() error {
panic("implement me")
}

0 comments on commit 47cba5b

Please sign in to comment.