forked from viney-shih/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_test.go
250 lines (216 loc) · 6.18 KB
/
factory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cache
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"reflect"
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/suite"
)
const (
mockFactPfx = "fact-pfx"
mockFactKey = "fact-key"
)
var (
mockFactoryCTX = context.Background()
)
type factorySuite struct {
suite.Suite
factory *factory
rds *rds
lfu *tinyLFU
ring *redis.Ring
}
func (s *factorySuite) SetupSuite() {
s.ring = redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"server1": ":6379",
},
})
}
func (s *factorySuite) TearDownSuite() {}
func (s *factorySuite) SetupTest() {
s.rds = NewRedis(s.ring).(*rds)
s.lfu = NewTinyLFU(10000).(*tinyLFU)
s.factory = NewFactory(s.rds, s.lfu).(*factory)
}
func (s *factorySuite) TearDownTest() {
// prevent registering twice
ClearPrefix()
// flush redis
_ = s.ring.ForEachShard(mockFactoryCTX, func(ctx context.Context, client *redis.Client) error {
return client.FlushDB(ctx).Err()
})
s.factory.Close()
}
func TestFactorySuite(t *testing.T) {
suite.Run(t, new(factorySuite))
}
func (s *factorySuite) TestNewFactoryWithOnlyMarshal() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("both of Marshal and Unmarshal functions need to be specified"), r)
}()
NewFactory(s.rds, s.lfu, WithMarshalFunc(json.Marshal))
}
func (s *factorySuite) TestNewFactoryWithOnlyUnmarshal() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("both of Marshal and Unmarshal functions need to be specified"), r)
}()
NewFactory(s.rds, s.lfu, WithUnmarshalFunc(json.Unmarshal))
}
func (s *factorySuite) TestNewFactoryWithBoth() {
f := NewFactory(s.rds, s.lfu, WithMarshalFunc(xml.Marshal), WithUnmarshalFunc(xml.Unmarshal)).(*factory)
s.Require().True(reflect.ValueOf(xml.Marshal).Pointer() == reflect.ValueOf(f.marshal).Pointer())
s.Require().True(reflect.ValueOf(xml.Unmarshal).Pointer() == reflect.ValueOf(f.unmarshal).Pointer())
}
func (s *factorySuite) TestNewFactoryWithCacheHitAndMiss() {
hitCount := 0
missCount := 0
// Due to use share cache only, init factory with NewEmpty()
f := NewFactory(s.rds, NewEmpty(),
OnCacheHitFunc(func(prefix, key string, count int) {
s.Require().Equal(mockFactPfx, prefix)
s.Require().Equal(mockFactKey, key)
hitCount += count
}),
OnCacheMissFunc(func(prefix, key string, count int) {
s.Require().Equal(mockFactPfx, prefix)
s.Require().Equal(mockFactKey, key)
missCount += count
}),
)
var ret int
var stage string
c := f.NewCache([]Setting{
{
Prefix: mockFactPfx,
CacheAttributes: map[Type]Attribute{SharedCacheType: {time.Hour}},
},
})
stage = "before"
s.Require().Equal(0, hitCount, stage)
s.Require().Equal(0, missCount, stage)
stage = "get and miss"
s.Require().Equal(ErrCacheMiss, c.Get(mockFactoryCTX, mockFactPfx, mockFactKey, &ret))
s.Require().Equal(0, ret, stage)
s.Require().Equal(0, hitCount, stage)
s.Require().Equal(1, missCount, stage)
stage = "set and get"
s.Require().NoError(c.Set(mockFactoryCTX, mockFactPfx, mockFactKey, 100))
s.Require().NoError(c.Get(mockFactoryCTX, mockFactPfx, mockFactKey, &ret))
s.Require().Equal(100, ret, stage)
s.Require().Equal(1, hitCount, stage)
s.Require().Equal(1, missCount, stage)
}
func (s *factorySuite) TestNewFactoryWithCostAddAndEvict() {
costAdd := 0
costEvict := 0
f := NewFactory(s.rds, s.lfu,
OnLocalCacheCostAddFunc(func(prefix, key string, cost int) {
s.Require().Equal(mockFactPfx, prefix)
s.Require().Equal(mockFactKey, key)
costAdd += cost
}),
OnLocalCacheCostEvictFunc(func(prefix, key string, cost int) {
s.Require().Equal(mockFactPfx, prefix)
s.Require().Equal(mockFactKey, key)
costEvict += cost
}),
)
//var ret int
var stage string
var bs []byte
var err error
c := f.NewCache([]Setting{
{
Prefix: mockFactPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
MarshalFunc: json.Marshal,
UnmarshalFunc: json.Unmarshal,
},
})
stage = "before"
s.Require().Equal(0, costAdd, stage)
s.Require().Equal(0, costEvict, stage)
stage = "set"
s.Require().NoError(c.Set(mockFactoryCTX, mockFactPfx, mockFactKey, 100))
bs, err = json.Marshal(100)
s.Require().NoError(err, stage)
s.Require().Equal(len(bs), costAdd, stage)
s.Require().Equal(0, costEvict, stage)
stage = "del"
s.Require().NoError(c.Del(mockFactoryCTX, mockFactPfx, mockFactKey))
s.Require().Equal(len(bs), costAdd, stage)
s.Require().Equal(len(bs), costEvict, stage)
}
func (s *factorySuite) TestNewCacheWithoutCacheType() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("no cache type indicated"), r)
}()
s.factory.NewCache([]Setting{{Prefix: "noCacheType"}})
}
func (s *factorySuite) TestNewCacheWithEmptyPrefix() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("not allowed empty prefix"), r)
}()
s.factory.NewCache([]Setting{{Prefix: ""}})
}
func (s *factorySuite) TestNewCacheWithDuplicatedPrefix() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("duplicated prefix"), r)
}()
s.factory.NewCache([]Setting{
{
Prefix: "exist",
CacheAttributes: map[Type]Attribute{SharedCacheType: {time.Hour}},
},
{
Prefix: "exist",
CacheAttributes: map[Type]Attribute{SharedCacheType: {time.Second}},
},
})
}
func (s *factorySuite) TestNewCacheWithOnlyMarshal() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("both of Marshal and Unmarshal functions need to be specified"), r)
}()
s.factory.NewCache([]Setting{
{
Prefix: "OnlyMarshal",
CacheAttributes: map[Type]Attribute{SharedCacheType: {time.Hour}},
MarshalFunc: json.Marshal,
},
})
}
func (s *factorySuite) TestNewCacheWithOnlyUnmarshal() {
defer func() {
r := recover()
s.Require().NotNil(r)
s.Require().Equal(errors.New("both of Marshal and Unmarshal functions need to be specified"), r)
}()
s.factory.NewCache([]Setting{
{
Prefix: "OnlyMarshal",
CacheAttributes: map[Type]Attribute{SharedCacheType: {time.Hour}},
UnmarshalFunc: json.Unmarshal,
},
})
}