-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_cache_test.go
More file actions
248 lines (210 loc) · 6.04 KB
/
Copy pathredis_cache_test.go
File metadata and controls
248 lines (210 loc) · 6.04 KB
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
package hcache
import (
"context"
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func cli(dbNum int) *redis.Pool {
return &redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "localhost:6379")
},
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
}
}
func flushRedisDb(cli *redis.Pool) error {
_, err := cli.Get().Do("FLUSHDB")
return err
}
func TestNewRedisCache(t *testing.T) {
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: cli(0),
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
}).(*redisCache)
if assert.NotNil(t, r) {
return
}
assert.Equal(t, "abc", r.name)
assert.Equal(t, "cache", r.prefix)
assert.Equal(t, time.Second*2, r.defaultTTL)
assert.Equal(t, MsgpackMarshal, r.marshal)
assert.Equal(t, MsgpackUnmarshal, r.unmarshal)
}
func TestRedisCache_key(t *testing.T) {
cases := []struct {
Tag string
Key string
ResultKey string
}{
{"t1", "", "cache:abc:"},
{"t2", "123", "cache:abc:123"},
{"t3", "*", "cache:abc:*"},
}
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: cli(0),
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
}).(*redisCache)
for _, c := range cases {
t.Run(c.Tag, func(t *testing.T) {
assert.Equal(t, c.ResultKey, r.key(c.Key))
})
}
}
func TestRedisCache_Name(t *testing.T) {
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: cli(0),
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
})
assert.Equal(t, "abc", r.Name())
}
func TestRedisCache_SetWithTTL(t *testing.T) {
client := cli(0)
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: client,
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
})
marshaledHi := "\xa2hi" // [162,104,105]
cases := []struct {
Tag string
Key string
FullKey string
Val string
MarshaledVal string
TTL time.Duration
}{
{"t1", "k1", "cache:abc:k1", "hi", marshaledHi, 0}, // forever
{"t2", "k2", "cache:abc:k2", "hi", marshaledHi, 0}, // forever
{"t3", "k3", "cache:abc:k3", "hi", marshaledHi, 0}, // forever
{"t3", "k4", "cache:abc:k4", "hi", marshaledHi, time.Millisecond * 2},
}
ctx := context.Background()
for _, c := range cases {
var val string
t.Run(c.Tag, func(t *testing.T) {
// Just to make sure full-key is true:
assert.Equal(t, c.FullKey, r.(*redisCache).key(c.Key))
if !assert.Nil(t, r.SetWithTTL(ctx, c.Key, c.Val, c.TTL)) {
return
}
realVal, err := redis.String(client.Get().Do("GET", c.FullKey))
assert.Nil(t, err)
assert.Equal(t, c.MarshaledVal, realVal)
if c.TTL != 0 {
time.Sleep(c.TTL)
realVal, err := redis.String(client.Get().Do("GET", c.FullKey))
// Should be expired and return empty value for it.
assert.Equal(t, "", realVal)
assert.Equal(t, redis.ErrNil, err)
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, c.Key, nil), &val)
}
})
}
}
func TestRedisCache_Get(t *testing.T) {
client := cli(0)
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: client,
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
})
marshaledHi := "\xa2hi" // [162,104,105]
cases := []struct {
Tag string
Key string
FullKey string
Val string
MarshaledVal string
TTL time.Duration
}{
{"t1", "k1", "cache:abc:k1", "hi", marshaledHi, 0}, // forever
{"t2", "k2", "cache:abc:k2", "hi", marshaledHi, 0}, // forever
{"t3", "k3", "cache:abc:k3", "hi", marshaledHi, 0}, // forever
{"t3", "k4", "cache:abc:k4", "hi", marshaledHi, time.Millisecond * 2},
}
ctx := context.Background()
for _, c := range cases {
t.Run(c.Tag, func(t *testing.T) {
// Just to make sure full-key is true:
assert.Equal(t, c.FullKey, r.(*redisCache).key(c.Key))
// Set the value
if c.TTL != 0 {
_, err := client.Get().Do("SET", c.FullKey, c.MarshaledVal, "PX", c.TTL.Milliseconds())
assert.Nil(t, err)
} else {
_, err := client.Get().Do("SET", c.FullKey, c.MarshaledVal)
assert.Nil(t, err)
}
var val string
require.Nil(t, r.Get(ctx, c.Key, &val))
assert.Equal(t, c.Val, val)
if c.TTL != 0 {
time.Sleep(c.TTL)
// Should be expired and return empty value for it.
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, c.Key, &val))
}
})
}
}
func TestRedisCache_Remove(t *testing.T) {
client := cli(0)
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: client,
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
})
var val string
ctx := context.Background()
require.Nil(t, r.Set(ctx, "k1", "hi"))
require.Nil(t, r.Set(ctx, "k2", "hi"))
require.Nil(t, r.Remove(ctx, "k1"))
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, "k1", &val))
// We should have the k2
require.Nil(t, r.Get(ctx, "k2", &val))
assert.Equal(t, "hi", val)
// Delete the k2
require.Nil(t, r.Remove(ctx, "k2"))
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, "k2", &val))
}
func TestRedisCache_Purge(t *testing.T) {
client := cli(0)
r := NewRedisCache("abc", &RedisOptions{
Prefix: "cache",
Pool: client,
Marshaler: MsgpackMarshal,
Unmarshaler: MsgpackUnmarshal,
DefaultTTL: time.Second * 2,
})
var val string
ctx := context.Background()
// Set another value which is not for the cache to make sure it doesn't remove any value!
_, err := client.Get().Do("SET", "another_key", "val")
require.Nil(t, err)
require.Nil(t, r.Set(ctx, "k1", "hi"))
require.Nil(t, r.Set(ctx, "k2", "hi"))
require.Nil(t, r.Purge(ctx))
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, "k1", &val))
assert.Equal(t, ErrKeyNotFound, r.Get(ctx, "k2", &val))
res, err := redis.String(client.Get().Do("GET", "another_key"))
assert.Nil(t, err)
assert.Equal(t, "val", res)
}