-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_test.go
435 lines (384 loc) · 9.89 KB
/
client_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package redis
import (
"errors"
"fmt"
"io"
"log"
"math/rand"
"net"
"os"
"runtime"
"strings"
"testing"
"time"
)
var testClient, benchClient *Client[string, string]
func init() {
addr, ok := os.LookupEnv("TEST_REDIS_ADDR")
if !ok {
log.Fatal("Need TEST_REDIS_ADDR evironment variable with an address of a test server.\nCAUTION! Tests insert, modify and delete data.")
}
config := ClientConfig{Addr: addr}
if s, ok := os.LookupEnv("TEST_REDIS_PASSWORD"); ok {
config.Password = []byte(s)
}
benchClient = NewClient[string, string](config)
config.CommandTimeout = time.Second
testClient = NewClient[string, string](config)
// make random keys vary
rand.Seed(time.Now().UnixNano())
}
func byteValueClient(t testing.TB) *Client[string, []byte] {
c := NewClient[string, []byte](testClient.ClientConfig)
t.Cleanup(func() {
err := c.Close()
if err != nil {
t.Error("close error:", err)
}
})
return c
}
func randomKey(prefix string) string {
return fmt.Sprintf("%s-%d", prefix, rand.Uint64())
}
func TestClose(t *testing.T) {
t.Parallel()
c := NewDefaultClient[string, string](testClient.Addr)
if err := c.Close(); err != nil {
t.Fatal("close got error:", err)
}
if _, err := c.GET("arbitrary"); err != ErrClosed {
t.Errorf("command got error %q, want %q", err, ErrClosed)
}
if err := c.Close(); err != nil {
t.Fatal("second close got error:", err)
}
}
func TestCloseBussy(t *testing.T) {
t.Parallel()
c := NewClient[string, string](testClient.ClientConfig)
key := randomKey("counter")
timeout := time.NewTimer(time.Second)
// launch command loops
exit := make(chan error, runtime.GOMAXPROCS(0))
for routines := cap(exit); routines > 0; routines-- {
go func() {
for {
_, err := c.INCR(key)
if err != nil {
exit <- err
return
}
}
}()
}
// await full I/O activity
time.Sleep(2 * time.Millisecond)
t.Log(len(c.readQueue), "pending commands")
if err := c.Close(); err != nil {
t.Fatal("close got error:", err)
}
for i := 0; i < cap(exit); i++ {
select {
case <-timeout.C:
t.Fatalf("%d out of %d command routines stopped before timeout", i, cap(exit))
case err := <-exit:
if err != ErrClosed {
t.Errorf("got exit error %q, want %q", err, ErrClosed)
}
}
}
}
func TestUnavailable(t *testing.T) {
t.Parallel()
config := testClient.ClientConfig
config.Addr = "doesnotexist.example.com:70"
config.DialTimeout = 100 * time.Millisecond
c := NewClient[string, string](config)
defer func() {
if err := c.Close(); err != nil {
t.Error("close got error:", err)
}
if _, err := c.GET("arbitrary"); err != ErrClosed {
t.Errorf("command after close got error %q, want %q", err, ErrClosed)
}
}()
_, err := c.GET("arbitrary")
if e := new(net.OpError); !errors.As(err, &e) {
t.Errorf("got error %v, want a net.OpError", err)
} else if e.Op != "dial" {
t.Errorf(`got error for opperation %q, want "dial"`, e.Op)
}
// let the Client retry…
time.Sleep(2 * config.DialTimeout)
_, err = c.GET("arbitrary")
if e := new(net.OpError); !errors.As(err, &e) {
t.Errorf("after connect retry, got error %v, want a net.OpError", err)
} else if e.Op != "dial" {
t.Errorf(`after connect retry, got error for opperation %q, want "dial"`, e.Op)
}
}
// Note that testClient must recover for the next test to pass.
func TestWriteError(t *testing.T) {
timeout := time.After(time.Second)
select {
case conn := <-testClient.connSem:
if conn.Conn != nil {
conn.Close()
}
select {
case testClient.connSem <- conn:
break
case <-timeout:
t.Fatal("connection sempahore release timeout")
}
if conn.Conn == nil {
t.Fatal("no connection")
}
case <-timeout:
t.Fatal("connection sempahore acquire timeout")
}
_, err := testClient.DEL("key")
var e *net.OpError
if !errors.As(err, &e) {
t.Fatalf("got error %v, want a net.OpError", err)
}
if e.Op != "write" {
t.Errorf(`got error for opperation %q, want "write"`, e.Op)
}
}
// Note that testClient must recover for the next test to pass.
func TestReadError(t *testing.T) {
timeout := time.After(time.Second)
// break connection
select {
case conn := <-testClient.connSem:
if conn.Conn != nil {
conn.Conn.Close()
}
// replace with closed pipe
c, _ := net.Pipe()
c.Close()
conn.Conn = c
select {
case testClient.connSem <- conn:
break // write unlocked
case <-timeout:
t.Fatal("connection sempahore release timeout")
}
case <-timeout:
t.Fatal("connection sempahore acquire timeout")
}
_, err := testClient.DEL("key")
if !errors.Is(err, io.ErrClosedPipe) {
t.Errorf("DEL got error %q, want %q", err, io.ErrClosedPipe)
}
}
func TestRedisError(t *testing.T) {
// server errors may not interfear with other commands
t.Parallel()
key, value := randomKey("test"), "abc"
newLen, err := testClient.APPEND(key, value)
if err != nil {
t.Fatalf("APPEND %q %q error: %s", key, value, err)
}
if newLen != int64(len(value)) {
t.Errorf("APPEND %q %q got length %d, want %d", key, value, newLen, len(value))
}
_, err = testClient.DELArgs()
switch e := err.(type) {
default:
t.Errorf("DEL without arguments got error %v, want a RedisError", err)
case ServerError:
t.Log("DEL without arguments got error:", e)
if got := e.Prefix(); got != "ERR" {
t.Errorf(`error %q got prefix %q, want "ERR"`, err, got)
}
}
_, err = testClient.LINDEX(key, 42)
switch e := err.(type) {
default:
t.Errorf("LINDEX %q got error %v, want a RedisError", key, err)
case ServerError:
t.Log("LINDEX on string got error:", e)
if got := e.Prefix(); got != "WRONGTYPE" {
t.Errorf(`LINDEX %q error %q got prefix %q, want "WRONGTYPE"`, key, err, got)
}
}
_, err = testClient.LRANGE(key, 42, 99)
switch e := err.(type) {
default:
t.Errorf("LRANGE %q got error %v, want a RedisError", key, err)
case ServerError:
t.Log("LRANGE on string got error:", e)
if got := e.Prefix(); got != "WRONGTYPE" {
t.Errorf(`LRANGE %q error %q got prefix %q, want "WRONGTYPE"`, key, err, got)
}
}
}
func BenchmarkSimpleString(b *testing.B) {
key := randomKey("bench")
defer func() {
if _, err := benchClient.DEL(key); err != nil {
b.Error("cleanup error:", err)
}
}()
value := "01234567"
b.Run("sequential", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := benchClient.SET(key, value); err != nil {
b.Fatal("error:", err)
}
}
})
b.Run("parallel", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := benchClient.SET(key, value); err != nil {
b.Fatal("error:", err)
}
}
})
})
}
func BenchmarkInteger(b *testing.B) {
key := randomKey("bench")
defer func() {
if _, err := benchClient.DEL(key); err != nil {
b.Error("cleanup error:", err)
}
}()
b.Run("sequential", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := benchClient.DEL(key); err != nil {
b.Fatal("error:", err)
}
}
})
b.Run("parallel", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := benchClient.DEL(key); err != nil {
b.Fatal("error:", err)
}
}
})
})
}
func BenchmarkBulk(b *testing.B) {
key := randomKey("bench")
defer func() {
if _, err := benchClient.DEL(key); err != nil {
b.Error("cleanup error:", err)
}
}()
for _, size := range []int{8, 800, 8000} {
b.Run(fmt.Sprintf("%dB", size), func(b *testing.B) {
if err := benchClient.SET(key, strings.Repeat("B", size)); err != nil {
b.Fatal("population error:", err)
}
b.Run("sequential", func(b *testing.B) {
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
v, err := benchClient.GET(key)
if err != nil {
b.Fatal("error:", err)
}
if len(v) != size {
b.Fatalf("got %d bytes, want %d", len(v), size)
}
}
})
b.Run("parallel", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
b.SetBytes(int64(size))
for pb.Next() {
v, err := benchClient.GET(key)
if err != nil {
b.Fatal("error:", err)
}
if len(v) != size {
b.Fatalf("got %d bytes, want %d", len(v), size)
}
}
})
})
})
}
}
func BenchmarkArray(b *testing.B) {
key := randomKey("bench")
defer func() {
if _, err := benchClient.DEL(key); err != nil {
b.Error("cleanup error:", err)
}
}()
for _, size := range []int64{1, 12, 144} {
b.Run(fmt.Sprintf("%d×8B", size), func(b *testing.B) {
for {
n, err := benchClient.RPUSH(key, strings.Repeat("B", 8))
if err != nil {
b.Fatal("population error:", err)
}
if n >= size {
break
}
}
b.Run("sequential", func(b *testing.B) {
b.SetBytes(size * 8)
for i := 0; i < b.N; i++ {
values, err := benchClient.LRANGE(key, 0, size-1)
if err != nil {
b.Fatal("error:", err)
}
if int64(len(values)) != size {
b.Fatalf("got %d values, want %d", len(values), size)
}
}
})
b.Run("parallel", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
b.SetBytes(int64(size) * 8)
for pb.Next() {
values, err := benchClient.LRANGE(key, 0, size-1)
if err != nil {
b.Fatal("error:", err)
}
if int64(len(values)) != size {
b.Fatalf("got %d values, want %d", len(values), size)
}
}
})
})
})
}
}
func TestNoAllocation(t *testing.T) {
// both too large for stack:
key := randomKey(strings.Repeat("k", 10e6))
value := strings.Repeat("v", 10e6)
f := func() {
if _, err := testClient.INCR(key); err != nil {
t.Fatal(err)
}
if err := testClient.SET(key, value); err != nil {
t.Fatal(err)
}
if _, err := testClient.APPEND(key, value); err != nil {
t.Fatal(err)
}
if _, err := testClient.DEL(key); err != nil {
t.Fatal(err)
}
if err := testClient.HMSET(key, []string{key}, []string{value}); err != nil {
t.Fatal(err)
}
if _, err := testClient.DELArgs(key); err != nil {
t.Fatal(err)
}
}
perRun := testing.AllocsPerRun(1, f)
if perRun != 0 {
t.Errorf("did %f memory allocations, want 0", perRun)
}
}