-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_test.go
253 lines (200 loc) · 4.76 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
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
251
252
253
package possessions
import (
"context"
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/gofrs/uuid"
)
func TestRedisStorerNew(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping long test")
}
r := redis.Options{
Password: "test",
}
storer, err := NewRedisStorer(r, 2)
if err != nil {
t.Error(err)
}
if storer.maxAge != 2 {
t.Error("expected max age to be 2")
}
if storer.client == nil {
t.Error("Expected client to be created")
}
}
func TestRedisStorerNewDefault(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping long test")
}
storer, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
if storer.client == nil {
t.Error("Expected client to be created")
}
if storer.maxAge != time.Hour*24*2 {
t.Error("expected max age to be 2 days")
}
}
func TestRedisStorerAll(t *testing.T) {
t.Parallel()
s, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
ctx := context.Background()
list, err := s.All(ctx)
if err != nil {
t.Error("expected no error on empty list")
}
if len(list) > 0 {
t.Error("Expected len 0")
}
s.Set(ctx, "hi", "hello")
s.Set(ctx, "yo", "friend")
list, err = s.All(ctx)
if err != nil {
t.Error(err)
}
if len(list) != 2 {
t.Errorf("Expected len 2, got %d", len(list))
}
if (list[0] != "hi" && list[0] != "yo") || list[0] == list[1] {
t.Errorf("Expected list[0] to be %q or %q, got %q", "yo", "hi", list[0])
}
if (list[1] != "yo" && list[1] != "hi") || list[1] == list[0] {
t.Errorf("Expected list[1] to be %q or %q, got %q", "hi", "yo", list[1])
}
// Cleanup
s.Del(ctx, "hi")
s.Del(ctx, "yo")
}
func TestRedisStorerGet(t *testing.T) {
if testing.Short() {
t.Skip("skipping long test")
}
storer, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
testidUUID, err := uuid.NewV4()
if err != nil {
t.Error(err)
}
testid1 := testidUUID.String()
val, err := storer.Get(context.Background(), testid1)
if !IsNoSessionError(err) {
t.Errorf("Expected ErrNoSession, got: %v", err)
}
storer.Set(context.Background(), testid1, "banana")
val, err = storer.Get(context.Background(), testid1)
if err != nil {
t.Error(err)
}
if val != "banana" {
t.Errorf("Expected %q, got %q", "banana", val)
}
// Cleanup
storer.Del(context.Background(), testid1)
}
func TestRedisStorerSet(t *testing.T) {
if testing.Short() {
t.Skip("skipping long test")
}
storer, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
testid1 := uuid.Must(uuid.NewV4()).String()
testid2 := uuid.Must(uuid.NewV4()).String()
ctx := context.Background()
storer.Set(ctx, testid1, "hello")
storer.Set(ctx, testid1, "whatsup")
storer.Set(ctx, testid2, "friend")
val, err := storer.Get(ctx, testid1)
if err != nil {
t.Fatal(err)
}
if val != "whatsup" {
t.Errorf("Expected %q, got %q", "whatsup", val)
}
val, err = storer.Get(ctx, testid2)
if err != nil {
t.Error(err)
}
if val != "friend" {
t.Errorf("Expected %q, got %q", "friend", val)
}
// Cleanup
storer.Del(ctx, testid1)
storer.Del(ctx, testid2)
}
func TestRedisStorerDel(t *testing.T) {
if testing.Short() {
t.Skip("skipping long test")
}
storer, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
ctx := context.Background()
storer.Set(ctx, "hi", "hello")
storer.Set(ctx, "hi", "whatsup")
storer.Set(ctx, "yo", "friend")
err = storer.Del(ctx, "hi")
if err != nil {
t.Error(err)
}
_, err = storer.Get(ctx, "hi")
if err == nil {
t.Errorf("Expected get hi to fail")
}
// Cleanup
storer.Del(ctx, "hi")
storer.Del(ctx, "yo")
}
func TestRedisStorerResetExpiry(t *testing.T) {
if testing.Short() {
t.Skip("skipping long test")
}
storer, err := NewDefaultRedisStorer("", "", 13)
if err != nil {
t.Error(err)
}
// Set maxage duration to 1 hour
storer.maxAge = time.Hour * 1
ctx := context.Background()
err = storer.Set(ctx, "test", "test1")
if err != nil {
t.Error(err)
}
oldDur, err := storer.client.TTL(ctx, "test").Result()
if err != nil {
t.Error(err)
}
// Make sure the duration is roughly 1 hour and no greater
if oldDur > time.Hour*1 || oldDur < time.Minute*59 {
t.Errorf("expected TTL in Redis to be set to 1 hour, but got: %v", oldDur.String())
}
// Adjust the duration to something else so we can ensure it was reset
storer.maxAge = time.Hour * 24
err = storer.ResetExpiry(ctx, "test")
if err != nil {
t.Error(err)
}
newDur, err := storer.client.TTL(ctx, "test").Result()
if err != nil {
t.Error(err)
}
// Make sure the new reset duration is roughly 1 day and no less
if newDur > time.Hour*24 || newDur < time.Hour*23 {
t.Errorf("expected TTL in Redis to be set to 24 hours, but got: %v", newDur.String())
}
// Cleanup
storer.Del(ctx, "test")
}