-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_test.go
7300 lines (6162 loc) · 224 KB
/
commands_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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package redis_test
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/redis/go-redis/v9"
"github.com/redis/go-redis/v9/internal/proto"
)
type TimeValue struct {
time.Time
}
func (t *TimeValue) ScanRedis(s string) (err error) {
t.Time, err = time.Parse(time.RFC3339Nano, s)
return
}
var _ = Describe("Commands", func() {
ctx := context.TODO()
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
Describe("server", func() {
It("should Auth", func() {
cmds, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Auth(ctx, "password")
pipe.Auth(ctx, "")
return nil
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("ERR AUTH"))
Expect(cmds[0].Err().Error()).To(ContainSubstring("ERR AUTH"))
Expect(cmds[1].Err().Error()).To(ContainSubstring("ERR AUTH"))
stats := client.PoolStats()
Expect(stats.Hits).To(Equal(uint32(1)))
Expect(stats.Misses).To(Equal(uint32(1)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
Expect(stats.TotalConns).To(Equal(uint32(1)))
Expect(stats.IdleConns).To(Equal(uint32(1)))
})
It("should hello", func() {
cmds, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Hello(ctx, 3, "", "", "")
return nil
})
Expect(err).NotTo(HaveOccurred())
m, err := cmds[0].(*redis.MapStringInterfaceCmd).Result()
Expect(err).NotTo(HaveOccurred())
Expect(m["proto"]).To(Equal(int64(3)))
})
It("should Echo", func() {
pipe := client.Pipeline()
echo := pipe.Echo(ctx, "hello")
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(echo.Err()).NotTo(HaveOccurred())
Expect(echo.Val()).To(Equal("hello"))
})
It("should Ping", func() {
ping := client.Ping(ctx)
Expect(ping.Err()).NotTo(HaveOccurred())
Expect(ping.Val()).To(Equal("PONG"))
})
It("should Wait", func() {
const wait = 3 * time.Second
// assume testing on single redis instance
start := time.Now()
val, err := client.Wait(ctx, 1, wait).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(int64(0)))
Expect(time.Now()).To(BeTemporally("~", start.Add(wait), 3*time.Second))
})
It("should WaitAOF", func() {
const waitAOF = 3 * time.Second
Skip("flaky test")
// assuming that the redis instance doesn't have AOF enabled
start := time.Now()
val, err := client.WaitAOF(ctx, 1, 1, waitAOF).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).NotTo(ContainSubstring("ERR WAITAOF cannot be used when numlocal is set but appendonly is disabled"))
Expect(time.Now()).To(BeTemporally("~", start.Add(waitAOF), 3*time.Second))
})
It("should Select", Label("NonRedisEnterprise"), func() {
pipe := client.Pipeline()
sel := pipe.Select(ctx, 1)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(sel.Err()).NotTo(HaveOccurred())
Expect(sel.Val()).To(Equal("OK"))
})
It("should SwapDB", Label("NonRedisEnterprise"), func() {
pipe := client.Pipeline()
sel := pipe.SwapDB(ctx, 1, 2)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(sel.Err()).NotTo(HaveOccurred())
Expect(sel.Val()).To(Equal("OK"))
})
It("should BgRewriteAOF", func() {
Skip("flaky test")
val, err := client.BgRewriteAOF(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(ContainSubstring("Background append only file rewriting"))
})
It("should BgSave", func() {
Skip("flaky test")
// workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress"
Eventually(func() string {
return client.BgSave(ctx).Val()
}, "30s").Should(Equal("Background saving started"))
})
It("Should CommandGetKeys", func() {
keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"a", "c", "e"}))
keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(Equal([]string{"mylist", "outlist"}))
_, err = client.CommandGetKeys(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("ERR Invalid command specified"))
})
It("should CommandGetKeysAndFlags", func() {
keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keysAndFlags).To(Equal([]redis.KeyFlags{
{
Key: "mylist1",
Flags: []string{"RW", "access", "delete"},
},
{
Key: "mylist2",
Flags: []string{"RW", "insert"},
},
}))
_, err = client.CommandGetKeysAndFlags(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("ERR Invalid command specified"))
})
It("should ClientKill", func() {
r := client.ClientKill(ctx, "1.1.1.1:1111")
Expect(r.Err()).To(MatchError("ERR No such client"))
Expect(r.Val()).To(Equal(""))
})
It("should ClientKillByFilter", func() {
r := client.ClientKillByFilter(ctx, "TYPE", "test")
Expect(r.Err()).To(MatchError("ERR Unknown client type 'test'"))
Expect(r.Val()).To(Equal(int64(0)))
})
It("should ClientKillByFilter with MAXAGE", Label("NonRedisEnterprise"), func() {
var s []string
started := make(chan bool)
done := make(chan bool)
go func() {
defer GinkgoRecover()
started <- true
blpop := client.BLPop(ctx, 0, "list")
Expect(blpop.Val()).To(Equal(s))
done <- true
}()
<-started
select {
case <-done:
Fail("BLPOP is not blocked.")
case <-time.After(2 * time.Second):
// ok
}
killed := client.ClientKillByFilter(ctx, "MAXAGE", "1")
Expect(killed.Err()).NotTo(HaveOccurred())
Expect(killed.Val()).To(SatisfyAny(Equal(int64(2)), Equal(int64(3))))
select {
case <-done:
// ok
case <-time.After(time.Second):
Fail("BLPOP is still blocked.")
}
})
It("should ClientID", func() {
err := client.ClientID(ctx).Err()
Expect(err).NotTo(HaveOccurred())
Expect(client.ClientID(ctx).Val()).To(BeNumerically(">=", 0))
})
It("should ClientUnblock", func() {
id := client.ClientID(ctx).Val()
r, err := client.ClientUnblock(ctx, id).Result()
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(int64(0)))
})
It("should ClientUnblockWithError", func() {
id := client.ClientID(ctx).Val()
r, err := client.ClientUnblockWithError(ctx, id).Result()
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(int64(0)))
})
It("should ClientInfo", func() {
info, err := client.ClientInfo(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(info).NotTo(BeNil())
})
It("should ClientPause", Label("NonRedisEnterprise"), func() {
err := client.ClientPause(ctx, time.Second).Err()
Expect(err).NotTo(HaveOccurred())
start := time.Now()
err = client.Ping(ctx).Err()
Expect(err).NotTo(HaveOccurred())
Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond))
})
It("should ClientSetName and ClientGetName", func() {
pipe := client.Pipeline()
set := pipe.ClientSetName(ctx, "theclientname")
get := pipe.ClientGetName(ctx)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(BeTrue())
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("theclientname"))
})
It("should ClientSetInfo", func() {
pipe := client.Pipeline()
// Test setting the libName
libName := "go-redis"
libInfo := redis.WithLibraryName(libName)
setInfo := pipe.ClientSetInfo(ctx, libInfo)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(setInfo.Err()).NotTo(HaveOccurred())
Expect(setInfo.Val()).To(Equal("OK"))
// Test setting the libVer
libVer := "vX.x"
libInfo = redis.WithLibraryVersion(libVer)
setInfo = pipe.ClientSetInfo(ctx, libInfo)
_, err = pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(setInfo.Err()).NotTo(HaveOccurred())
Expect(setInfo.Val()).To(Equal("OK"))
// Test setting both fields, expect a panic
libInfo = redis.LibraryInfo{LibName: &libName, LibVer: &libVer}
Expect(func() {
defer func() {
if r := recover(); r != nil {
err := r.(error)
Expect(err).To(MatchError("both LibName and LibVer cannot be set at the same time"))
}
}()
pipe.ClientSetInfo(ctx, libInfo)
}).To(Panic())
// Test setting neither field, expect a panic
libInfo = redis.LibraryInfo{}
Expect(func() {
defer func() {
if r := recover(); r != nil {
err := r.(error)
Expect(err).To(MatchError("at least one of LibName and LibVer should be set"))
}
}()
pipe.ClientSetInfo(ctx, libInfo)
}).To(Panic())
// Test setting the default options for libName, libName suffix and libVer
clientInfo := client.ClientInfo(ctx).Val()
Expect(clientInfo.LibName).To(ContainSubstring("go-redis(go-redis,"))
// Test setting the libName suffix in options
opt := redisOptions()
opt.IdentitySuffix = "suffix"
client2 := redis.NewClient(opt)
defer client2.Close()
clientInfo = client2.ClientInfo(ctx).Val()
Expect(clientInfo.LibName).To(ContainSubstring("go-redis(suffix,"))
})
It("should ConfigGet", func() {
val, err := client.ConfigGet(ctx, "*").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).NotTo(BeEmpty())
})
It("should ConfigResetStat", Label("NonRedisEnterprise"), func() {
r := client.ConfigResetStat(ctx)
Expect(r.Err()).NotTo(HaveOccurred())
Expect(r.Val()).To(Equal("OK"))
})
It("should ConfigSet", Label("NonRedisEnterprise"), func() {
configGet := client.ConfigGet(ctx, "maxmemory")
Expect(configGet.Err()).NotTo(HaveOccurred())
Expect(configGet.Val()).To(HaveLen(1))
_, ok := configGet.Val()["maxmemory"]
Expect(ok).To(BeTrue())
configSet := client.ConfigSet(ctx, "maxmemory", configGet.Val()["maxmemory"])
Expect(configSet.Err()).NotTo(HaveOccurred())
Expect(configSet.Val()).To(Equal("OK"))
})
It("should ConfigRewrite", Label("NonRedisEnterprise"), func() {
configRewrite := client.ConfigRewrite(ctx)
Expect(configRewrite.Err()).NotTo(HaveOccurred())
Expect(configRewrite.Val()).To(Equal("OK"))
})
It("should DBSize", func() {
size, err := client.DBSize(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(0)))
})
It("should Info", func() {
info := client.Info(ctx)
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))
})
It("should InfoMap", Label("redis.info"), func() {
info := client.InfoMap(ctx)
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(BeNil())
info = client.InfoMap(ctx, "dummy")
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).To(BeNil())
info = client.InfoMap(ctx, "server")
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).To(HaveLen(1))
})
It("should Info cpu", func() {
info := client.Info(ctx, "cpu")
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))
Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
})
It("should Info cpu and memory", func() {
info := client.Info(ctx, "cpu", "memory")
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))
Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
Expect(info.Val()).To(ContainSubstring(`memory`))
})
It("should LastSave", Label("NonRedisEnterprise"), func() {
lastSave := client.LastSave(ctx)
Expect(lastSave.Err()).NotTo(HaveOccurred())
Expect(lastSave.Val()).NotTo(Equal(0))
})
It("should Save", Label("NonRedisEnterprise"), func() {
// workaround for "ERR Background save already in progress"
Eventually(func() string {
return client.Save(ctx).Val()
}, "10s").Should(Equal("OK"))
})
It("should SlaveOf", Label("NonRedisEnterprise"), func() {
slaveOf := client.SlaveOf(ctx, "localhost", "8888")
Expect(slaveOf.Err()).NotTo(HaveOccurred())
Expect(slaveOf.Val()).To(Equal("OK"))
slaveOf = client.SlaveOf(ctx, "NO", "ONE")
Expect(slaveOf.Err()).NotTo(HaveOccurred())
Expect(slaveOf.Val()).To(Equal("OK"))
})
It("should Time", func() {
tm, err := client.Time(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
})
It("should Command", Label("NonRedisEnterprise"), func() {
cmds, err := client.Command(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(cmds)).To(BeNumerically("~", 240, 25))
cmd := cmds["mget"]
Expect(cmd.Name).To(Equal("mget"))
Expect(cmd.Arity).To(Equal(int8(-2)))
Expect(cmd.Flags).To(ContainElement("readonly"))
Expect(cmd.FirstKeyPos).To(Equal(int8(1)))
Expect(cmd.LastKeyPos).To(Equal(int8(-1)))
Expect(cmd.StepCount).To(Equal(int8(1)))
cmd = cmds["ping"]
Expect(cmd.Name).To(Equal("ping"))
Expect(cmd.Arity).To(Equal(int8(-1)))
Expect(cmd.Flags).To(ContainElement("fast"))
Expect(cmd.FirstKeyPos).To(Equal(int8(0)))
Expect(cmd.LastKeyPos).To(Equal(int8(0)))
Expect(cmd.StepCount).To(Equal(int8(0)))
})
It("should return all command names", func() {
cmdList := client.CommandList(ctx, nil)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()
Expect(cmdNames).NotTo(BeEmpty())
// Assert that some expected commands are present in the list
Expect(cmdNames).To(ContainElement("get"))
Expect(cmdNames).To(ContainElement("set"))
Expect(cmdNames).To(ContainElement("hset"))
})
It("should filter commands by module", func() {
filter := &redis.FilterBy{
Module: "JSON",
}
cmdList := client.CommandList(ctx, filter)
Expect(cmdList.Err()).NotTo(HaveOccurred())
Expect(cmdList.Val()).To(HaveLen(0))
})
It("should filter commands by ACL category", func() {
filter := &redis.FilterBy{
ACLCat: "admin",
}
cmdList := client.CommandList(ctx, filter)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()
// Assert that the returned list only contains commands from the admin ACL category
Expect(len(cmdNames)).To(BeNumerically(">", 10))
})
It("should filter commands by pattern", func() {
filter := &redis.FilterBy{
Pattern: "*GET*",
}
cmdList := client.CommandList(ctx, filter)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()
// Assert that the returned list only contains commands that match the given pattern
Expect(cmdNames).To(ContainElement("get"))
Expect(cmdNames).To(ContainElement("getbit"))
Expect(cmdNames).To(ContainElement("getrange"))
Expect(cmdNames).NotTo(ContainElement("set"))
})
})
Describe("debugging", func() {
PIt("should DebugObject", func() {
err := client.DebugObject(ctx, "foo").Err()
Expect(err).To(MatchError("ERR no such key"))
err = client.Set(ctx, "foo", "bar", 0).Err()
Expect(err).NotTo(HaveOccurred())
s, err := client.DebugObject(ctx, "foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(ContainSubstring("serializedlength:4"))
})
It("should MemoryUsage", func() {
err := client.MemoryUsage(ctx, "foo").Err()
Expect(err).To(Equal(redis.Nil))
err = client.Set(ctx, "foo", "bar", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.MemoryUsage(ctx, "foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).NotTo(BeZero())
n, err = client.MemoryUsage(ctx, "foo", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).NotTo(BeZero())
})
})
Describe("keys", func() {
It("should Del", func() {
err := client.Set(ctx, "key1", "Hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
err = client.Set(ctx, "key2", "World", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.Del(ctx, "key1", "key2", "key3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Unlink", func() {
err := client.Set(ctx, "key1", "Hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
err = client.Set(ctx, "key2", "World", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.Unlink(ctx, "key1", "key2", "key3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Dump", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
Expect(dump.Val()).NotTo(BeEmpty())
})
It("should Exists", func() {
set := client.Set(ctx, "key1", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.Exists(ctx, "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
n, err = client.Exists(ctx, "key1", "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.Exists(ctx, "key1", "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Expire", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireAt", func() {
setCmd := client.Set(ctx, "key", "Hello", 0)
Expect(setCmd.Err()).NotTo(HaveOccurred())
Expect(setCmd.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
// Check correct expiration time is set in the future
expireAt := time.Now().Add(time.Minute)
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
// Check correct expiration in the past
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should Keys", func() {
mset := client.MSet(ctx, "one", "1", "two", "2", "three", "3", "four", "4")
Expect(mset.Err()).NotTo(HaveOccurred())
Expect(mset.Val()).To(Equal("OK"))
keys := client.Keys(ctx, "*o*")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"}))
keys = client.Keys(ctx, "t??")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(Equal([]string{"two"}))
keys = client.Keys(ctx, "*")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"}))
})
It("should Migrate", Label("NonRedisEnterprise"), func() {
migrate := client.Migrate(ctx, "localhost", redisSecondaryPort, "key", 0, 0)
Expect(migrate.Err()).NotTo(HaveOccurred())
Expect(migrate.Val()).To(Equal("NOKEY"))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
migrate = client.Migrate(ctx, "localhost", redisSecondaryPort, "key", 0, 0)
Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance"))
Expect(migrate.Val()).To(Equal(""))
})
It("should Move", Label("NonRedisEnterprise"), func() {
move := client.Move(ctx, "key", 2)
Expect(move.Err()).NotTo(HaveOccurred())
Expect(move.Val()).To(Equal(false))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
move = client.Move(ctx, "key", 2)
Expect(move.Err()).NotTo(HaveOccurred())
Expect(move.Val()).To(Equal(true))
get := client.Get(ctx, "key")
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
pipe := client.Pipeline()
pipe.Select(ctx, 2)
get = pipe.Get(ctx, "key")
pipe.FlushDB(ctx)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should Object", Label("NonRedisEnterprise"), func() {
start := time.Now()
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
refCount := client.ObjectRefCount(ctx, "key")
Expect(refCount.Err()).NotTo(HaveOccurred())
Expect(refCount.Val()).To(Equal(int64(1)))
client.ConfigSet(ctx, "maxmemory-policy", "volatile-lfu")
freq := client.ObjectFreq(ctx, "key")
Expect(freq.Err()).NotTo(HaveOccurred())
client.ConfigSet(ctx, "maxmemory-policy", "noeviction") // default
err := client.ObjectEncoding(ctx, "key").Err()
Expect(err).NotTo(HaveOccurred())
idleTime := client.ObjectIdleTime(ctx, "key")
Expect(idleTime.Err()).NotTo(HaveOccurred())
// Redis returned milliseconds/1000, which may cause ObjectIdleTime to be at a critical value,
// should be +1s to deal with the critical value problem.
// if too much time (>1s) is used during command execution, it may also cause the test to fail.
// so the ObjectIdleTime result should be <=now-start+1s
// link: https://github.com/redis/redis/blob/5b48d900498c85bbf4772c1d466c214439888115/src/object.c#L1265-L1272
Expect(idleTime.Val()).To(BeNumerically("<=", time.Since(start)+time.Second))
})
It("should Persist", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
persist := client.Persist(ctx, "key")
Expect(persist.Err()).NotTo(HaveOccurred())
Expect(persist.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val() < 0).To(Equal(true))
})
It("should PExpire", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpire := client.PExpire(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireAt", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpireat := client.PExpireAt(ctx, "key", time.Now().Add(expiration))
Expect(pexpireat.Err()).NotTo(HaveOccurred())
Expect(pexpireat.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireTime", func() {
// The command returns -1 if the key exists but has no associated expiration time.
// The command returns -2 if the key does not exist.
pExpireTime := client.PExpireTime(ctx, "key")
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
Expect(pExpireTime.Val() < 0).To(Equal(true))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
timestamp := time.Now().Add(time.Minute)
expireAt := client.PExpireAt(ctx, "key", timestamp)
Expect(expireAt.Err()).NotTo(HaveOccurred())
Expect(expireAt.Val()).To(Equal(true))
pExpireTime = client.PExpireTime(ctx, "key")
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
Expect(pExpireTime.Val().Milliseconds()).To(BeNumerically("==", timestamp.UnixMilli()))
})
It("should PTTL", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := time.Second
expire := client.Expire(ctx, "key", expiration)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should RandomKey", func() {
randomKey := client.RandomKey(ctx)
Expect(randomKey.Err()).To(Equal(redis.Nil))
Expect(randomKey.Val()).To(Equal(""))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
randomKey = client.RandomKey(ctx)
Expect(randomKey.Err()).NotTo(HaveOccurred())
Expect(randomKey.Val()).To(Equal("key"))
})
It("should Rename", Label("NonRedisEnterprise"), func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
status := client.Rename(ctx, "key", "key1")
Expect(status.Err()).NotTo(HaveOccurred())
Expect(status.Val()).To(Equal("OK"))
get := client.Get(ctx, "key1")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should RenameNX", Label("NonRedisEnterprise"), func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
renameNX := client.RenameNX(ctx, "key", "key1")
Expect(renameNX.Err()).NotTo(HaveOccurred())
Expect(renameNX.Val()).To(Equal(true))
get := client.Get(ctx, "key1")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should Restore", func() {
err := client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
err = client.Del(ctx, "key").Err()
Expect(err).NotTo(HaveOccurred())
restore, err := client.Restore(ctx, "key", 0, dump.Val()).Result()
Expect(err).NotTo(HaveOccurred())
Expect(restore).To(Equal("OK"))
type_, err := client.Type(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(type_).To(Equal("string"))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should RestoreReplace", func() {
err := client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
restore, err := client.RestoreReplace(ctx, "key", 0, dump.Val()).Result()
Expect(err).NotTo(HaveOccurred())
Expect(restore).To(Equal("OK"))
type_, err := client.Type(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(type_).To(Equal("string"))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should Sort RO", func() {
size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.SortRO(ctx, "list", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should Sort", func() {
size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.Sort(ctx, "list", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should Sort and Get", Label("NonRedisEnterprise"), func() {
size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
err = client.Set(ctx, "object_2", "value2", 0).Err()
Expect(err).NotTo(HaveOccurred())
{
els, err := client.Sort(ctx, "list", &redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"", "value2", ""}))
}
{
els, err := client.SortInterfaces(ctx, "list", &redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]interface{}{nil, "value2", nil}))
}
})
It("should Sort and Store", Label("NonRedisEnterprise"), func() {