forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
543 lines (451 loc) · 16.1 KB
/
command_test.go
File metadata and controls
543 lines (451 loc) · 16.1 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package redis_test
import (
"bytes"
"errors"
"strconv"
"time"
"github.com/redis/go-redis/v9"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
)
// parseRESPType returns the RESP type character and validates basic structure
func parseRESPType(data []byte) (byte, bool) {
if len(data) < 3 {
return 0, false
}
// Check for CRLF at the end
if !bytes.HasSuffix(data, []byte("\r\n")) {
return 0, false
}
return data[0], true
}
// countRESPElements counts the number of elements in a RESP array/map response
func countRESPElements(data []byte) (int, bool) {
if len(data) < 4 {
return 0, false
}
// Find first CRLF
idx := bytes.Index(data, []byte("\r\n"))
if idx < 2 {
return 0, false
}
count, err := strconv.Atoi(string(data[1:idx]))
if err != nil {
return 0, false
}
return count, true
}
var _ = Describe("Cmd", func() {
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("implements Stringer", func() {
set := client.Set(ctx, "foo", "bar", 0)
Expect(set.String()).To(Equal("set foo bar: OK"))
get := client.Get(ctx, "foo")
Expect(get.String()).To(Equal("get foo: bar"))
})
It("has val/err", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
})
It("has helpers", func() {
set := client.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
n, err := client.Get(ctx, "key").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(10)))
un, err := client.Get(ctx, "key").Uint64()
Expect(err).NotTo(HaveOccurred())
Expect(un).To(Equal(uint64(10)))
f, err := client.Get(ctx, "key").Float64()
Expect(err).NotTo(HaveOccurred())
Expect(f).To(Equal(float64(10)))
})
It("supports float32", func() {
f := float32(66.97)
err := client.Set(ctx, "float_key", f, 0).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "float_key").Float32()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(f))
})
It("supports time.Time", func() {
tm := time.Date(2019, 1, 1, 9, 45, 10, 222125, time.UTC)
err := client.Set(ctx, "time_key", tm, 0).Err()
Expect(err).NotTo(HaveOccurred())
s, err := client.Get(ctx, "time_key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal("2019-01-01T09:45:10.000222125Z"))
tm2, err := client.Get(ctx, "time_key").Time()
Expect(err).NotTo(HaveOccurred())
Expect(tm2).To(BeTemporally("==", tm))
})
It("allows to set custom error", func() {
e := errors.New("custom error")
cmd := redis.Cmd{}
cmd.SetErr(e)
_, err := cmd.Result()
Expect(err).To(Equal(e))
})
})
var _ = Describe("RawCmd", func() {
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("returns raw RESP bytes for simple string (PING)", func() {
cmd := client.DoRaw(ctx, "PING")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal("+PONG\r\n"))
})
It("returns raw RESP bytes for bulk string (GET)", func() {
err := client.Set(ctx, "rawtest", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "GET", "rawtest")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal("$5\r\nhello\r\n"))
})
It("returns raw RESP bytes for integer (INCR)", func() {
err := client.Set(ctx, "counter", "10", 0).Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "INCR", "counter")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal(":11\r\n"))
})
It("returns raw RESP bytes for array (KEYS) with validation", func() {
err := client.Set(ctx, "key1", "val1", 0).Err()
Expect(err).NotTo(HaveOccurred())
err = client.Set(ctx, "key2", "val2", 0).Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "KEYS", "key*")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
// Validate RESP structure
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue())
Expect(respType).To(Equal(byte('*')), "Should be array type")
// Verify element count
count, ok := countRESPElements(rawBytes)
Expect(ok).To(BeTrue())
Expect(count).To(Equal(2))
// Verify keys are present in the raw bytes
rawStr := string(rawBytes)
Expect(rawStr).To(ContainSubstring("key1"))
Expect(rawStr).To(ContainSubstring("key2"))
})
It("returns raw RESP bytes for HGETALL with complete validation", func() {
err := client.HSet(ctx, "myhash", "field1", "value1", "field2", "value2").Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "HGETALL", "myhash")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
// Validate RESP structure
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue(), "Response should be valid RESP")
// HGETALL returns array (RESP2) or map (RESP3)
Expect(respType).To(BeElementOf(byte('*'), byte('%')), "Should be array or map type")
// Count elements: array has 4 elements (2 key-value pairs), map has 2 pairs
count, ok := countRESPElements(rawBytes)
Expect(ok).To(BeTrue())
if respType == '*' {
Expect(count).To(Equal(4), "Array should have 4 elements (2 key-value pairs)")
} else {
Expect(count).To(Equal(2), "Map should have 2 pairs")
}
rawStr := string(rawBytes)
Expect(rawStr).To(ContainSubstring("field1"))
Expect(rawStr).To(ContainSubstring("value1"))
Expect(rawStr).To(ContainSubstring("field2"))
Expect(rawStr).To(ContainSubstring("value2"))
// Verify response ends with CRLF
Expect(rawBytes[len(rawBytes)-2:]).To(Equal([]byte("\r\n")))
})
It("returns raw RESP bytes for nil value", func() {
cmd := client.DoRaw(ctx, "GET", "nonexistent")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
rawStr := string(rawBytes)
Expect(rawStr == "$-1\r\n" || rawStr == "_\r\n").To(BeTrue())
})
It("returns raw RESP bytes for empty array", func() {
cmd := client.DoRaw(ctx, "KEYS", "nonexistent*")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal("*0\r\n"))
})
It("has Val and Bytes methods", func() {
cmd := client.DoRaw(ctx, "PING")
Expect(cmd.Err()).NotTo(HaveOccurred())
val := cmd.Val()
Expect(string(val)).To(Equal("+PONG\r\n"))
bytes, err := cmd.Bytes()
Expect(err).NotTo(HaveOccurred())
Expect(string(bytes)).To(Equal("+PONG\r\n"))
})
It("implements Stringer", func() {
cmd := client.DoRaw(ctx, "PING")
Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.String()).To(ContainSubstring("PING"))
})
It("allows to set custom error", func() {
e := errors.New("custom error")
cmd := redis.RawCmd{}
cmd.SetErr(e)
_, err := cmd.Result()
Expect(err).To(Equal(e))
})
It("can be cloned", func() {
cmd := client.DoRaw(ctx, "PING")
Expect(cmd.Err()).NotTo(HaveOccurred())
cloned := cmd.Clone().(*redis.RawCmd)
Expect(cloned.Val()).To(Equal(cmd.Val()))
originalVal := cmd.Val()
clonedVal := cloned.Val()
Expect(&originalVal[0]).NotTo(BeIdenticalTo(&clonedVal[0]))
})
It("returns raw RESP bytes for error response", func() {
cmd := client.DoRaw(ctx, "INVALIDCMD")
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(HavePrefix("-"))
Expect(string(rawBytes)).To(ContainSubstring("ERR"))
})
It("returns raw RESP bytes for empty hash (nil map)", func() {
// HGETALL on non-existent hash returns empty array/map
cmd := client.DoRaw(ctx, "HGETALL", "nonexistent_hash")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue())
// Should be empty array or empty map
Expect(respType).To(BeElementOf(byte('*'), byte('%')))
count, ok := countRESPElements(rawBytes)
Expect(ok).To(BeTrue())
Expect(count).To(Equal(0), "Empty hash should return 0 elements")
})
It("returns raw RESP bytes for nested array structures (SMEMBERS)", func() {
// Create a set with multiple members
err := client.SAdd(ctx, "myset", "member1", "member2", "member3").Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "SMEMBERS", "myset")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
// Validate basic RESP structure
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue())
// SMEMBERS returns array (RESP2) or set (RESP3)
Expect(respType).To(BeElementOf(byte('*'), byte('~')))
// Verify count
count, ok := countRESPElements(rawBytes)
Expect(ok).To(BeTrue())
Expect(count).To(Equal(3))
// Verify members are present
rawStr := string(rawBytes)
Expect(rawStr).To(ContainSubstring("member1"))
Expect(rawStr).To(ContainSubstring("member2"))
Expect(rawStr).To(ContainSubstring("member3"))
})
It("returns raw RESP bytes for LRANGE with exact byte validation", func() {
// Create a list with multiple elements
err := client.RPush(ctx, "mylist", "elem1", "elem2", "elem3").Err()
Expect(err).NotTo(HaveOccurred())
cmd := client.DoRaw(ctx, "LRANGE", "mylist", "0", "-1")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
// LRANGE returns elements in order, so we can validate exact bytes
expectedResp := "*3\r\n$5\r\nelem1\r\n$5\r\nelem2\r\n$5\r\nelem3\r\n"
Expect(string(rawBytes)).To(Equal(expectedResp))
})
It("returns raw RESP bytes for LPUSH with exact integer response", func() {
// LPUSH returns the length of the list after push
cmd := client.DoRaw(ctx, "LPUSH", "newlist", "item")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal(":1\r\n"))
})
It("returns raw RESP bytes for ECHO with exact bulk string", func() {
cmd := client.DoRaw(ctx, "ECHO", "testmsg")
Expect(cmd.Err()).NotTo(HaveOccurred())
rawBytes, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(string(rawBytes)).To(Equal("$7\r\ntestmsg\r\n"))
})
})
var _ = Describe("RawWriteToCmd", func() {
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("streams raw RESP bytes for PING", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "PING")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(7)))
Expect(buf.String()).To(Equal("+PONG\r\n"))
})
It("streams raw RESP bytes for bulk string (GET)", func() {
client.Set(ctx, "rawkey", "rawvalue", 0)
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "GET", "rawkey")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(BeNumerically(">", 0))
Expect(buf.String()).To(Equal("$8\r\nrawvalue\r\n"))
})
It("streams raw RESP bytes for integer (INCR)", func() {
client.Set(ctx, "writeto_counter", "10", 0)
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "INCR", "writeto_counter")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(5)))
Expect(buf.String()).To(Equal(":11\r\n"))
})
It("streams raw RESP bytes for nil value (GET non-existent)", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "GET", "nonexistent_key")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(buf.Len())))
rawStr := buf.String()
// RESP2 returns $-1\r\n, RESP3 returns _\r\n
Expect(rawStr == "$-1\r\n" || rawStr == "_\r\n").To(BeTrue())
})
It("streams raw RESP bytes for array (LRANGE) with exact validation", func() {
client.RPush(ctx, "writeto_list", "a", "b", "c")
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "LRANGE", "writeto_list", "0", "-1")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(buf.Len())))
// LRANGE returns elements in order
expectedResp := "*3\r\n$1\r\na\r\n$1\r\nb\r\n$1\r\nc\r\n"
Expect(buf.String()).To(Equal(expectedResp))
})
It("streams raw RESP bytes for empty array (LRANGE empty list)", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "LRANGE", "nonexistent_list", "0", "-1")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(buf.Len())))
Expect(buf.String()).To(Equal("*0\r\n"))
})
It("streams raw RESP bytes for HGETALL", func() {
client.HSet(ctx, "rawhash", "f1", "v1", "f2", "v2")
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "HGETALL", "rawhash")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(BeNumerically(">", 0))
raw := buf.String()
Expect(raw).To(ContainSubstring("f1"))
Expect(raw).To(ContainSubstring("v1"))
})
It("returns correct byte count", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "PING")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(buf.Len())))
})
It("implements Stringer", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "PING")
Expect(cmd.String()).To(ContainSubstring("PING"))
})
It("can be cloned", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "PING")
clone := cmd.Clone().(*redis.RawWriteToCmd)
Expect(clone.Val()).To(Equal(cmd.Val()))
})
It("streams HGETALL with complete validation", func() {
client.HSet(ctx, "rawhash2", "key1", "val1", "key2", "val2")
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "HGETALL", "rawhash2")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
// Verify written bytes match buffer length
Expect(written).To(Equal(int64(buf.Len())))
// Validate RESP structure
rawBytes := buf.Bytes()
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue())
Expect(respType).To(BeElementOf(byte('*'), byte('%')))
// Verify all data present
rawStr := buf.String()
Expect(rawStr).To(ContainSubstring("key1"))
Expect(rawStr).To(ContainSubstring("val1"))
Expect(rawStr).To(ContainSubstring("key2"))
Expect(rawStr).To(ContainSubstring("val2"))
})
It("streams empty hash correctly", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "HGETALL", "nonexistent_hash")
written, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
Expect(written).To(Equal(int64(buf.Len())))
rawBytes := buf.Bytes()
respType, valid := parseRESPType(rawBytes)
Expect(valid).To(BeTrue())
Expect(respType).To(BeElementOf(byte('*'), byte('%')))
count, ok := countRESPElements(rawBytes)
Expect(ok).To(BeTrue())
Expect(count).To(Equal(0))
})
It("streams error response correctly", func() {
var buf bytes.Buffer
cmd := client.DoRawWriteTo(ctx, &buf, "INVALIDCMD")
_, err := cmd.Result()
Expect(err).NotTo(HaveOccurred())
rawStr := buf.String()
Expect(rawStr).To(HavePrefix("-"))
Expect(rawStr).To(ContainSubstring("ERR"))
})
})
var _ = Describe("NoRetry behavior", func() {
It("RawCmd does not have NoRetry set", func() {
cmd := redis.NewRawCmd(ctx, "PING")
Expect(cmd.NoRetry()).To(BeFalse())
})
It("RawWriteToCmd has NoRetry set to prevent data corruption", func() {
var buf bytes.Buffer
cmd := redis.NewRawWriteToCmd(ctx, &buf, "PING")
Expect(cmd.NoRetry()).To(BeTrue())
})
})