forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aside_test.go
276 lines (262 loc) · 7.33 KB
/
aside_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package rueidisaside
import (
"context"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/redis/rueidis"
)
var addr = []string{"127.0.0.1:6379"}
func makeClient(t *testing.T, addr []string) CacheAsideClient {
client, err := NewClient(ClientOption{
ClientOption: rueidis.ClientOption{InitAddress: addr},
ClientTTL: time.Second,
})
if err != nil {
t.Fatal(err)
}
return client
}
func TestClientErr(t *testing.T) {
if _, err := NewClient(ClientOption{}); err == nil {
t.Error(err)
}
}
func TestWithClientBuilder(t *testing.T) {
var client rueidis.Client
c, err := NewClient(ClientOption{
ClientOption: rueidis.ClientOption{InitAddress: addr},
ClientBuilder: func(option rueidis.ClientOption) (_ rueidis.Client, err error) {
client, err = rueidis.NewClient(option)
return client, err
},
})
if err != nil {
t.Fatal(err)
}
defer c.Close()
if c.Client() != client {
t.Fatal("client mismatched")
}
}
func TestCacheFilled(t *testing.T) {
client := makeClient(t, addr)
defer client.Close()
key := strconv.Itoa(rand.Int())
for i := 0; i < 2; i++ {
val, err := client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
return "1", nil
})
if err != nil || val != "1" {
t.Fatal(err)
}
val, err = client.Get(context.Background(), time.Millisecond*500, key, nil)
if err != nil || val != "1" {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 600)
val, err = client.Get(context.Background(), time.Millisecond*500, key, nil) // should miss
if !rueidis.IsRedisNil(err) {
t.Fatal(err)
}
}
}
func TestCacheDel(t *testing.T) {
client := makeClient(t, addr)
defer client.Close()
key := strconv.Itoa(rand.Int())
for i := 0; i < 2; i++ {
val, err := client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
return "1", nil
})
if err != nil || val != "1" {
t.Fatal(err)
}
val, err = client.Get(context.Background(), time.Millisecond*500, key, nil)
if err != nil || val != "1" {
t.Fatal(err)
}
if err = client.Del(context.Background(), key); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
val, err = client.Get(context.Background(), time.Millisecond*500, key, nil) // should miss
if !rueidis.IsRedisNil(err) {
t.Fatal(err)
}
}
}
func TestClientRefresh(t *testing.T) {
client := makeClient(t, addr).(*Client)
defer client.Close()
key := strconv.Itoa(rand.Int())
_, _ = client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
id, err := client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).ToString()
if err != nil {
t.Error(err)
}
for i := 0; i < 2; i++ {
err = client.client.Do(context.Background(), client.client.B().Get().Key(id).Build()).Error()
if err != nil {
t.Error(err)
}
time.Sleep(client.ttl)
}
return "1", nil
})
}
func TestCloseCleanup(t *testing.T) {
client := makeClient(t, addr).(*Client)
key := strconv.Itoa(rand.Int())
ch := make(chan string, 1)
_, _ = client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
id, err := client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).ToString()
if err != nil {
t.Error(err)
}
err = client.client.Do(context.Background(), client.client.B().Get().Key(id).Build()).Error()
if err != nil {
t.Error(err)
}
ch <- id
return "1", nil
})
client.Close()
client = makeClient(t, addr).(*Client)
defer client.Close()
err := client.client.Do(context.Background(), client.client.B().Get().Key(<-ch).Build()).Error()
if !rueidis.IsRedisNil(err) {
t.Error(err)
}
}
func TestWriteCancel(t *testing.T) {
client := makeClient(t, addr).(*Client)
defer client.Close()
key := strconv.Itoa(rand.Int())
ch := make(chan string, 1)
ctx, cancel := context.WithCancel(context.Background())
val, err := client.Get(ctx, time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
id, err := client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).ToString()
if err != nil {
t.Error(err)
}
cancel()
ch <- id
return "1", nil
})
if val != "1" {
t.Fatal(err)
}
if err != context.Canceled {
t.Fatal(err)
}
err = client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).Error()
if !rueidis.IsRedisNil(err) {
t.Error(err)
}
}
func TestTimeout(t *testing.T) {
client := makeClient(t, addr).(*Client)
defer client.Close()
key := strconv.Itoa(rand.Int())
_, err := client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
_, err = client.Get(context.Background(), time.Millisecond*500, key, func(ctx context.Context, key string) (val string, err error) {
return "1", nil
})
if err != context.DeadlineExceeded {
t.Error(err)
}
return "", err
})
if err != context.DeadlineExceeded {
t.Fatal(err)
}
}
func TestDisconnect(t *testing.T) {
client := makeClient(t, addr).(*Client)
defer client.Close()
key := strconv.Itoa(rand.Int())
ch := make(chan string, 2)
val, err := client.Get(context.Background(), time.Second*5, key, func(ctx context.Context, key string) (val string, err error) {
id1, err := client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).ToString()
if err != nil {
t.Error(err)
}
go func() {
val, err := client.Get(context.Background(), time.Second*5, key, func(ctx context.Context, key string) (val string, err error) {
id2, err := client.client.Do(context.Background(), client.client.B().Get().Key(key).Build()).ToString()
if err != nil {
t.Error(err)
}
ch <- id2
return "2", nil
})
if val != "2" {
t.Error(err)
}
}()
client.onInvalidation(nil) // simulate disconnection
id2 := <-ch
if id1 == id2 {
t.Error("id not changed")
}
ch <- id1
ch <- id2
return "1", nil
})
if val != "1" {
t.Fatal(err)
}
val, err = client.Get(context.Background(), time.Millisecond*500, key, nil)
if val != "2" {
t.Error(err)
}
err = client.client.Do(context.Background(), client.client.B().Get().Key(<-ch).Build()).Error() // id1
if !rueidis.IsRedisNil(err) {
t.Error(err)
}
err = client.client.Do(context.Background(), client.client.B().Get().Key(<-ch).Build()).Error() // id2
if err != nil {
t.Error(err)
}
time.Sleep(client.ttl) // wait old refresh goroutine exit
}
func TestMultipleClient(t *testing.T) {
clients := make([]CacheAsideClient, 10)
for i := 0; i < len(clients); i++ {
clients[i] = makeClient(t, addr)
}
defer func() {
for _, client := range clients {
client.Close()
}
}()
cnt := 1000
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(len(clients))
key := strconv.Itoa(rand.Int())
sum := int64(0)
for i, c := range clients {
go func(i int, c CacheAsideClient) {
defer wg.Done()
for j := 0; j < cnt; j++ {
v, err := c.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (val string, err error) {
atomic.AddInt64(&sum, 1)
return "1", nil
})
if err != nil || v != "1" {
t.Error(err)
}
}
}(i, c)
}
wg.Wait()
if atomic.LoadInt64(&sum) != 1 {
t.Fatalf("unexpected sum")
}
}
}