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 randomInt
{max
} {
69 expr {int
(rand
()*$max)}
73 set path
[expr {int
(rand
()*[llength $args])}]
74 uplevel 1 [lindex $args $path]
79 # Small enough to likely collide
82 # 32 bit compressible signed/unsigned
83 randpath
{randomInt
2000000000} {randomInt
4000000000}
86 randpath
{randomInt
1000000000000}
89 randpath
{randstring
0 256 alpha
} \
90 {randstring
0 256 compr
} \
91 {randstring
0 256 binary}
97 # Small enough to likely collide
100 # 32 bit compressible signed/unsigned
101 randpath
{randomInt
2000000000} {randomInt
4000000000}
104 randpath
{randomInt
1000000000000}
107 randpath
{randstring
1 256 alpha
} \
108 {randstring
1 256 compr
}
112 proc createComplexDataset
{r ops
} {
113 for {set j
0} {$j < $ops} {incr j
} {
116 set d
[expr {rand
()}]
137 randpath
{$r lpush
$k $v} \
144 randpath
{$r sadd
$k $v} \
148 randpath
{$r zadd
$k $d $v} \
155 proc datasetDigest r
{
156 set keys
[lsort [split [$r keys
*] " "]]
157 set digest
[::sha1::sha1 -hex $keys]
161 {string} {set aux
[::sha1::sha1 -hex [$r get
$k]]} \
162 {list} {set aux
[::sha1::sha1 -hex [$r lrange $k 0 -1]]} \
163 {set} {set aux
[::sha1::sha1 -hex [$r smembers
$k]]} \
164 {zset
} {set aux
[::sha1::sha1 -hex [$r zrange
$k 0 -1]]}
167 set digest
[::sha1::sha1 -hex $aux]
172 proc main
{server port
} {
173 set r
[redis
$server $port]
178 # The following AUTH test should be enabled only when requirepass
179 # <PASSWORD> is set in redis.conf and redis-server was started with
180 # redis.conf as the first argument.
182 #test {AUTH with requirepass in redis.conf} {
186 test
{DEL all keys to start with a clean DB
} {
187 foreach key
[$r keys
*] {$r del
$key}
191 test
{SET and GET an item
} {
196 test
{SET and GET an empty item
} {
201 test
{DEL against a single item
} {
210 list [$r del foo1 foo2 foo3 foo4
] [$r mget foo1 foo2 foo3
]
213 test
{KEYS with pattern
} {
214 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
218 } {foo_a foo_b foo_c
}
220 test
{KEYS to get all keys
} {
222 } {foo_a foo_b foo_c key_x key_y key_z
}
228 test
{DEL all keys
} {
229 foreach key
[$r keys
*] {$r del
$key}
233 test
{Very big payload in GET
/SET
} {
234 set buf
[string repeat
"abcd" 1000000]
237 } [string repeat
"abcd" 1000000]
239 test
{SET
10000 numeric keys and access all them in reverse order
} {
240 for {set x
0} {$x < 10000} {incr x
} {
244 for {set x
9999} {$x >= 0} {incr x
-1} {
250 test
{DBSIZE should be
10001 now
} {
254 test
{INCR against non existing key
} {
256 append res
[$r incr novar
]
257 append res
[$r get novar
]
260 test
{INCR against key created by
incr itself
} {
264 test
{INCR against key originally
set with SET
} {
269 test
{INCR over
32bit value
} {
270 $r set novar
17179869184
274 test
{INCRBY over
32bit value with over
32bit increment
} {
275 $r set novar
17179869184
276 $r incrby novar
17179869184
279 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
280 $r set novar
17179869184
281 $r decrby novar
17179869185
284 test
{SETNX target key missing
} {
285 $r setnx novar2 foobared
289 test
{SETNX target key exists
} {
290 $r setnx novar2 blabla
294 test
{SETNX will overwrite EXPIREing key
} {
304 append res
[$r exists newkey
]
306 append res
[$r exists newkey
]
309 test
{Zero length value in key. SET
/GET
/EXISTS
} {
311 set res
[$r get emptykey
]
312 append res
[$r exists emptykey
]
314 append res
[$r exists emptykey
]
317 test
{Commands pipelining
} {
319 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
322 append res
[string match OK
* [::redis::redis_read_reply $fd]]
323 append res
[::redis::redis_read_reply $fd]
324 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
328 test
{Non existing command
} {
329 catch {$r foobaredcommand
} err
330 string match ERR
* $err
333 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
337 set res
[$r llen mylist
]
338 append res
[$r lindex mylist
0]
339 append res
[$r lindex mylist
1]
340 append res
[$r lindex mylist
2]
348 test
{Create a long
list and check every single element with LINDEX
} {
350 for {set i
0} {$i < 1000} {incr i
} {
353 for {set i
0} {$i < 1000} {incr i
} {
354 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
355 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
362 test
{Test elements with LINDEX in random access
} {
364 for {set i
0} {$i < 1000} {incr i
} {
365 set rint
[expr int
(rand
()*1000)]
366 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
367 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
374 test
{Check
if the
list is still ok
after a DEBUG RELOAD
} {
377 for {set i
0} {$i < 1000} {incr i
} {
378 set rint
[expr int
(rand
()*1000)]
379 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
380 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
387 test
{LLEN against non-list value
error} {
390 catch {$r llen mylist
} err
394 test
{LINDEX against non-list value
error} {
395 catch {$r lindex mylist
0} err
399 test
{LPUSH against non-list value
error} {
400 catch {$r lpush mylist
0} err
404 test
{RPUSH against non-list value
error} {
405 catch {$r rpush mylist
0} err
409 test
{RPOPLPUSH base case
} {
415 set v1
[$r rpoplpush mylist newlist
]
416 set v2
[$r rpoplpush mylist newlist
]
417 set l1
[$r lrange mylist
0 -1]
418 set l2
[$r lrange newlist
0 -1]
422 test
{RPOPLPUSH with the same
list as src and dst
} {
427 set l1
[$r lrange mylist
0 -1]
428 set v
[$r rpoplpush mylist mylist
]
429 set l2
[$r lrange mylist
0 -1]
431 } {{a b c
} c
{c a b
}}
433 test
{RPOPLPUSH target
list already exists
} {
441 set v1
[$r rpoplpush mylist newlist
]
442 set v2
[$r rpoplpush mylist newlist
]
443 set l1
[$r lrange mylist
0 -1]
444 set l2
[$r lrange newlist
0 -1]
446 } {d c
{a b
} {c d x
}}
448 test
{RPOPLPUSH against non existing key
} {
451 set v1
[$r rpoplpush mylist newlist
]
452 list $v1 [$r exists mylist
] [$r exists newlist
]
455 test
{RPOPLPUSH against non
list src key
} {
459 catch {$r rpoplpush mylist newlist
} err
460 list [$r type mylist
] [$r exists newlist
] [string range
$err 0 2]
463 test
{RPOPLPUSH against non
list dst key
} {
471 catch {$r rpoplpush mylist newlist
} err
472 list [$r lrange mylist
0 -1] [$r type newlist
] [string range
$err 0 2]
473 } {{a b c d
} string ERR
}
475 test
{RENAME basic usage
} {
477 $r rename mykey mykey1
478 $r rename mykey1 mykey2
482 test
{RENAME
source key should no longer exist
} {
486 test
{RENAME against already existing key
} {
489 $r rename mykey2 mykey
490 set res
[$r get mykey
]
491 append res
[$r exists mykey2
]
494 test
{RENAMENX basic usage
} {
498 $r renamenx mykey mykey2
499 set res
[$r get mykey2
]
500 append res
[$r exists mykey
]
503 test
{RENAMENX against already existing key
} {
506 $r renamenx mykey mykey2
509 test
{RENAMENX against already existing key
(2)} {
510 set res
[$r get mykey
]
511 append res
[$r get mykey2
]
514 test
{RENAME against non existing
source key
} {
515 catch {$r rename nokey foobar
} err
519 test
{RENAME where
source and dest key is the same
} {
520 catch {$r rename mykey mykey
} err
524 test
{DEL all keys again
(DB
0)} {
525 foreach key
[$r keys
*] {
531 test
{DEL all keys again
(DB
1)} {
533 foreach key
[$r keys
*] {
541 test
{MOVE basic usage
} {
545 lappend res
[$r exists mykey
]
546 lappend res
[$r dbsize
]
548 lappend res
[$r get mykey
]
549 lappend res
[$r dbsize
]
552 } [list 0 0 foobar
1]
554 test
{MOVE against key existing in the target DB
} {
559 test
{SET
/GET keys in different DBs
} {
567 lappend res
[$r get a
]
568 lappend res
[$r get b
]
570 lappend res
[$r get a
]
571 lappend res
[$r get b
]
574 } {hello world foo bared
}
576 test
{Basic LPOP
/RPOP
} {
581 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
584 test
{LPOP
/RPOP against empty
list} {
588 test
{LPOP against non
list value
} {
590 catch {$r lpop notalist
} err
594 test
{Mass LPUSH
/LPOP
} {
596 for {set i
0} {$i < 1000} {incr i
} {
601 for {set i
0} {$i < 500} {incr i
} {
602 incr sum2
[$r lpop mylist
]
603 incr sum2
[$r rpop mylist
]
608 test
{LRANGE basics
} {
609 for {set i
0} {$i < 10} {incr i
} {
612 list [$r lrange mylist
1 -2] \
613 [$r lrange mylist
-3 -1] \
614 [$r lrange mylist
4 4]
615 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
617 test
{LRANGE inverted indexes
} {
621 test
{LRANGE out of range indexes including the full
list} {
622 $r lrange mylist
-1000 1000
623 } {0 1 2 3 4 5 6 7 8 9}
625 test
{LRANGE against non existing key
} {
626 $r lrange nosuchkey
0 1
629 test
{LTRIM basics
} {
631 for {set i
0} {$i < 100} {incr i
} {
635 $r lrange mylist
0 -1
640 $r lset mylist
-1 bar
641 $r lrange mylist
0 -1
644 test
{LSET out of range index
} {
645 catch {$r lset mylist
10 foo
} err
649 test
{LSET against non existing key
} {
650 catch {$r lset nosuchkey
10 foo
} err
654 test
{LSET against non
list value
} {
656 catch {$r lset nolist
0 foo
} err
660 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
663 list [$r scard myset
] [$r sismember myset foo
] \
664 [$r sismember myset bar
] [$r sismember myset bla
] \
665 [lsort [$r smembers myset
]]
666 } {2 1 1 0 {bar foo
}}
668 test
{SADD adding the same element multiple times
} {
675 test
{SADD against non
set} {
676 catch {$r sadd mylist foo
} err
683 lsort [$r smembers myset
]
686 test
{Mass SADD and SINTER with two sets
} {
687 for {set i
0} {$i < 1000} {incr i
} {
689 $r sadd set2
[expr $i+995]
691 lsort [$r sinter set1 set2
]
692 } {995 996 997 998 999}
694 test
{SUNION with two sets
} {
695 lsort [$r sunion set1 set2
]
696 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
698 test
{SINTERSTORE with two sets
} {
699 $r sinterstore setres set1 set2
700 lsort [$r smembers setres
]
701 } {995 996 997 998 999}
703 test
{SINTERSTORE with two sets
, after a DEBUG RELOAD
} {
705 $r sinterstore setres set1 set2
706 lsort [$r smembers setres
]
707 } {995 996 997 998 999}
709 test
{SUNIONSTORE with two sets
} {
710 $r sunionstore setres set1 set2
711 lsort [$r smembers setres
]
712 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
714 test
{SUNIONSTORE against non existing keys
} {
716 list [$r sunionstore setres foo111 bar222
] [$r exists xxx
]
719 test
{SINTER against three sets
} {
724 lsort [$r sinter set1 set2 set3
]
727 test
{SINTERSTORE with three sets
} {
728 $r sinterstore setres set1 set2 set3
729 lsort [$r smembers setres
]
732 test
{SUNION with non existing keys
} {
733 lsort [$r sunion nokey1 set1 set2 nokey2
]
734 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
736 test
{SDIFF with two sets
} {
737 for {set i
5} {$i < 1000} {incr i
} {
740 lsort [$r sdiff set1 set4
]
743 test
{SDIFF with three sets
} {
745 lsort [$r sdiff set1 set4 set5
]
748 test
{SDIFFSTORE with three sets
} {
749 $r sdiffstore sres set1 set4 set5
750 lsort [$r smembers sres
]
758 list [lsort [list [$r spop myset
] [$r spop myset
] [$r spop myset
]]] [$r scard myset
]
761 test
{SAVE
- make sure there are all the types as values
} {
762 # Wait for a background saving in progress to terminate
764 $r lpush mysavelist hello
765 $r lpush mysavelist world
767 $r set mynormalkey
{blablablba
}
768 $r zadd mytestzset a
10
769 $r zadd mytestzset b
20
770 $r zadd mytestzset c
30
779 unset -nocomplain myset
781 for {set i
0} {$i < 100} {incr i
} {
782 set myset
([$r srandmember myset
]) 1
784 lsort [array names myset
]
787 test
{Create a random
list} {
789 array set seenrand
{}
790 for {set i
0} {$i < 10000} {incr i
} {
792 # Make sure all the weights are different because
793 # Redis does not use a stable sort but Tcl does.
794 set rint
[expr int
(rand
()*1000000)]
795 if {![info exists seenrand
($rint)]} break
797 set seenrand
($rint) x
799 $r set weight_
$i $rint
800 lappend tosort
[list $i $rint]
802 set sorted
[lsort -index 1 -real $tosort]
804 for {set i
0} {$i < 10000} {incr i
} {
805 lappend res
[lindex $sorted $i 0]
810 test
{SORT with BY against the newly created
list} {
811 $r sort tosort
{BY weight_
*}
814 test
{SORT direct
, numeric
, against the newly created
list} {
816 } [lsort -integer $res]
818 test
{SORT decreasing sort
} {
819 $r sort tosort
{DESC
}
820 } [lsort -decreasing -integer $res]
822 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
823 set start
[clock clicks
-milliseconds]
824 for {set i
0} {$i < 100} {incr i
} {
825 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
827 set elapsed
[expr [clock clicks
-milliseconds]-$start]
828 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
833 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
834 set start
[clock clicks
-milliseconds]
835 for {set i
0} {$i < 100} {incr i
} {
836 set sorted
[$r sort tosort
{LIMIT
0 10}]
838 set elapsed
[expr [clock clicks
-milliseconds]-$start]
839 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
844 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
845 set start
[clock clicks
-milliseconds]
846 for {set i
0} {$i < 100} {incr i
} {
847 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
849 set elapsed
[expr [clock clicks
-milliseconds]-$start]
850 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
855 test
{SORT regression
for issue
#19, sorting floats} {
857 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
861 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
863 test
{SORT with GET
#} {
868 $r mset weight_1
10 weight_2
5 weight_3
30
869 $r sort mylist BY weight_
* GET
#
872 test
{LREM
, remove all the occurrences
} {
876 $r rpush mylist foobar
877 $r rpush mylist foobared
882 set res
[$r lrem mylist
0 bar
]
883 list [$r lrange mylist
0 -1] $res
884 } {{foo foobar foobared zap test foo
} 2}
886 test
{LREM
, remove the first occurrence
} {
887 set res
[$r lrem mylist
1 foo
]
888 list [$r lrange mylist
0 -1] $res
889 } {{foobar foobared zap test foo
} 1}
891 test
{LREM
, remove non existing element
} {
892 set res
[$r lrem mylist
1 nosuchelement
]
893 list [$r lrange mylist
0 -1] $res
894 } {{foobar foobared zap test foo
} 0}
896 test
{LREM
, starting from tail with negative count
} {
900 $r rpush mylist foobar
901 $r rpush mylist foobared
907 set res
[$r lrem mylist
-1 bar
]
908 list [$r lrange mylist
0 -1] $res
909 } {{foo bar foobar foobared zap test foo foo
} 1}
911 test
{LREM
, starting from tail with negative count
(2)} {
912 set res
[$r lrem mylist
-2 foo
]
913 list [$r lrange mylist
0 -1] $res
914 } {{foo bar foobar foobared zap test
} 2}
916 test
{LREM
, deleting objects that may be encoded as integers
} {
917 $r lpush myotherlist
1
918 $r lpush myotherlist
2
919 $r lpush myotherlist
3
920 $r lrem myotherlist
1 2
931 test
{MGET against non existing key
} {
932 $r mget foo baazz bar
935 test
{MGET against non-string key
} {
938 $r mget foo baazz bar myset
947 for {set i
0} {$i < 100} {incr i
} {
948 set rkey
[$r randomkey
]
949 if {$rkey eq
{foo
}} {
952 if {$rkey eq
{bar
}} {
956 list $foo_seen $bar_seen
959 test
{RANDOMKEY against empty DB
} {
964 test
{RANDOMKEY regression
1} {
971 test
{GETSET
(set new value
)} {
972 list [$r getset foo xyz
] [$r get foo
]
975 test
{GETSET
(replace old value
)} {
977 list [$r getset foo xyz
] [$r get foo
]
980 test
{SMOVE basics
} {
987 $r smove myset1 myset2 a
988 list [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
991 test
{SMOVE non existing key
} {
992 list [$r smove myset1 myset2 foo
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
993 } {0 {a x y z
} {b c
}}
995 test
{SMOVE non existing src
set} {
996 list [$r smove noset myset2 foo
] [lsort [$r smembers myset2
]]
999 test
{SMOVE non existing dst
set} {
1000 list [$r smove myset2 myset3 y
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset3
]]
1003 test
{SMOVE wrong src key type
} {
1005 catch {$r smove x myset2 foo
} err
1009 test
{SMOVE wrong dst key type
} {
1011 catch {$r smove myset2 x foo
} err
1015 test
{MSET base case
} {
1016 $r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
1018 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
1020 test
{MSET wrong number of args
} {
1021 catch {$r mset x
10 y
"foo bar" z
} err
1025 test
{MSETNX with already existent key
} {
1026 list [$r msetnx x1 xxx y2 yyy x
20] [$r exists x1
] [$r exists y2
]
1029 test
{MSETNX with not existing keys
} {
1030 list [$r msetnx x1 xxx y2 yyy
] [$r get x1
] [$r get y2
]
1033 test
{MSETNX should remove all the volatile keys even on
failure} {
1037 list [$r msetnx x A y B z C
] [$r mget x y z
]
1040 test
{ZSET basic ZADD and score
update} {
1044 set aux1
[$r zrange ztmp
0 -1]
1046 set aux2
[$r zrange ztmp
0 -1]
1050 test
{ZCARD basics
} {
1054 test
{ZCARD non existing key
} {
1055 $r zcard ztmp-blabla
1061 for {set i
0} {$i < 1000} {incr i
} {
1062 set score
[expr rand
()]
1064 $r zadd zscoretest
$score $i
1066 for {set i
0} {$i < 1000} {incr i
} {
1067 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1068 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1075 test
{ZSCORE
after a DEBUG RELOAD
} {
1079 for {set i
0} {$i < 1000} {incr i
} {
1080 set score
[expr rand
()]
1082 $r zadd zscoretest
$score $i
1085 for {set i
0} {$i < 1000} {incr i
} {
1086 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1087 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1094 test
{ZRANGE and ZREVRANGE
} {
1095 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1]
1098 test
{ZSETs stress tester
- sorting is working well?
} {
1100 for {set test
0} {$test < 2} {incr test
} {
1101 unset -nocomplain auxarray
1102 array set auxarray
{}
1105 for {set i
0} {$i < 1000} {incr i
} {
1107 set score
[expr rand
()]
1109 set score
[expr int
(rand
()*10)]
1111 set auxarray
($i) $score
1112 $r zadd myzset
$score $i
1114 if {[expr rand
()] < .2} {
1115 set j
[expr int
(rand
()*1000)]
1117 set score
[expr rand
()]
1119 set score
[expr int
(rand
()*10)]
1121 set auxarray
($j) $score
1122 $r zadd myzset
$score $j
1125 foreach {item score
} [array get auxarray
] {
1126 lappend auxlist
[list $score $item]
1128 set sorted
[lsort -command zlistAlikeSort
$auxlist]
1131 lappend auxlist
[lindex $x 1]
1133 set fromredis
[$r zrange myzset
0 -1]
1135 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
1136 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1144 test
{ZINCRBY
- can create a new sorted
set} {
1146 $r zincrby zset
1 foo
1147 list [$r zrange zset
0 -1] [$r zscore zset foo
]
1150 test
{ZINCRBY
- increment and decrement
} {
1151 $r zincrby zset
2 foo
1152 $r zincrby zset
1 bar
1153 set v1
[$r zrange zset
0 -1]
1154 $r zincrby zset
10 bar
1155 $r zincrby zset
-5 foo
1156 $r zincrby zset
-5 bar
1157 set v2
[$r zrange zset
0 -1]
1158 list $v1 $v2 [$r zscore zset foo
] [$r zscore zset bar
]
1159 } {{bar foo
} {foo bar
} -2 6}
1161 test
{ZRANGEBYSCORE basics
} {
1168 $r zrangebyscore zset
2 4
1171 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
1174 for {set i
0} {$i < 1000} {incr i
} {
1175 $r zadd zset
[expr rand
()] $i
1177 for {set i
0} {$i < 100} {incr i
} {
1178 set min
[expr rand
()]
1179 set max
[expr rand
()]
1185 set low
[$r zrangebyscore zset
-inf $min]
1186 set ok
[$r zrangebyscore zset
$min $max]
1187 set high
[$r zrangebyscore zset
$max +inf
]
1189 set score
[$r zscore zset
$x]
1190 if {$score > $min} {
1191 append err
"Error, score for $x is $score > $min\n"
1195 set score
[$r zscore zset
$x]
1196 if {$score < $min ||
$score > $max} {
1197 append err
"Error, score for $x is $score outside $min-$max range\n"
1201 set score
[$r zscore zset
$x]
1202 if {$score < $max} {
1203 append err
"Error, score for $x is $score < $max\n"
1210 test
{ZRANGEBYSCORE with LIMIT
} {
1218 [$r zrangebyscore zset
0 10 LIMIT
0 2] \
1219 [$r zrangebyscore zset
0 10 LIMIT
2 3] \
1220 [$r zrangebyscore zset
0 10 LIMIT
2 10] \
1221 [$r zrangebyscore zset
0 10 LIMIT
20 10]
1222 } {{a b
} {c d e
} {c d e
} {}}
1224 test
{ZREMRANGE basics
} {
1231 list [$r zremrangebyscore zset
2 4] [$r zrange zset
0 -1]
1234 test
{ZREMRANGE from
-inf to
+inf
} {
1241 list [$r zremrangebyscore zset
-inf +inf
] [$r zrange zset
0 -1]
1244 test
{SORT against sorted sets
} {
1251 $r sort zset alpha desc
1254 test
{Sorted sets
+inf and
-inf handling
} {
1259 $r zadd zset
1000000 d
1260 $r zadd zset
+inf max
1261 $r zadd zset
-inf min
1265 test
{EXPIRE
- don't
set timeouts multiple times
} {
1267 set v1
[$r expire x
5]
1269 set v3
[$r expire x
10]
1271 list $v1 $v2 $v3 $v4
1274 test
{EXPIRE
- It should be still possible to
read 'x'
} {
1278 test
{EXPIRE
- After
6 seconds the key should no longer be here
} {
1280 list [$r get x
] [$r exists x
]
1283 test
{EXPIRE
- Delete on write policy
} {
1291 test
{EXPIREAT
- Check
for EXPIRE alike behavior
} {
1294 $r expireat x
[expr [clock seconds
]+15]
1298 test
{ZSETs skiplist implementation backlink consistency test
} {
1301 for {set j
0} {$j < $elements} {incr j
} {
1302 $r zadd myzset
[expr rand
()] "Element-$j"
1303 $r zrem myzset
"Element-[expr int(rand()*$elements)]"
1305 set l1
[$r zrange myzset
0 -1]
1306 set l2
[$r zrevrange myzset
0 -1]
1307 for {set j
0} {$j < [llength $l1]} {incr j
} {
1308 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
1315 foreach fuzztype
{binary alpha compr
} {
1316 test
"FUZZ stresser with data model $fuzztype" {
1318 for {set i
0} {$i < 10000} {incr i
} {
1319 set fuzz
[randstring
0 512 $fuzztype]
1321 set got
[$r get foo
]
1322 if {$got ne
$fuzz} {
1323 set err
[list $fuzz $got]
1341 test
{Handle an empty query well
} {
1343 puts -nonewline $fd "\r\n"
1348 test
{Negative multi bulk command does not create problems
} {
1350 puts -nonewline $fd "*-10\r\n"
1355 test
{Negative multi bulk payload
} {
1357 puts -nonewline $fd "SET x -10\r\n"
1362 test
{Too big bulk payload
} {
1364 puts -nonewline $fd "SET x 2000000000\r\n"
1367 } {*invalid bulk
*count
*}
1369 test
{Multi bulk request not followed by bulk args
} {
1371 puts -nonewline $fd "*1\r\nfoo\r\n"
1374 } {*protocol
error*}
1376 test
{Generic wrong number of args
} {
1377 catch {$r ping x y z
} err
1379 } {*wrong
*arguments
*ping
*}
1381 test
{SELECT an out of range DB
} {
1382 catch {$r select
1000000} err
1386 if {![catch {package require sha1
}]} {
1387 test
{Check consistency of different data types
after a reload
} {
1389 createComplexDataset
$r 10000
1390 set sha1
[datasetDigest
$r]
1392 set sha1_after
[datasetDigest
$r]
1393 expr {$sha1 eq
$sha1_after}
1397 test
{EXPIRES
after a reload
} {
1404 expr {$ttl > 900 && $ttl <= 1000}
1407 # Leave the user with a clean DB before to exit
1412 lappend aux
[$r dbsize
]
1415 lappend aux
[$r dbsize
]
1418 test
{Perform a final SAVE to leave a clean DB on disk
} {
1423 if {[string match
{*Darwin
*} [exec uname
-a]]} {
1424 test
{Check
for memory leaks
} {
1425 exec leaks redis-server
1430 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1431 if {$::failed > 0} {
1432 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1441 set randkey
[expr int
(rand
()*10000)]
1442 set randval
[expr int
(rand
()*10000)]
1443 set randidx0
[expr int
(rand
()*10)]
1444 set randidx1
[expr int
(rand
()*10)]
1445 set cmd
[expr int
(rand
()*20)]
1447 if {$cmd == 0} {$r set $randkey $randval}
1448 if {$cmd == 1} {$r get
$randkey}
1449 if {$cmd == 2} {$r incr $randkey}
1450 if {$cmd == 3} {$r lpush
$randkey $randval}
1451 if {$cmd == 4} {$r rpop
$randkey}
1452 if {$cmd == 5} {$r del
$randkey}
1453 if {$cmd == 6} {$r llen
$randkey}
1454 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1455 if {$cmd == 8} {$r ltrim
$randkey $randidx0 $randidx1}
1456 if {$cmd == 9} {$r lindex $randkey $randidx0}
1457 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1458 if {$cmd == 11} {$r sadd
$randkey $randval}
1459 if {$cmd == 12} {$r srem
$randkey $randval}
1460 if {$cmd == 13} {$r smove
$randkey $randval}
1461 if {$cmd == 14} {$r scard
$randkey}
1462 if {$cmd == 15} {$r expire
$randkey [expr $randval%60]}
1470 # Set a few configuration defaults
1471 set ::host 127.0.0.1
1479 for {set j
0} {$j < [llength $argv]} {incr j
} {
1480 set opt
[lindex $argv $j]
1481 set arg
[lindex $argv [expr $j+1]]
1482 set lastarg
[expr {$arg eq
{}}]
1483 if {$opt eq
{-h} && !$lastarg} {
1486 } elseif
{$opt eq
{-p} && !$lastarg} {
1489 } elseif
{$opt eq
{-stress}} {
1491 } elseif
{$opt eq
{--flush}} {
1493 } elseif
{$opt eq
{--first} && !$lastarg} {
1496 } elseif
{$opt eq
{--last} && !$lastarg} {
1500 puts "Wrong argument: $opt"
1505 # Before to run the test check if DB 9 and DB 10 are empty
1513 set db9size
[$r dbsize
]
1515 set db10size
[$r dbsize
]
1516 if {$db9size != 0 ||
$db10size != 0} {
1517 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1528 main
$::host $::port