-
Notifications
You must be signed in to change notification settings - Fork 0
/
throughcache_test.go
209 lines (186 loc) · 4.35 KB
/
throughcache_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
package cache
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCacheable(t *testing.T) {
type testDefinition struct {
name string
key string
initializer func(c *Cache) error
fn func(ctx context.Context) (int, error)
expected int
shouldErr bool
}
tests := []testDefinition{
{
name: "Cache Miss",
key: "user:43234:grade",
initializer: func(c *Cache) error {
return nil
},
fn: func(ctx context.Context) (int, error) {
// simulate really slow computation or IO operation
time.Sleep(2 * time.Second)
return 42, nil
},
expected: 42,
shouldErr: false,
},
{
name: "Cache Hit",
key: "user:123:grade",
initializer: func(c *Cache) error {
err := c.Set(context.Background(), "user:123:grade", 95)
assert.NoError(t, err)
return nil
},
fn: func(ctx context.Context) (int, error) {
// simulate really slow computation or IO operation
time.Sleep(2 * time.Second)
// Initial to ensure the value comes from the cache
return 0, nil
},
expected: 95,
shouldErr: false,
},
{
name: "Cache Miss with Error",
key: "user:123:grade",
initializer: func(c *Cache) error {
return nil
},
fn: func(ctx context.Context) (int, error) {
return 0, assert.AnError
},
expected: 0,
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup()
defer tearDown()
cache := New(client)
err := test.initializer(cache)
assert.NoError(t, err)
res, err := Cacheable(context.Background(),
cache,
test.key,
100*time.Millisecond,
10*time.Minute,
test.fn,
)
if test.shouldErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expected, res)
}
// Wait for async cache updates to complete
time.Sleep(1 * time.Second)
if !test.shouldErr {
// Ensure values are stored in the cache
var target int
err = cache.Get(context.Background(), test.key, &target)
assert.NoError(t, err)
assert.Equal(t, test.expected, target)
}
})
}
}
func TestWrite(t *testing.T) {
type testDefinition struct {
name string
key string
val int
initializer func(c *Cache) error
fn func(ctx context.Context, val int) error
shouldErr bool
expectedCachedValue int
}
tests := []testDefinition{
{
name: "Write Success - Empty Cache",
key: "user:123:grade",
expectedCachedValue: 55,
val: 55,
initializer: func(c *Cache) error {
return nil
},
fn: func(ctx context.Context, val int) error {
return nil
},
shouldErr: false,
},
{
name: "Write Success - Overwrite Cache",
key: "user:123:grade",
val: 95,
initializer: func(c *Cache) error {
assert.NoError(t, c.Set(context.Background(), "user:123:grade", 90))
return nil
},
fn: func(ctx context.Context, val int) error {
return nil
},
shouldErr: false,
expectedCachedValue: 95,
},
{
name: "Write Failure",
key: "user:123:grade",
val: 95,
expectedCachedValue: 0,
initializer: func(c *Cache) error {
return nil
},
fn: func(ctx context.Context, val int) error {
return assert.AnError
},
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup()
defer tearDown()
cache := New(client)
err := test.initializer(cache)
assert.NoError(t, err)
err = Write(
context.Background(),
cache,
test.key,
test.val,
10*time.Minute,
test.fn,
)
if test.shouldErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if !test.shouldErr {
// Ensure values are stored in the cache
var target int
err = cache.Get(context.Background(), test.key, &target)
assert.NoError(t, err)
assert.Equal(t, test.expectedCachedValue, target)
}
})
}
}
func TestDelete(t *testing.T) {
fn := func(ctx context.Context, val int) error {
return nil
}
setup()
defer tearDown()
cache := New(client)
assert.NoError(t, cache.Set(context.Background(), "user:123:grade", 95))
err := Delete(context.Background(), cache, "user:123:grade", 44, fn)
assert.NoError(t, err)
}