2 # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
3 # This softare is released under the BSD License. See the COPYING file for
13 proc test
{name code okpattern
} {
15 if {$::testnum < $::first ||
$::testnum > $::last} return
16 puts -nonewline [format "%-70s " "#$::testnum $name"]
18 set retval
[uplevel 1 $code]
19 if {$okpattern eq
$retval ||
[string match
$okpattern $retval]} {
23 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
28 proc randstring
{min max
{type
binary}} {
29 set len
[expr {$min+int
(rand
()*($max-$min+1))}]
31 if {$type eq
{binary}} {
34 } elseif
{$type eq
{alpha
}} {
37 } elseif
{$type eq
{compr
}} {
42 append output
[format "%c" [expr {$minval+int
(rand
()*($maxval-$minval+1))}]]
48 # Useful for some test
49 proc zlistAlikeSort
{a b
} {
50 if {[lindex $a 0] > [lindex $b 0]} {return 1}
51 if {[lindex $a 0] < [lindex $b 0]} {return -1}
52 string compare
[lindex $a 1] [lindex $b 1]
55 proc waitForBgsave r
{
58 if {[string match
{*bgsave_in_progress
:1*} $i]} {
59 puts -nonewline "\nWaiting for background save to finish... "
68 proc waitForBgrewriteaof r
{
71 if {[string match
{*bgrewriteaof_in_progress
:1*} $i]} {
72 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
81 proc randomInt
{max
} {
82 expr {int
(rand
()*$max)}
86 set path
[expr {int
(rand
()*[llength $args])}]
87 uplevel 1 [lindex $args $path]
92 # Small enough to likely collide
95 # 32 bit compressible signed/unsigned
96 randpath
{randomInt
2000000000} {randomInt
4000000000}
99 randpath
{randomInt
1000000000000}
102 randpath
{randstring
0 256 alpha
} \
103 {randstring
0 256 compr
} \
104 {randstring
0 256 binary}
110 # Small enough to likely collide
113 # 32 bit compressible signed/unsigned
114 randpath
{randomInt
2000000000} {randomInt
4000000000}
117 randpath
{randomInt
1000000000000}
120 randpath
{randstring
1 256 alpha
} \
121 {randstring
1 256 compr
}
125 proc createComplexDataset
{r ops
} {
126 for {set j
0} {$j < $ops} {incr j
} {
130 set d
[expr {rand
()}]
132 set d
[expr {rand
()}]
134 set d
[expr {rand
()}]
136 set d
[expr {rand
()}]
138 set d
[expr {rand
()}]
140 randpath
{set d
+inf
} {set d
-inf}
162 randpath
{$r lpush
$k $v} \
169 randpath
{$r sadd
$k $v} \
173 randpath
{$r zadd
$k $d $v} \
180 proc datasetDigest r
{
181 set keys
[lsort [split [$r keys
*] " "]]
187 set aux
[::sha1::sha1 -hex [$r get
$k]]
189 if {[$r llen
$k] == 0} {
192 set aux
[::sha1::sha1 -hex [$r lrange $k 0 -1]]
195 if {[$r scard
$k] == 0} {
198 set aux
[::sha1::sha1 -hex [lsort [$r smembers
$k]]]
201 if {[$r zcard
$k] == 0} {
204 set aux
[::sha1::sha1 -hex [$r zrange
$k 0 -1]]
207 error "Type not supported"
210 if {$aux eq
{}} continue
211 set digest
[::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
216 proc main
{server port
} {
217 set r
[redis
$server $port]
222 # The following AUTH test should be enabled only when requirepass
223 # <PASSWORD> is set in redis.conf and redis-server was started with
224 # redis.conf as the first argument.
226 #test {AUTH with requirepass in redis.conf} {
230 test
{DEL all keys to start with a clean DB
} {
231 foreach key
[$r keys
*] {$r del
$key}
235 test
{SET and GET an item
} {
240 test
{SET and GET an empty item
} {
245 test
{DEL against a single item
} {
254 list [$r del foo1 foo2 foo3 foo4
] [$r mget foo1 foo2 foo3
]
257 test
{KEYS with pattern
} {
258 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
262 } {foo_a foo_b foo_c
}
264 test
{KEYS to get all keys
} {
266 } {foo_a foo_b foo_c key_x key_y key_z
}
272 test
{DEL all keys
} {
273 foreach key
[$r keys
*] {$r del
$key}
277 test
{Very big payload in GET
/SET
} {
278 set buf
[string repeat
"abcd" 1000000]
281 } [string repeat
"abcd" 1000000]
283 test
{SET
10000 numeric keys and access all them in reverse order
} {
284 for {set x
0} {$x < 10000} {incr x
} {
288 for {set x
9999} {$x >= 0} {incr x
-1} {
294 test
{DBSIZE should be
10001 now
} {
298 test
{INCR against non existing key
} {
300 append res
[$r incr novar
]
301 append res
[$r get novar
]
304 test
{INCR against key created by
incr itself
} {
308 test
{INCR against key originally
set with SET
} {
313 test
{INCR over
32bit value
} {
314 $r set novar
17179869184
318 test
{INCRBY over
32bit value with over
32bit increment
} {
319 $r set novar
17179869184
320 $r incrby novar
17179869184
323 test
{INCR against key with spaces
(no integer encoded
)} {
328 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
329 $r set novar
17179869184
330 $r decrby novar
17179869185
333 test
{SETNX target key missing
} {
334 $r setnx novar2 foobared
338 test
{SETNX target key exists
} {
339 $r setnx novar2 blabla
343 test
{SETNX will overwrite EXPIREing key
} {
353 append res
[$r exists newkey
]
355 append res
[$r exists newkey
]
358 test
{Zero length value in key. SET
/GET
/EXISTS
} {
360 set res
[$r get emptykey
]
361 append res
[$r exists emptykey
]
363 append res
[$r exists emptykey
]
366 test
{Commands pipelining
} {
368 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
371 append res
[string match OK
* [::redis::redis_read_reply $fd]]
372 append res
[::redis::redis_read_reply $fd]
373 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
377 test
{Non existing command
} {
378 catch {$r foobaredcommand
} err
379 string match ERR
* $err
382 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
386 set res
[$r llen mylist
]
387 append res
[$r lindex mylist
0]
388 append res
[$r lindex mylist
1]
389 append res
[$r lindex mylist
2]
390 list $res [$r lindex mylist
100]
398 test
{Create a long
list and check every single element with LINDEX
} {
400 for {set i
0} {$i < 1000} {incr i
} {
403 for {set i
0} {$i < 1000} {incr i
} {
404 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
405 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
412 test
{Test elements with LINDEX in random access
} {
414 for {set i
0} {$i < 1000} {incr i
} {
415 set rint
[expr int
(rand
()*1000)]
416 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
417 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
424 test
{Check
if the
list is still ok
after a DEBUG RELOAD
} {
427 for {set i
0} {$i < 1000} {incr i
} {
428 set rint
[expr int
(rand
()*1000)]
429 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
430 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
437 test
{LLEN against non-list value
error} {
440 catch {$r llen mylist
} err
444 test
{LLEN against non existing key
} {
448 test
{LINDEX against non-list value
error} {
449 catch {$r lindex mylist
0} err
453 test
{LINDEX against non existing key
} {
454 $r lindex not-a-key
10
457 test
{LPUSH against non-list value
error} {
458 catch {$r lpush mylist
0} err
462 test
{RPUSH against non-list value
error} {
463 catch {$r rpush mylist
0} err
467 test
{RPOPLPUSH base case
} {
473 set v1
[$r rpoplpush mylist newlist
]
474 set v2
[$r rpoplpush mylist newlist
]
475 set l1
[$r lrange mylist
0 -1]
476 set l2
[$r lrange newlist
0 -1]
480 test
{RPOPLPUSH with the same
list as src and dst
} {
485 set l1
[$r lrange mylist
0 -1]
486 set v
[$r rpoplpush mylist mylist
]
487 set l2
[$r lrange mylist
0 -1]
489 } {{a b c
} c
{c a b
}}
491 test
{RPOPLPUSH target
list already exists
} {
499 set v1
[$r rpoplpush mylist newlist
]
500 set v2
[$r rpoplpush mylist newlist
]
501 set l1
[$r lrange mylist
0 -1]
502 set l2
[$r lrange newlist
0 -1]
504 } {d c
{a b
} {c d x
}}
506 test
{RPOPLPUSH against non existing key
} {
509 set v1
[$r rpoplpush mylist newlist
]
510 list $v1 [$r exists mylist
] [$r exists newlist
]
513 test
{RPOPLPUSH against non
list src key
} {
517 catch {$r rpoplpush mylist newlist
} err
518 list [$r type mylist
] [$r exists newlist
] [string range
$err 0 2]
521 test
{RPOPLPUSH against non
list dst key
} {
529 catch {$r rpoplpush mylist newlist
} err
530 list [$r lrange mylist
0 -1] [$r type newlist
] [string range
$err 0 2]
531 } {{a b c d
} string ERR
}
533 test
{RPOPLPUSH against non existing src key
} {
536 $r rpoplpush mylist newlist
539 test
{RENAME basic usage
} {
541 $r rename mykey mykey1
542 $r rename mykey1 mykey2
546 test
{RENAME
source key should no longer exist
} {
550 test
{RENAME against already existing key
} {
553 $r rename mykey2 mykey
554 set res
[$r get mykey
]
555 append res
[$r exists mykey2
]
558 test
{RENAMENX basic usage
} {
562 $r renamenx mykey mykey2
563 set res
[$r get mykey2
]
564 append res
[$r exists mykey
]
567 test
{RENAMENX against already existing key
} {
570 $r renamenx mykey mykey2
573 test
{RENAMENX against already existing key
(2)} {
574 set res
[$r get mykey
]
575 append res
[$r get mykey2
]
578 test
{RENAME against non existing
source key
} {
579 catch {$r rename nokey foobar
} err
583 test
{RENAME where
source and dest key is the same
} {
584 catch {$r rename mykey mykey
} err
588 test
{DEL all keys again
(DB
0)} {
589 foreach key
[$r keys
*] {
595 test
{DEL all keys again
(DB
1)} {
597 foreach key
[$r keys
*] {
605 test
{MOVE basic usage
} {
609 lappend res
[$r exists mykey
]
610 lappend res
[$r dbsize
]
612 lappend res
[$r get mykey
]
613 lappend res
[$r dbsize
]
616 } [list 0 0 foobar
1]
618 test
{MOVE against key existing in the target DB
} {
623 test
{SET
/GET keys in different DBs
} {
631 lappend res
[$r get a
]
632 lappend res
[$r get b
]
634 lappend res
[$r get a
]
635 lappend res
[$r get b
]
638 } {hello world foo bared
}
640 test
{Basic LPOP
/RPOP
} {
645 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
648 test
{LPOP
/RPOP against empty
list} {
652 test
{LPOP against non
list value
} {
654 catch {$r lpop notalist
} err
658 test
{Mass LPUSH
/LPOP
} {
660 for {set i
0} {$i < 1000} {incr i
} {
665 for {set i
0} {$i < 500} {incr i
} {
666 incr sum2
[$r lpop mylist
]
667 incr sum2
[$r rpop mylist
]
672 test
{LRANGE basics
} {
673 for {set i
0} {$i < 10} {incr i
} {
676 list [$r lrange mylist
1 -2] \
677 [$r lrange mylist
-3 -1] \
678 [$r lrange mylist
4 4]
679 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
681 test
{LRANGE inverted indexes
} {
685 test
{LRANGE out of range indexes including the full
list} {
686 $r lrange mylist
-1000 1000
687 } {0 1 2 3 4 5 6 7 8 9}
689 test
{LRANGE against non existing key
} {
690 $r lrange nosuchkey
0 1
693 test
{LTRIM basics
} {
695 for {set i
0} {$i < 100} {incr i
} {
699 $r lrange mylist
0 -1
702 test
{LTRIM stress testing
} {
705 for {set i
0} {$i < 20} {incr i
} {
709 for {set j
0} {$j < 100} {incr j
} {
712 for {set i
0} {$i < 20} {incr i
} {
718 $r ltrim mylist
$a $b
719 if {[$r lrange mylist
0 -1] ne
[lrange $mylist $a $b]} {
720 set err
"[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
729 foreach x
{99 98 97 96 95} {
733 $r lset mylist
-1 bar
734 $r lrange mylist
0 -1
737 test
{LSET out of range index
} {
738 catch {$r lset mylist
10 foo
} err
742 test
{LSET against non existing key
} {
743 catch {$r lset nosuchkey
10 foo
} err
747 test
{LSET against non
list value
} {
749 catch {$r lset nolist
0 foo
} err
753 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
756 list [$r scard myset
] [$r sismember myset foo
] \
757 [$r sismember myset bar
] [$r sismember myset bla
] \
758 [lsort [$r smembers myset
]]
759 } {2 1 1 0 {bar foo
}}
761 test
{SADD adding the same element multiple times
} {
768 test
{SADD against non
set} {
769 catch {$r sadd mylist foo
} err
776 lsort [$r smembers myset
]
779 test
{Mass SADD and SINTER with two sets
} {
780 for {set i
0} {$i < 1000} {incr i
} {
782 $r sadd set2
[expr $i+995]
784 lsort [$r sinter set1 set2
]
785 } {995 996 997 998 999}
787 test
{SUNION with two sets
} {
788 lsort [$r sunion set1 set2
]
789 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
791 test
{SINTERSTORE with two sets
} {
792 $r sinterstore setres set1 set2
793 lsort [$r smembers setres
]
794 } {995 996 997 998 999}
796 test
{SINTERSTORE with two sets
, after a DEBUG RELOAD
} {
798 $r sinterstore setres set1 set2
799 lsort [$r smembers setres
]
800 } {995 996 997 998 999}
802 test
{SUNIONSTORE with two sets
} {
803 $r sunionstore setres set1 set2
804 lsort [$r smembers setres
]
805 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
807 test
{SUNIONSTORE against non existing keys
} {
809 list [$r sunionstore setres foo111 bar222
] [$r exists xxx
]
812 test
{SINTER against three sets
} {
817 lsort [$r sinter set1 set2 set3
]
820 test
{SINTERSTORE with three sets
} {
821 $r sinterstore setres set1 set2 set3
822 lsort [$r smembers setres
]
825 test
{SUNION with non existing keys
} {
826 lsort [$r sunion nokey1 set1 set2 nokey2
]
827 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
829 test
{SDIFF with two sets
} {
830 for {set i
5} {$i < 1000} {incr i
} {
833 lsort [$r sdiff set1 set4
]
836 test
{SDIFF with three sets
} {
838 lsort [$r sdiff set1 set4 set5
]
841 test
{SDIFFSTORE with three sets
} {
842 $r sdiffstore sres set1 set4 set5
843 lsort [$r smembers sres
]
851 list [lsort [list [$r spop myset
] [$r spop myset
] [$r spop myset
]]] [$r scard myset
]
854 test
{SAVE
- make sure there are all the types as values
} {
855 # Wait for a background saving in progress to terminate
857 $r lpush mysavelist hello
858 $r lpush mysavelist world
860 $r set mynormalkey
{blablablba
}
861 $r zadd mytestzset a
10
862 $r zadd mytestzset b
20
863 $r zadd mytestzset c
30
872 unset -nocomplain myset
874 for {set i
0} {$i < 100} {incr i
} {
875 set myset
([$r srandmember myset
]) 1
877 lsort [array names myset
]
880 test
{Create a random
list} {
882 array set seenrand
{}
883 for {set i
0} {$i < 10000} {incr i
} {
885 # Make sure all the weights are different because
886 # Redis does not use a stable sort but Tcl does.
887 set rint
[expr int
(rand
()*1000000)]
888 if {![info exists seenrand
($rint)]} break
890 set seenrand
($rint) x
892 $r set weight_
$i $rint
893 lappend tosort
[list $i $rint]
895 set sorted
[lsort -index 1 -real $tosort]
897 for {set i
0} {$i < 10000} {incr i
} {
898 lappend res
[lindex $sorted $i 0]
903 test
{SORT with BY against the newly created
list} {
904 $r sort tosort
{BY weight_
*}
907 test
{SORT direct
, numeric
, against the newly created
list} {
909 } [lsort -integer $res]
911 test
{SORT decreasing sort
} {
912 $r sort tosort
{DESC
}
913 } [lsort -decreasing -integer $res]
915 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
916 set start
[clock clicks
-milliseconds]
917 for {set i
0} {$i < 100} {incr i
} {
918 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
920 set elapsed
[expr [clock clicks
-milliseconds]-$start]
921 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
926 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
927 set start
[clock clicks
-milliseconds]
928 for {set i
0} {$i < 100} {incr i
} {
929 set sorted
[$r sort tosort
{LIMIT
0 10}]
931 set elapsed
[expr [clock clicks
-milliseconds]-$start]
932 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
937 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
938 set start
[clock clicks
-milliseconds]
939 for {set i
0} {$i < 100} {incr i
} {
940 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
942 set elapsed
[expr [clock clicks
-milliseconds]-$start]
943 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
948 test
{SORT regression
for issue
#19, sorting floats} {
950 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
954 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
956 test
{SORT with GET
#} {
961 $r mset weight_1
10 weight_2
5 weight_3
30
962 $r sort mylist BY weight_
* GET
#
965 test
{LREM
, remove all the occurrences
} {
969 $r rpush mylist foobar
970 $r rpush mylist foobared
975 set res
[$r lrem mylist
0 bar
]
976 list [$r lrange mylist
0 -1] $res
977 } {{foo foobar foobared zap test foo
} 2}
979 test
{LREM
, remove the first occurrence
} {
980 set res
[$r lrem mylist
1 foo
]
981 list [$r lrange mylist
0 -1] $res
982 } {{foobar foobared zap test foo
} 1}
984 test
{LREM
, remove non existing element
} {
985 set res
[$r lrem mylist
1 nosuchelement
]
986 list [$r lrange mylist
0 -1] $res
987 } {{foobar foobared zap test foo
} 0}
989 test
{LREM
, starting from tail with negative count
} {
993 $r rpush mylist foobar
994 $r rpush mylist foobared
1000 set res
[$r lrem mylist
-1 bar
]
1001 list [$r lrange mylist
0 -1] $res
1002 } {{foo bar foobar foobared zap test foo foo
} 1}
1004 test
{LREM
, starting from tail with negative count
(2)} {
1005 set res
[$r lrem mylist
-2 foo
]
1006 list [$r lrange mylist
0 -1] $res
1007 } {{foo bar foobar foobared zap test
} 2}
1009 test
{LREM
, deleting objects that may be encoded as integers
} {
1010 $r lpush myotherlist
1
1011 $r lpush myotherlist
2
1012 $r lpush myotherlist
3
1013 $r lrem myotherlist
1 2
1024 test
{MGET against non existing key
} {
1025 $r mget foo baazz bar
1028 test
{MGET against non-string key
} {
1031 $r mget foo baazz bar myset
1040 for {set i
0} {$i < 100} {incr i
} {
1041 set rkey
[$r randomkey
]
1042 if {$rkey eq
{foo
}} {
1045 if {$rkey eq
{bar
}} {
1049 list $foo_seen $bar_seen
1052 test
{RANDOMKEY against empty DB
} {
1057 test
{RANDOMKEY regression
1} {
1064 test
{GETSET
(set new value
)} {
1065 list [$r getset foo xyz
] [$r get foo
]
1068 test
{GETSET
(replace old value
)} {
1070 list [$r getset foo xyz
] [$r get foo
]
1073 test
{SMOVE basics
} {
1080 $r smove myset1 myset2 a
1081 list [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1084 test
{SMOVE non existing key
} {
1085 list [$r smove myset1 myset2 foo
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1086 } {0 {a x y z
} {b c
}}
1088 test
{SMOVE non existing src
set} {
1089 list [$r smove noset myset2 foo
] [lsort [$r smembers myset2
]]
1092 test
{SMOVE non existing dst
set} {
1093 list [$r smove myset2 myset3 y
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset3
]]
1096 test
{SMOVE wrong src key type
} {
1098 catch {$r smove x myset2 foo
} err
1102 test
{SMOVE wrong dst key type
} {
1104 catch {$r smove myset2 x foo
} err
1108 test
{MSET base case
} {
1109 $r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
1111 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
1113 test
{MSET wrong number of args
} {
1114 catch {$r mset x
10 y
"foo bar" z
} err
1118 test
{MSETNX with already existent key
} {
1119 list [$r msetnx x1 xxx y2 yyy x
20] [$r exists x1
] [$r exists y2
]
1122 test
{MSETNX with not existing keys
} {
1123 list [$r msetnx x1 xxx y2 yyy
] [$r get x1
] [$r get y2
]
1126 test
{MSETNX should remove all the volatile keys even on
failure} {
1130 list [$r msetnx x A y B z C
] [$r mget x y z
]
1133 test
{ZSET basic ZADD and score
update} {
1137 set aux1
[$r zrange ztmp
0 -1]
1139 set aux2
[$r zrange ztmp
0 -1]
1143 test
{ZCARD basics
} {
1147 test
{ZCARD non existing key
} {
1148 $r zcard ztmp-blabla
1154 for {set i
0} {$i < 1000} {incr i
} {
1155 set score
[expr rand
()]
1157 $r zadd zscoretest
$score $i
1159 for {set i
0} {$i < 1000} {incr i
} {
1160 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1161 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1168 test
{ZSCORE
after a DEBUG RELOAD
} {
1172 for {set i
0} {$i < 1000} {incr i
} {
1173 set score
[expr rand
()]
1175 $r zadd zscoretest
$score $i
1178 for {set i
0} {$i < 1000} {incr i
} {
1179 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1180 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1187 test
{ZRANGE and ZREVRANGE basics
} {
1188 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1]
1191 test
{ZRANGE and ZREVRANGE stress testing
} {
1192 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1]
1195 test
{ZSETs stress tester
- sorting is working well?
} {
1197 for {set test
0} {$test < 2} {incr test
} {
1198 unset -nocomplain auxarray
1199 array set auxarray
{}
1202 for {set i
0} {$i < 1000} {incr i
} {
1204 set score
[expr rand
()]
1206 set score
[expr int
(rand
()*10)]
1208 set auxarray
($i) $score
1209 $r zadd myzset
$score $i
1211 if {[expr rand
()] < .2} {
1212 set j
[expr int
(rand
()*1000)]
1214 set score
[expr rand
()]
1216 set score
[expr int
(rand
()*10)]
1218 set auxarray
($j) $score
1219 $r zadd myzset
$score $j
1222 foreach {item score
} [array get auxarray
] {
1223 lappend auxlist
[list $score $item]
1225 set sorted
[lsort -command zlistAlikeSort
$auxlist]
1228 lappend auxlist
[lindex $x 1]
1230 set fromredis
[$r zrange myzset
0 -1]
1232 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
1233 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1241 test
{ZINCRBY
- can create a new sorted
set} {
1243 $r zincrby zset
1 foo
1244 list [$r zrange zset
0 -1] [$r zscore zset foo
]
1247 test
{ZINCRBY
- increment and decrement
} {
1248 $r zincrby zset
2 foo
1249 $r zincrby zset
1 bar
1250 set v1
[$r zrange zset
0 -1]
1251 $r zincrby zset
10 bar
1252 $r zincrby zset
-5 foo
1253 $r zincrby zset
-5 bar
1254 set v2
[$r zrange zset
0 -1]
1255 list $v1 $v2 [$r zscore zset foo
] [$r zscore zset bar
]
1256 } {{bar foo
} {foo bar
} -2 6}
1258 test
{ZRANGEBYSCORE basics
} {
1265 $r zrangebyscore zset
2 4
1268 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
1271 for {set i
0} {$i < 1000} {incr i
} {
1272 $r zadd zset
[expr rand
()] $i
1274 for {set i
0} {$i < 100} {incr i
} {
1275 set min
[expr rand
()]
1276 set max
[expr rand
()]
1282 set low
[$r zrangebyscore zset
-inf $min]
1283 set ok
[$r zrangebyscore zset
$min $max]
1284 set high
[$r zrangebyscore zset
$max +inf
]
1286 set score
[$r zscore zset
$x]
1287 if {$score > $min} {
1288 append err
"Error, score for $x is $score > $min\n"
1292 set score
[$r zscore zset
$x]
1293 if {$score < $min ||
$score > $max} {
1294 append err
"Error, score for $x is $score outside $min-$max range\n"
1298 set score
[$r zscore zset
$x]
1299 if {$score < $max} {
1300 append err
"Error, score for $x is $score < $max\n"
1307 test
{ZRANGEBYSCORE with LIMIT
} {
1315 [$r zrangebyscore zset
0 10 LIMIT
0 2] \
1316 [$r zrangebyscore zset
0 10 LIMIT
2 3] \
1317 [$r zrangebyscore zset
0 10 LIMIT
2 10] \
1318 [$r zrangebyscore zset
0 10 LIMIT
20 10]
1319 } {{a b
} {c d e
} {c d e
} {}}
1321 test
{ZREMRANGE basics
} {
1328 list [$r zremrangebyscore zset
2 4] [$r zrange zset
0 -1]
1331 test
{ZREMRANGE from
-inf to
+inf
} {
1338 list [$r zremrangebyscore zset
-inf +inf
] [$r zrange zset
0 -1]
1341 test
{SORT against sorted sets
} {
1348 $r sort zset alpha desc
1351 test
{Sorted sets
+inf and
-inf handling
} {
1356 $r zadd zset
1000000 d
1357 $r zadd zset
+inf max
1358 $r zadd zset
-inf min
1362 test
{EXPIRE
- don't
set timeouts multiple times
} {
1364 set v1
[$r expire x
5]
1366 set v3
[$r expire x
10]
1368 list $v1 $v2 $v3 $v4
1371 test
{EXPIRE
- It should be still possible to
read 'x'
} {
1375 test
{EXPIRE
- After
6 seconds the key should no longer be here
} {
1377 list [$r get x
] [$r exists x
]
1380 test
{EXPIRE
- Delete on write policy
} {
1388 test
{EXPIREAT
- Check
for EXPIRE alike behavior
} {
1391 $r expireat x
[expr [clock seconds
]+15]
1395 test
{ZSETs skiplist implementation backlink consistency test
} {
1398 for {set j
0} {$j < $elements} {incr j
} {
1399 $r zadd myzset
[expr rand
()] "Element-$j"
1400 $r zrem myzset
"Element-[expr int(rand()*$elements)]"
1402 set l1
[$r zrange myzset
0 -1]
1403 set l2
[$r zrevrange myzset
0 -1]
1404 for {set j
0} {$j < [llength $l1]} {incr j
} {
1405 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
1412 foreach fuzztype
{binary alpha compr
} {
1413 test
"FUZZ stresser with data model $fuzztype" {
1415 for {set i
0} {$i < 10000} {incr i
} {
1416 set fuzz
[randstring
0 512 $fuzztype]
1418 set got
[$r get foo
]
1419 if {$got ne
$fuzz} {
1420 set err
[list $fuzz $got]
1438 test
{Handle an empty query well
} {
1440 puts -nonewline $fd "\r\n"
1445 test
{Negative multi bulk command does not create problems
} {
1447 puts -nonewline $fd "*-10\r\n"
1452 test
{Negative multi bulk payload
} {
1454 puts -nonewline $fd "SET x -10\r\n"
1459 test
{Too big bulk payload
} {
1461 puts -nonewline $fd "SET x 2000000000\r\n"
1464 } {*invalid bulk
*count
*}
1466 test
{Multi bulk request not followed by bulk args
} {
1468 puts -nonewline $fd "*1\r\nfoo\r\n"
1471 } {*protocol
error*}
1473 test
{Generic wrong number of args
} {
1474 catch {$r ping x y z
} err
1476 } {*wrong
*arguments
*ping
*}
1478 test
{SELECT an out of range DB
} {
1479 catch {$r select
1000000} err
1483 if {![catch {package require sha1
}]} {
1484 test
{Check consistency of different data types
after a reload
} {
1486 createComplexDataset
$r 10000
1487 set sha1
[datasetDigest
$r]
1489 set sha1_after
[datasetDigest
$r]
1490 expr {$sha1 eq
$sha1_after}
1493 test
{Same dataset digest
if saving
/reloading as AOF?
} {
1495 waitForBgrewriteaof
$r
1497 set sha1_after
[datasetDigest
$r]
1498 expr {$sha1 eq
$sha1_after}
1502 test
{EXPIRES
after a reload
(snapshot
+ append only
file)} {
1509 set e1
[expr {$ttl > 900 && $ttl <= 1000}]
1511 waitForBgrewriteaof
$r
1513 set e2
[expr {$ttl > 900 && $ttl <= 1000}]
1517 test
{PIPELINING stresser
(also a regression
for the old epoll bug
)} {
1518 set fd2
[socket 127.0.0.1 6379]
1519 fconfigure $fd2 -encoding binary -translation binary
1521 for {set i
0} {$i < 100000} {incr i
} {
1523 set val
"0000${i}0000"
1524 append q
"SET key:$i [string length $val]\r\n$val\r\n"
1525 puts -nonewline $fd2 $q
1527 append q
"GET key:$i\r\n"
1528 puts -nonewline $fd2 $q
1532 for {set i
0} {$i < 100000} {incr i
} {
1535 set count
[string range
$count 1 end
]
1536 set val
[read $fd2 $count]
1543 # Leave the user with a clean DB before to exit
1548 lappend aux
[$r dbsize
]
1551 lappend aux
[$r dbsize
]
1554 test
{Perform a final SAVE to leave a clean DB on disk
} {
1559 if {[string match
{*Darwin
*} [exec uname
-a]]} {
1560 test
{Check
for memory leaks
} {
1561 exec leaks redis-server
1566 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1567 if {$::failed > 0} {
1568 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1577 set randkey
[expr int
(rand
()*10000)]
1578 set randval
[expr int
(rand
()*10000)]
1579 set randidx0
[expr int
(rand
()*10)]
1580 set randidx1
[expr int
(rand
()*10)]
1581 set cmd
[expr int
(rand
()*20)]
1583 if {$cmd == 0} {$r set $randkey $randval}
1584 if {$cmd == 1} {$r get
$randkey}
1585 if {$cmd == 2} {$r incr $randkey}
1586 if {$cmd == 3} {$r lpush
$randkey $randval}
1587 if {$cmd == 4} {$r rpop
$randkey}
1588 if {$cmd == 5} {$r del
$randkey}
1589 if {$cmd == 6} {$r llen
$randkey}
1590 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1591 if {$cmd == 8} {$r ltrim
$randkey $randidx0 $randidx1}
1592 if {$cmd == 9} {$r lindex $randkey $randidx0}
1593 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1594 if {$cmd == 11} {$r sadd
$randkey $randval}
1595 if {$cmd == 12} {$r srem
$randkey $randval}
1596 if {$cmd == 13} {$r smove
$randkey $randval}
1597 if {$cmd == 14} {$r scard
$randkey}
1598 if {$cmd == 15} {$r expire
$randkey [expr $randval%60]}
1606 # Set a few configuration defaults
1607 set ::host 127.0.0.1
1615 for {set j
0} {$j < [llength $argv]} {incr j
} {
1616 set opt
[lindex $argv $j]
1617 set arg
[lindex $argv [expr $j+1]]
1618 set lastarg
[expr {$arg eq
{}}]
1619 if {$opt eq
{-h} && !$lastarg} {
1622 } elseif
{$opt eq
{-p} && !$lastarg} {
1625 } elseif
{$opt eq
{-stress}} {
1627 } elseif
{$opt eq
{--flush}} {
1629 } elseif
{$opt eq
{--first} && !$lastarg} {
1632 } elseif
{$opt eq
{--last} && !$lastarg} {
1636 puts "Wrong argument: $opt"
1641 # Before to run the test check if DB 9 and DB 10 are empty
1649 set db9size
[$r dbsize
]
1651 set db10size
[$r dbsize
]
1652 if {$db9size != 0 ||
$db10size != 0} {
1653 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1664 main
$::host $::port