-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_test.go
155 lines (131 loc) · 3.39 KB
/
redis_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
package recache
import (
"testing"
"github.com/alicebob/miniredis"
"github.com/go-redis/redis"
)
var (
cl *redis.Client
cache RedisCache
)
func TestMain(m *testing.M) {
s, err := miniredis.Run()
if err != nil {
panic(err)
}
defer s.Close()
cl = redis.NewClient(&redis.Options{
Addr: s.Addr(),
})
cache = NewRedisCache(cl)
m.Run()
}
func TestRedisCache_Set(t *testing.T) {
err := cache.Set("foo", "bar", 10)
if err != nil {
t.Errorf("unexpected error: %q", err)
}
res := cl.Get("foo").Val()
if res != "bar" {
t.Errorf("unexpected result: %q", res)
}
}
func TestRedisCache_Get(t *testing.T) {
cl.Set("foo", "bar", 0)
res, err := cache.Get("foo")
if err != nil {
t.Errorf("unexpected error: %q", err)
}
if string(res) != "bar" {
t.Errorf("unexpected result: %q", res)
}
}
// TODO fix this test. Implement logic for that
//func TestRedisCache_Set_RemoveOldTags(t *testing.T) {
// // we mark this cache entry with tag "tag1"
// err := cache.Set("foo", "bar", 10, "tag1")
// if err != nil {
// t.Errorf("unexpected error: %q", err)
// }
//
// // then, we remark this cache entry with tag "tag2"
// err = cache.Set("foo", "bar", 10, "tag2")
// if err != nil {
// t.Errorf("unexpected error: %q", err)
// }
//
// // clear by "tag1"
// // it must not clear "foo" entry since "tag1" is no longer related to that entry.
// err = cache.ClearByTag("tag1")
// if err != nil {
// t.Errorf("unexpected error: %q", err)
// }
//
// if res, _ := cache.Get("foo"); string(res) != "bar" {
// t.Errorf("unexpected result: %q", string(res))
// }
//}
func TestRedisCache_ClearByTag_ClearOne(t *testing.T) {
// set up keys
if err := cache.Set("foo", "bar", 10, "tag1", "tag2"); err != nil {
t.Errorf("unexpected error: %q", err)
}
if res, _ := cache.Get("foo"); string(res) != "bar" {
t.Errorf("unexpected result: %q", string(res))
}
if err := cache.Set("foo2", "bar", 10, "tag2", "tag3"); err != nil {
t.Errorf("unexpected error: %q", err)
}
if res, _ := cache.Get("foo2"); string(res) != "bar" {
t.Errorf("unexpected result: %q", string(res))
}
// clear by tag
err := cache.ClearByTag("tag1")
if err != nil {
t.Errorf("unexpected error: %q", err)
}
// check that only ONE key has been flushed
res, err := cache.Get("foo")
if err != ErrKeyNotFound {
t.Errorf("unexpected error: %q", err)
}
if string(res) != "" {
t.Errorf("unexpected result: %q", string(res))
}
res, err = cache.Get("foo2")
if err != nil {
t.Errorf("unexpected error: %q", err)
}
if string(res) != "bar" {
t.Errorf("unexpected result: %q", string(res))
}
}
func TestRedisCache_ClearByTag_CleanAll(t *testing.T) {
// set up keys
if err := cache.Set("foo", "bar", 10, "tag1", "tag2"); err != nil {
t.Errorf("unexpected error: %q", err)
}
if res, _ := cache.Get("foo"); string(res) != "bar" {
t.Errorf("unexpected result: %q", string(res))
}
if err := cache.Set("foo2", "bar", 10, "tag2", "tag3"); err != nil {
t.Errorf("unexpected error: %q", err)
}
if res, _ := cache.Get("foo2"); string(res) != "bar" {
t.Errorf("unexpected result: %q", string(res))
}
// clear by tag
err := cache.ClearByTag("tag2")
if err != nil {
t.Errorf("unexpected error: %q", err)
}
// check that ALL keys have been flushed
_, err = cache.Get("foo")
if err != ErrKeyNotFound {
t.Errorf("unexpected error: %q", err)
}
_, err = cache.Get("foo2")
if err != ErrKeyNotFound {
t.Errorf("unexpected error: %q", err)
}
}