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
} {
129 set d
[expr {rand
()}]
150 randpath
{$r lpush
$k $v} \
157 randpath
{$r sadd
$k $v} \
161 randpath
{$r zadd
$k $d $v} \
168 proc datasetDigest r
{
169 set keys
[lsort [split [$r keys
*] " "]]
175 set aux
[::sha1::sha1 -hex [$r get
$k]]
177 if {[$r llen
$k] == 0} {
180 set aux
[::sha1::sha1 -hex [$r lrange $k 0 -1]]
183 if {[$r scard
$k] == 0} {
186 set aux
[::sha1::sha1 -hex [lsort [$r smembers
$k]]]
189 if {[$r zcard
$k] == 0} {
192 set aux
[::sha1::sha1 -hex [$r zrange
$k 0 -1]]
195 error "Type not supported"
198 if {$aux eq
{}} continue
199 set digest
[::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
204 proc main
{server port
} {
205 set r
[redis
$server $port]
210 # The following AUTH test should be enabled only when requirepass
211 # <PASSWORD> is set in redis.conf and redis-server was started with
212 # redis.conf as the first argument.
214 #test {AUTH with requirepass in redis.conf} {
218 test
{DEL all keys to start with a clean DB
} {
219 foreach key
[$r keys
*] {$r del
$key}
223 test
{SET and GET an item
} {
228 test
{SET and GET an empty item
} {
233 test
{DEL against a single item
} {
242 list [$r del foo1 foo2 foo3 foo4
] [$r mget foo1 foo2 foo3
]
245 test
{KEYS with pattern
} {
246 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
250 } {foo_a foo_b foo_c
}
252 test
{KEYS to get all keys
} {
254 } {foo_a foo_b foo_c key_x key_y key_z
}
260 test
{DEL all keys
} {
261 foreach key
[$r keys
*] {$r del
$key}
265 test
{Very big payload in GET
/SET
} {
266 set buf
[string repeat
"abcd" 1000000]
269 } [string repeat
"abcd" 1000000]
271 test
{SET
10000 numeric keys and access all them in reverse order
} {
272 for {set x
0} {$x < 10000} {incr x
} {
276 for {set x
9999} {$x >= 0} {incr x
-1} {
282 test
{DBSIZE should be
10001 now
} {
286 test
{INCR against non existing key
} {
288 append res
[$r incr novar
]
289 append res
[$r get novar
]
292 test
{INCR against key created by
incr itself
} {
296 test
{INCR against key originally
set with SET
} {
301 test
{INCR over
32bit value
} {
302 $r set novar
17179869184
306 test
{INCRBY over
32bit value with over
32bit increment
} {
307 $r set novar
17179869184
308 $r incrby novar
17179869184
311 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
312 $r set novar
17179869184
313 $r decrby novar
17179869185
316 test
{SETNX target key missing
} {
317 $r setnx novar2 foobared
321 test
{SETNX target key exists
} {
322 $r setnx novar2 blabla
326 test
{SETNX will overwrite EXPIREing key
} {
336 append res
[$r exists newkey
]
338 append res
[$r exists newkey
]
341 test
{Zero length value in key. SET
/GET
/EXISTS
} {
343 set res
[$r get emptykey
]
344 append res
[$r exists emptykey
]
346 append res
[$r exists emptykey
]
349 test
{Commands pipelining
} {
351 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
354 append res
[string match OK
* [::redis::redis_read_reply $fd]]
355 append res
[::redis::redis_read_reply $fd]
356 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
360 test
{Non existing command
} {
361 catch {$r foobaredcommand
} err
362 string match ERR
* $err
365 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
369 set res
[$r llen mylist
]
370 append res
[$r lindex mylist
0]
371 append res
[$r lindex mylist
1]
372 append res
[$r lindex mylist
2]
380 test
{Create a long
list and check every single element with LINDEX
} {
382 for {set i
0} {$i < 1000} {incr i
} {
385 for {set i
0} {$i < 1000} {incr i
} {
386 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
387 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
394 test
{Test elements with LINDEX in random access
} {
396 for {set i
0} {$i < 1000} {incr i
} {
397 set rint
[expr int
(rand
()*1000)]
398 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
399 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
406 test
{Check
if the
list is still ok
after a DEBUG RELOAD
} {
409 for {set i
0} {$i < 1000} {incr i
} {
410 set rint
[expr int
(rand
()*1000)]
411 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
412 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
419 test
{LLEN against non-list value
error} {
422 catch {$r llen mylist
} err
426 test
{LINDEX against non-list value
error} {
427 catch {$r lindex mylist
0} err
431 test
{LPUSH against non-list value
error} {
432 catch {$r lpush mylist
0} err
436 test
{RPUSH against non-list value
error} {
437 catch {$r rpush mylist
0} err
441 test
{RPOPLPUSH base case
} {
447 set v1
[$r rpoplpush mylist newlist
]
448 set v2
[$r rpoplpush mylist newlist
]
449 set l1
[$r lrange mylist
0 -1]
450 set l2
[$r lrange newlist
0 -1]
454 test
{RPOPLPUSH with the same
list as src and dst
} {
459 set l1
[$r lrange mylist
0 -1]
460 set v
[$r rpoplpush mylist mylist
]
461 set l2
[$r lrange mylist
0 -1]
463 } {{a b c
} c
{c a b
}}
465 test
{RPOPLPUSH target
list already exists
} {
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]
478 } {d c
{a b
} {c d x
}}
480 test
{RPOPLPUSH against non existing key
} {
483 set v1
[$r rpoplpush mylist newlist
]
484 list $v1 [$r exists mylist
] [$r exists newlist
]
487 test
{RPOPLPUSH against non
list src key
} {
491 catch {$r rpoplpush mylist newlist
} err
492 list [$r type mylist
] [$r exists newlist
] [string range
$err 0 2]
495 test
{RPOPLPUSH against non
list dst key
} {
503 catch {$r rpoplpush mylist newlist
} err
504 list [$r lrange mylist
0 -1] [$r type newlist
] [string range
$err 0 2]
505 } {{a b c d
} string ERR
}
507 test
{RENAME basic usage
} {
509 $r rename mykey mykey1
510 $r rename mykey1 mykey2
514 test
{RENAME
source key should no longer exist
} {
518 test
{RENAME against already existing key
} {
521 $r rename mykey2 mykey
522 set res
[$r get mykey
]
523 append res
[$r exists mykey2
]
526 test
{RENAMENX basic usage
} {
530 $r renamenx mykey mykey2
531 set res
[$r get mykey2
]
532 append res
[$r exists mykey
]
535 test
{RENAMENX against already existing key
} {
538 $r renamenx mykey mykey2
541 test
{RENAMENX against already existing key
(2)} {
542 set res
[$r get mykey
]
543 append res
[$r get mykey2
]
546 test
{RENAME against non existing
source key
} {
547 catch {$r rename nokey foobar
} err
551 test
{RENAME where
source and dest key is the same
} {
552 catch {$r rename mykey mykey
} err
556 test
{DEL all keys again
(DB
0)} {
557 foreach key
[$r keys
*] {
563 test
{DEL all keys again
(DB
1)} {
565 foreach key
[$r keys
*] {
573 test
{MOVE basic usage
} {
577 lappend res
[$r exists mykey
]
578 lappend res
[$r dbsize
]
580 lappend res
[$r get mykey
]
581 lappend res
[$r dbsize
]
584 } [list 0 0 foobar
1]
586 test
{MOVE against key existing in the target DB
} {
591 test
{SET
/GET keys in different DBs
} {
599 lappend res
[$r get a
]
600 lappend res
[$r get b
]
602 lappend res
[$r get a
]
603 lappend res
[$r get b
]
606 } {hello world foo bared
}
608 test
{Basic LPOP
/RPOP
} {
613 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
616 test
{LPOP
/RPOP against empty
list} {
620 test
{LPOP against non
list value
} {
622 catch {$r lpop notalist
} err
626 test
{Mass LPUSH
/LPOP
} {
628 for {set i
0} {$i < 1000} {incr i
} {
633 for {set i
0} {$i < 500} {incr i
} {
634 incr sum2
[$r lpop mylist
]
635 incr sum2
[$r rpop mylist
]
640 test
{LRANGE basics
} {
641 for {set i
0} {$i < 10} {incr i
} {
644 list [$r lrange mylist
1 -2] \
645 [$r lrange mylist
-3 -1] \
646 [$r lrange mylist
4 4]
647 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
649 test
{LRANGE inverted indexes
} {
653 test
{LRANGE out of range indexes including the full
list} {
654 $r lrange mylist
-1000 1000
655 } {0 1 2 3 4 5 6 7 8 9}
657 test
{LRANGE against non existing key
} {
658 $r lrange nosuchkey
0 1
661 test
{LTRIM basics
} {
663 for {set i
0} {$i < 100} {incr i
} {
667 $r lrange mylist
0 -1
672 $r lset mylist
-1 bar
673 $r lrange mylist
0 -1
676 test
{LSET out of range index
} {
677 catch {$r lset mylist
10 foo
} err
681 test
{LSET against non existing key
} {
682 catch {$r lset nosuchkey
10 foo
} err
686 test
{LSET against non
list value
} {
688 catch {$r lset nolist
0 foo
} err
692 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
695 list [$r scard myset
] [$r sismember myset foo
] \
696 [$r sismember myset bar
] [$r sismember myset bla
] \
697 [lsort [$r smembers myset
]]
698 } {2 1 1 0 {bar foo
}}
700 test
{SADD adding the same element multiple times
} {
707 test
{SADD against non
set} {
708 catch {$r sadd mylist foo
} err
715 lsort [$r smembers myset
]
718 test
{Mass SADD and SINTER with two sets
} {
719 for {set i
0} {$i < 1000} {incr i
} {
721 $r sadd set2
[expr $i+995]
723 lsort [$r sinter set1 set2
]
724 } {995 996 997 998 999}
726 test
{SUNION with two sets
} {
727 lsort [$r sunion set1 set2
]
728 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
730 test
{SINTERSTORE with two sets
} {
731 $r sinterstore setres set1 set2
732 lsort [$r smembers setres
]
733 } {995 996 997 998 999}
735 test
{SINTERSTORE with two sets
, after a DEBUG RELOAD
} {
737 $r sinterstore setres set1 set2
738 lsort [$r smembers setres
]
739 } {995 996 997 998 999}
741 test
{SUNIONSTORE with two sets
} {
742 $r sunionstore setres set1 set2
743 lsort [$r smembers setres
]
744 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
746 test
{SUNIONSTORE against non existing keys
} {
748 list [$r sunionstore setres foo111 bar222
] [$r exists xxx
]
751 test
{SINTER against three sets
} {
756 lsort [$r sinter set1 set2 set3
]
759 test
{SINTERSTORE with three sets
} {
760 $r sinterstore setres set1 set2 set3
761 lsort [$r smembers setres
]
764 test
{SUNION with non existing keys
} {
765 lsort [$r sunion nokey1 set1 set2 nokey2
]
766 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
768 test
{SDIFF with two sets
} {
769 for {set i
5} {$i < 1000} {incr i
} {
772 lsort [$r sdiff set1 set4
]
775 test
{SDIFF with three sets
} {
777 lsort [$r sdiff set1 set4 set5
]
780 test
{SDIFFSTORE with three sets
} {
781 $r sdiffstore sres set1 set4 set5
782 lsort [$r smembers sres
]
790 list [lsort [list [$r spop myset
] [$r spop myset
] [$r spop myset
]]] [$r scard myset
]
793 test
{SAVE
- make sure there are all the types as values
} {
794 # Wait for a background saving in progress to terminate
796 $r lpush mysavelist hello
797 $r lpush mysavelist world
799 $r set mynormalkey
{blablablba
}
800 $r zadd mytestzset a
10
801 $r zadd mytestzset b
20
802 $r zadd mytestzset c
30
811 unset -nocomplain myset
813 for {set i
0} {$i < 100} {incr i
} {
814 set myset
([$r srandmember myset
]) 1
816 lsort [array names myset
]
819 test
{Create a random
list} {
821 array set seenrand
{}
822 for {set i
0} {$i < 10000} {incr i
} {
824 # Make sure all the weights are different because
825 # Redis does not use a stable sort but Tcl does.
826 set rint
[expr int
(rand
()*1000000)]
827 if {![info exists seenrand
($rint)]} break
829 set seenrand
($rint) x
831 $r set weight_
$i $rint
832 lappend tosort
[list $i $rint]
834 set sorted
[lsort -index 1 -real $tosort]
836 for {set i
0} {$i < 10000} {incr i
} {
837 lappend res
[lindex $sorted $i 0]
842 test
{SORT with BY against the newly created
list} {
843 $r sort tosort
{BY weight_
*}
846 test
{SORT direct
, numeric
, against the newly created
list} {
848 } [lsort -integer $res]
850 test
{SORT decreasing sort
} {
851 $r sort tosort
{DESC
}
852 } [lsort -decreasing -integer $res]
854 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
855 set start
[clock clicks
-milliseconds]
856 for {set i
0} {$i < 100} {incr i
} {
857 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
859 set elapsed
[expr [clock clicks
-milliseconds]-$start]
860 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
865 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
866 set start
[clock clicks
-milliseconds]
867 for {set i
0} {$i < 100} {incr i
} {
868 set sorted
[$r sort tosort
{LIMIT
0 10}]
870 set elapsed
[expr [clock clicks
-milliseconds]-$start]
871 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
876 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
877 set start
[clock clicks
-milliseconds]
878 for {set i
0} {$i < 100} {incr i
} {
879 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
881 set elapsed
[expr [clock clicks
-milliseconds]-$start]
882 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
887 test
{SORT regression
for issue
#19, sorting floats} {
889 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
893 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
895 test
{SORT with GET
#} {
900 $r mset weight_1
10 weight_2
5 weight_3
30
901 $r sort mylist BY weight_
* GET
#
904 test
{LREM
, remove all the occurrences
} {
908 $r rpush mylist foobar
909 $r rpush mylist foobared
914 set res
[$r lrem mylist
0 bar
]
915 list [$r lrange mylist
0 -1] $res
916 } {{foo foobar foobared zap test foo
} 2}
918 test
{LREM
, remove the first occurrence
} {
919 set res
[$r lrem mylist
1 foo
]
920 list [$r lrange mylist
0 -1] $res
921 } {{foobar foobared zap test foo
} 1}
923 test
{LREM
, remove non existing element
} {
924 set res
[$r lrem mylist
1 nosuchelement
]
925 list [$r lrange mylist
0 -1] $res
926 } {{foobar foobared zap test foo
} 0}
928 test
{LREM
, starting from tail with negative count
} {
932 $r rpush mylist foobar
933 $r rpush mylist foobared
939 set res
[$r lrem mylist
-1 bar
]
940 list [$r lrange mylist
0 -1] $res
941 } {{foo bar foobar foobared zap test foo foo
} 1}
943 test
{LREM
, starting from tail with negative count
(2)} {
944 set res
[$r lrem mylist
-2 foo
]
945 list [$r lrange mylist
0 -1] $res
946 } {{foo bar foobar foobared zap test
} 2}
948 test
{LREM
, deleting objects that may be encoded as integers
} {
949 $r lpush myotherlist
1
950 $r lpush myotherlist
2
951 $r lpush myotherlist
3
952 $r lrem myotherlist
1 2
963 test
{MGET against non existing key
} {
964 $r mget foo baazz bar
967 test
{MGET against non-string key
} {
970 $r mget foo baazz bar myset
979 for {set i
0} {$i < 100} {incr i
} {
980 set rkey
[$r randomkey
]
981 if {$rkey eq
{foo
}} {
984 if {$rkey eq
{bar
}} {
988 list $foo_seen $bar_seen
991 test
{RANDOMKEY against empty DB
} {
996 test
{RANDOMKEY regression
1} {
1003 test
{GETSET
(set new value
)} {
1004 list [$r getset foo xyz
] [$r get foo
]
1007 test
{GETSET
(replace old value
)} {
1009 list [$r getset foo xyz
] [$r get foo
]
1012 test
{SMOVE basics
} {
1019 $r smove myset1 myset2 a
1020 list [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1023 test
{SMOVE non existing key
} {
1024 list [$r smove myset1 myset2 foo
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1025 } {0 {a x y z
} {b c
}}
1027 test
{SMOVE non existing src
set} {
1028 list [$r smove noset myset2 foo
] [lsort [$r smembers myset2
]]
1031 test
{SMOVE non existing dst
set} {
1032 list [$r smove myset2 myset3 y
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset3
]]
1035 test
{SMOVE wrong src key type
} {
1037 catch {$r smove x myset2 foo
} err
1041 test
{SMOVE wrong dst key type
} {
1043 catch {$r smove myset2 x foo
} err
1047 test
{MSET base case
} {
1048 $r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
1050 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
1052 test
{MSET wrong number of args
} {
1053 catch {$r mset x
10 y
"foo bar" z
} err
1057 test
{MSETNX with already existent key
} {
1058 list [$r msetnx x1 xxx y2 yyy x
20] [$r exists x1
] [$r exists y2
]
1061 test
{MSETNX with not existing keys
} {
1062 list [$r msetnx x1 xxx y2 yyy
] [$r get x1
] [$r get y2
]
1065 test
{MSETNX should remove all the volatile keys even on
failure} {
1069 list [$r msetnx x A y B z C
] [$r mget x y z
]
1072 test
{ZSET basic ZADD and score
update} {
1076 set aux1
[$r zrange ztmp
0 -1]
1078 set aux2
[$r zrange ztmp
0 -1]
1082 test
{ZCARD basics
} {
1086 test
{ZCARD non existing key
} {
1087 $r zcard ztmp-blabla
1093 for {set i
0} {$i < 1000} {incr i
} {
1094 set score
[expr rand
()]
1096 $r zadd zscoretest
$score $i
1098 for {set i
0} {$i < 1000} {incr i
} {
1099 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1100 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1107 test
{ZSCORE
after a DEBUG RELOAD
} {
1111 for {set i
0} {$i < 1000} {incr i
} {
1112 set score
[expr rand
()]
1114 $r zadd zscoretest
$score $i
1117 for {set i
0} {$i < 1000} {incr i
} {
1118 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1119 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1126 test
{ZRANGE and ZREVRANGE
} {
1127 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1]
1130 test
{ZSETs stress tester
- sorting is working well?
} {
1132 for {set test
0} {$test < 2} {incr test
} {
1133 unset -nocomplain auxarray
1134 array set auxarray
{}
1137 for {set i
0} {$i < 1000} {incr i
} {
1139 set score
[expr rand
()]
1141 set score
[expr int
(rand
()*10)]
1143 set auxarray
($i) $score
1144 $r zadd myzset
$score $i
1146 if {[expr rand
()] < .2} {
1147 set j
[expr int
(rand
()*1000)]
1149 set score
[expr rand
()]
1151 set score
[expr int
(rand
()*10)]
1153 set auxarray
($j) $score
1154 $r zadd myzset
$score $j
1157 foreach {item score
} [array get auxarray
] {
1158 lappend auxlist
[list $score $item]
1160 set sorted
[lsort -command zlistAlikeSort
$auxlist]
1163 lappend auxlist
[lindex $x 1]
1165 set fromredis
[$r zrange myzset
0 -1]
1167 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
1168 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1176 test
{ZINCRBY
- can create a new sorted
set} {
1178 $r zincrby zset
1 foo
1179 list [$r zrange zset
0 -1] [$r zscore zset foo
]
1182 test
{ZINCRBY
- increment and decrement
} {
1183 $r zincrby zset
2 foo
1184 $r zincrby zset
1 bar
1185 set v1
[$r zrange zset
0 -1]
1186 $r zincrby zset
10 bar
1187 $r zincrby zset
-5 foo
1188 $r zincrby zset
-5 bar
1189 set v2
[$r zrange zset
0 -1]
1190 list $v1 $v2 [$r zscore zset foo
] [$r zscore zset bar
]
1191 } {{bar foo
} {foo bar
} -2 6}
1193 test
{ZRANGEBYSCORE basics
} {
1200 $r zrangebyscore zset
2 4
1203 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
1206 for {set i
0} {$i < 1000} {incr i
} {
1207 $r zadd zset
[expr rand
()] $i
1209 for {set i
0} {$i < 100} {incr i
} {
1210 set min
[expr rand
()]
1211 set max
[expr rand
()]
1217 set low
[$r zrangebyscore zset
-inf $min]
1218 set ok
[$r zrangebyscore zset
$min $max]
1219 set high
[$r zrangebyscore zset
$max +inf
]
1221 set score
[$r zscore zset
$x]
1222 if {$score > $min} {
1223 append err
"Error, score for $x is $score > $min\n"
1227 set score
[$r zscore zset
$x]
1228 if {$score < $min ||
$score > $max} {
1229 append err
"Error, score for $x is $score outside $min-$max range\n"
1233 set score
[$r zscore zset
$x]
1234 if {$score < $max} {
1235 append err
"Error, score for $x is $score < $max\n"
1242 test
{ZRANGEBYSCORE with LIMIT
} {
1250 [$r zrangebyscore zset
0 10 LIMIT
0 2] \
1251 [$r zrangebyscore zset
0 10 LIMIT
2 3] \
1252 [$r zrangebyscore zset
0 10 LIMIT
2 10] \
1253 [$r zrangebyscore zset
0 10 LIMIT
20 10]
1254 } {{a b
} {c d e
} {c d e
} {}}
1256 test
{ZREMRANGE basics
} {
1263 list [$r zremrangebyscore zset
2 4] [$r zrange zset
0 -1]
1266 test
{ZREMRANGE from
-inf to
+inf
} {
1273 list [$r zremrangebyscore zset
-inf +inf
] [$r zrange zset
0 -1]
1276 test
{SORT against sorted sets
} {
1283 $r sort zset alpha desc
1286 test
{Sorted sets
+inf and
-inf handling
} {
1291 $r zadd zset
1000000 d
1292 $r zadd zset
+inf max
1293 $r zadd zset
-inf min
1297 test
{EXPIRE
- don't
set timeouts multiple times
} {
1299 set v1
[$r expire x
5]
1301 set v3
[$r expire x
10]
1303 list $v1 $v2 $v3 $v4
1306 test
{EXPIRE
- It should be still possible to
read 'x'
} {
1310 test
{EXPIRE
- After
6 seconds the key should no longer be here
} {
1312 list [$r get x
] [$r exists x
]
1315 test
{EXPIRE
- Delete on write policy
} {
1323 test
{EXPIREAT
- Check
for EXPIRE alike behavior
} {
1326 $r expireat x
[expr [clock seconds
]+15]
1330 test
{ZSETs skiplist implementation backlink consistency test
} {
1333 for {set j
0} {$j < $elements} {incr j
} {
1334 $r zadd myzset
[expr rand
()] "Element-$j"
1335 $r zrem myzset
"Element-[expr int(rand()*$elements)]"
1337 set l1
[$r zrange myzset
0 -1]
1338 set l2
[$r zrevrange myzset
0 -1]
1339 for {set j
0} {$j < [llength $l1]} {incr j
} {
1340 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
1347 foreach fuzztype
{binary alpha compr
} {
1348 test
"FUZZ stresser with data model $fuzztype" {
1350 for {set i
0} {$i < 10000} {incr i
} {
1351 set fuzz
[randstring
0 512 $fuzztype]
1353 set got
[$r get foo
]
1354 if {$got ne
$fuzz} {
1355 set err
[list $fuzz $got]
1373 test
{Handle an empty query well
} {
1375 puts -nonewline $fd "\r\n"
1380 test
{Negative multi bulk command does not create problems
} {
1382 puts -nonewline $fd "*-10\r\n"
1387 test
{Negative multi bulk payload
} {
1389 puts -nonewline $fd "SET x -10\r\n"
1394 test
{Too big bulk payload
} {
1396 puts -nonewline $fd "SET x 2000000000\r\n"
1399 } {*invalid bulk
*count
*}
1401 test
{Multi bulk request not followed by bulk args
} {
1403 puts -nonewline $fd "*1\r\nfoo\r\n"
1406 } {*protocol
error*}
1408 test
{Generic wrong number of args
} {
1409 catch {$r ping x y z
} err
1411 } {*wrong
*arguments
*ping
*}
1413 test
{SELECT an out of range DB
} {
1414 catch {$r select
1000000} err
1418 if {![catch {package require sha1
}]} {
1419 test
{Check consistency of different data types
after a reload
} {
1421 createComplexDataset
$r 10000
1422 set sha1
[datasetDigest
$r]
1424 set sha1_after
[datasetDigest
$r]
1425 expr {$sha1 eq
$sha1_after}
1428 test
{Same dataset digest
if saving
/reloading as AOF?
} {
1430 waitForBgrewriteaof
$r
1432 set sha1_after
[datasetDigest
$r]
1433 expr {$sha1 eq
$sha1_after}
1437 test
{EXPIRES
after a reload
(snapshot
+ append only
file)} {
1444 set e1
[expr {$ttl > 900 && $ttl <= 1000}]
1446 waitForBgrewriteaof
$r
1448 set e2
[expr {$ttl > 900 && $ttl <= 1000}]
1452 # Leave the user with a clean DB before to exit
1457 lappend aux
[$r dbsize
]
1460 lappend aux
[$r dbsize
]
1463 test
{Perform a final SAVE to leave a clean DB on disk
} {
1468 if {[string match
{*Darwin
*} [exec uname
-a]]} {
1469 test
{Check
for memory leaks
} {
1470 exec leaks redis-server
1475 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1476 if {$::failed > 0} {
1477 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1486 set randkey
[expr int
(rand
()*10000)]
1487 set randval
[expr int
(rand
()*10000)]
1488 set randidx0
[expr int
(rand
()*10)]
1489 set randidx1
[expr int
(rand
()*10)]
1490 set cmd
[expr int
(rand
()*20)]
1492 if {$cmd == 0} {$r set $randkey $randval}
1493 if {$cmd == 1} {$r get
$randkey}
1494 if {$cmd == 2} {$r incr $randkey}
1495 if {$cmd == 3} {$r lpush
$randkey $randval}
1496 if {$cmd == 4} {$r rpop
$randkey}
1497 if {$cmd == 5} {$r del
$randkey}
1498 if {$cmd == 6} {$r llen
$randkey}
1499 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1500 if {$cmd == 8} {$r ltrim
$randkey $randidx0 $randidx1}
1501 if {$cmd == 9} {$r lindex $randkey $randidx0}
1502 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1503 if {$cmd == 11} {$r sadd
$randkey $randval}
1504 if {$cmd == 12} {$r srem
$randkey $randval}
1505 if {$cmd == 13} {$r smove
$randkey $randval}
1506 if {$cmd == 14} {$r scard
$randkey}
1507 if {$cmd == 15} {$r expire
$randkey [expr $randval%60]}
1515 # Set a few configuration defaults
1516 set ::host 127.0.0.1
1524 for {set j
0} {$j < [llength $argv]} {incr j
} {
1525 set opt
[lindex $argv $j]
1526 set arg
[lindex $argv [expr $j+1]]
1527 set lastarg
[expr {$arg eq
{}}]
1528 if {$opt eq
{-h} && !$lastarg} {
1531 } elseif
{$opt eq
{-p} && !$lastarg} {
1534 } elseif
{$opt eq
{-stress}} {
1536 } elseif
{$opt eq
{--flush}} {
1538 } elseif
{$opt eq
{--first} && !$lastarg} {
1541 } elseif
{$opt eq
{--last} && !$lastarg} {
1545 puts "Wrong argument: $opt"
1550 # Before to run the test check if DB 9 and DB 10 are empty
1558 set db9size
[$r dbsize
]
1560 set db10size
[$r dbsize
]
1561 if {$db9size != 0 ||
$db10size != 0} {
1562 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1573 main
$::host $::port