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 [$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: $t"
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
{Very big payload random access
} {
286 for {set j
0} {$j < 100} {incr j
} {
287 set size
[expr 1+[randomInt
100000]]
288 set buf
[string repeat
"pl-$j" $size]
290 $r set bigpayload_
$j $buf
292 for {set j
0} {$j < 1000} {incr j
} {
293 set index
[randomInt
100]
294 set buf
[$r get bigpayload_
$index]
295 if {$buf != $payload($index)} {
296 set err
"Values differ: I set '$payload($index)' but I read back '$buf'"
304 test
{SET
10000 numeric keys and access all them in reverse order
} {
306 for {set x
0} {$x < 10000} {incr x
} {
310 for {set x
9999} {$x >= 0} {incr x
-1} {
313 set err
"Eleemnt at position $x is $val instead of $x"
320 test
{DBSIZE should be
10101 now
} {
324 test
{INCR against non existing key
} {
326 append res
[$r incr novar
]
327 append res
[$r get novar
]
330 test
{INCR against key created by
incr itself
} {
334 test
{INCR against key originally
set with SET
} {
339 test
{INCR over
32bit value
} {
340 $r set novar
17179869184
344 test
{INCRBY over
32bit value with over
32bit increment
} {
345 $r set novar
17179869184
346 $r incrby novar
17179869184
349 test
{INCR against key with spaces
(no integer encoded
)} {
354 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
355 $r set novar
17179869184
356 $r decrby novar
17179869185
359 test
{SETNX target key missing
} {
360 $r setnx novar2 foobared
364 test
{SETNX target key exists
} {
365 $r setnx novar2 blabla
369 test
{SETNX will overwrite EXPIREing key
} {
379 append res
[$r exists newkey
]
381 append res
[$r exists newkey
]
384 test
{Zero length value in key. SET
/GET
/EXISTS
} {
386 set res
[$r get emptykey
]
387 append res
[$r exists emptykey
]
389 append res
[$r exists emptykey
]
392 test
{Commands pipelining
} {
394 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
397 append res
[string match OK
* [::redis::redis_read_reply $fd]]
398 append res
[::redis::redis_read_reply $fd]
399 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
403 test
{Non existing command
} {
404 catch {$r foobaredcommand
} err
405 string match ERR
* $err
408 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
409 set res
[$r lpush mylist a
]
410 append res
[$r lpush mylist b
]
411 append res
[$r rpush mylist c
]
412 append res
[$r llen mylist
]
413 append res
[$r rpush anotherlist d
]
414 append res
[$r lpush anotherlist e
]
415 append res
[$r llen anotherlist
]
416 append res
[$r lindex mylist
0]
417 append res
[$r lindex mylist
1]
418 append res
[$r lindex mylist
2]
419 append res
[$r lindex anotherlist
0]
420 append res
[$r lindex anotherlist
1]
421 list $res [$r lindex mylist
100]
429 test
{Create a long
list and check every single element with LINDEX
} {
431 for {set i
0} {$i < 1000} {incr i
} {
434 for {set i
0} {$i < 1000} {incr i
} {
435 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
436 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
443 test
{Test elements with LINDEX in random access
} {
445 for {set i
0} {$i < 1000} {incr i
} {
446 set rint
[expr int
(rand
()*1000)]
447 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
448 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
455 test
{Check
if the
list is still ok
after a DEBUG RELOAD
} {
458 for {set i
0} {$i < 1000} {incr i
} {
459 set rint
[expr int
(rand
()*1000)]
460 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
461 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
468 test
{LLEN against non-list value
error} {
471 catch {$r llen mylist
} err
475 test
{LLEN against non existing key
} {
479 test
{LINDEX against non-list value
error} {
480 catch {$r lindex mylist
0} err
484 test
{LINDEX against non existing key
} {
485 $r lindex not-a-key
10
488 test
{LPUSH against non-list value
error} {
489 catch {$r lpush mylist
0} err
493 test
{RPUSH against non-list value
error} {
494 catch {$r rpush mylist
0} err
498 test
{RPOPLPUSH base case
} {
504 set v1
[$r rpoplpush mylist newlist
]
505 set v2
[$r rpoplpush mylist newlist
]
506 set l1
[$r lrange mylist
0 -1]
507 set l2
[$r lrange newlist
0 -1]
511 test
{RPOPLPUSH with the same
list as src and dst
} {
516 set l1
[$r lrange mylist
0 -1]
517 set v
[$r rpoplpush mylist mylist
]
518 set l2
[$r lrange mylist
0 -1]
520 } {{a b c
} c
{c a b
}}
522 test
{RPOPLPUSH target
list already exists
} {
530 set v1
[$r rpoplpush mylist newlist
]
531 set v2
[$r rpoplpush mylist newlist
]
532 set l1
[$r lrange mylist
0 -1]
533 set l2
[$r lrange newlist
0 -1]
535 } {d c
{a b
} {c d x
}}
537 test
{RPOPLPUSH against non existing key
} {
540 set v1
[$r rpoplpush mylist newlist
]
541 list $v1 [$r exists mylist
] [$r exists newlist
]
544 test
{RPOPLPUSH against non
list src key
} {
548 catch {$r rpoplpush mylist newlist
} err
549 list [$r type mylist
] [$r exists newlist
] [string range
$err 0 2]
552 test
{RPOPLPUSH against non
list dst key
} {
560 catch {$r rpoplpush mylist newlist
} err
561 list [$r lrange mylist
0 -1] [$r type newlist
] [string range
$err 0 2]
562 } {{a b c d
} string ERR
}
564 test
{RPOPLPUSH against non existing src key
} {
567 $r rpoplpush mylist newlist
570 test
{RENAME basic usage
} {
572 $r rename mykey mykey1
573 $r rename mykey1 mykey2
577 test
{RENAME
source key should no longer exist
} {
581 test
{RENAME against already existing key
} {
584 $r rename mykey2 mykey
585 set res
[$r get mykey
]
586 append res
[$r exists mykey2
]
589 test
{RENAMENX basic usage
} {
593 $r renamenx mykey mykey2
594 set res
[$r get mykey2
]
595 append res
[$r exists mykey
]
598 test
{RENAMENX against already existing key
} {
601 $r renamenx mykey mykey2
604 test
{RENAMENX against already existing key
(2)} {
605 set res
[$r get mykey
]
606 append res
[$r get mykey2
]
609 test
{RENAME against non existing
source key
} {
610 catch {$r rename nokey foobar
} err
614 test
{RENAME where
source and dest key is the same
} {
615 catch {$r rename mykey mykey
} err
619 test
{DEL all keys again
(DB
0)} {
620 foreach key
[$r keys
*] {
626 test
{DEL all keys again
(DB
1)} {
628 foreach key
[$r keys
*] {
636 test
{MOVE basic usage
} {
640 lappend res
[$r exists mykey
]
641 lappend res
[$r dbsize
]
643 lappend res
[$r get mykey
]
644 lappend res
[$r dbsize
]
647 } [list 0 0 foobar
1]
649 test
{MOVE against key existing in the target DB
} {
654 test
{SET
/GET keys in different DBs
} {
662 lappend res
[$r get a
]
663 lappend res
[$r get b
]
665 lappend res
[$r get a
]
666 lappend res
[$r get b
]
669 } {hello world foo bared
}
671 test
{Basic LPOP
/RPOP
} {
676 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
679 test
{LPOP
/RPOP against empty
list} {
683 test
{LPOP against non
list value
} {
685 catch {$r lpop notalist
} err
689 test
{Mass LPUSH
/LPOP
} {
691 for {set i
0} {$i < 1000} {incr i
} {
696 for {set i
0} {$i < 500} {incr i
} {
697 incr sum2
[$r lpop mylist
]
698 incr sum2
[$r rpop mylist
]
703 test
{LRANGE basics
} {
704 for {set i
0} {$i < 10} {incr i
} {
707 list [$r lrange mylist
1 -2] \
708 [$r lrange mylist
-3 -1] \
709 [$r lrange mylist
4 4]
710 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
712 test
{LRANGE inverted indexes
} {
716 test
{LRANGE out of range indexes including the full
list} {
717 $r lrange mylist
-1000 1000
718 } {0 1 2 3 4 5 6 7 8 9}
720 test
{LRANGE against non existing key
} {
721 $r lrange nosuchkey
0 1
724 test
{LTRIM basics
} {
726 for {set i
0} {$i < 100} {incr i
} {
730 $r lrange mylist
0 -1
733 test
{LTRIM stress testing
} {
736 for {set i
0} {$i < 20} {incr i
} {
740 for {set j
0} {$j < 100} {incr j
} {
743 for {set i
0} {$i < 20} {incr i
} {
749 $r ltrim mylist
$a $b
750 if {[$r lrange mylist
0 -1] ne
[lrange $mylist $a $b]} {
751 set err
"[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
760 foreach x
{99 98 97 96 95} {
764 $r lset mylist
-1 bar
765 $r lrange mylist
0 -1
768 test
{LSET out of range index
} {
769 catch {$r lset mylist
10 foo
} err
773 test
{LSET against non existing key
} {
774 catch {$r lset nosuchkey
10 foo
} err
778 test
{LSET against non
list value
} {
780 catch {$r lset nolist
0 foo
} err
784 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
787 list [$r scard myset
] [$r sismember myset foo
] \
788 [$r sismember myset bar
] [$r sismember myset bla
] \
789 [lsort [$r smembers myset
]]
790 } {2 1 1 0 {bar foo
}}
792 test
{SADD adding the same element multiple times
} {
799 test
{SADD against non
set} {
800 catch {$r sadd mylist foo
} err
807 lsort [$r smembers myset
]
810 test
{Mass SADD and SINTER with two sets
} {
811 for {set i
0} {$i < 1000} {incr i
} {
813 $r sadd set2
[expr $i+995]
815 lsort [$r sinter set1 set2
]
816 } {995 996 997 998 999}
818 test
{SUNION with two sets
} {
819 lsort [$r sunion set1 set2
]
820 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
822 test
{SINTERSTORE with two sets
} {
823 $r sinterstore setres set1 set2
824 lsort [$r smembers setres
]
825 } {995 996 997 998 999}
827 test
{SINTERSTORE with two sets
, after a DEBUG RELOAD
} {
829 $r sinterstore setres set1 set2
830 lsort [$r smembers setres
]
831 } {995 996 997 998 999}
833 test
{SUNIONSTORE with two sets
} {
834 $r sunionstore setres set1 set2
835 lsort [$r smembers setres
]
836 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
838 test
{SUNIONSTORE against non existing keys
} {
840 list [$r sunionstore setres foo111 bar222
] [$r exists xxx
]
843 test
{SINTER against three sets
} {
848 lsort [$r sinter set1 set2 set3
]
851 test
{SINTERSTORE with three sets
} {
852 $r sinterstore setres set1 set2 set3
853 lsort [$r smembers setres
]
856 test
{SUNION with non existing keys
} {
857 lsort [$r sunion nokey1 set1 set2 nokey2
]
858 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
860 test
{SDIFF with two sets
} {
861 for {set i
5} {$i < 1000} {incr i
} {
864 lsort [$r sdiff set1 set4
]
867 test
{SDIFF with three sets
} {
869 lsort [$r sdiff set1 set4 set5
]
872 test
{SDIFFSTORE with three sets
} {
873 $r sdiffstore sres set1 set4 set5
874 lsort [$r smembers sres
]
882 list [lsort [list [$r spop myset
] [$r spop myset
] [$r spop myset
]]] [$r scard myset
]
885 test
{SAVE
- make sure there are all the types as values
} {
886 # Wait for a background saving in progress to terminate
888 $r lpush mysavelist hello
889 $r lpush mysavelist world
891 $r set mynormalkey
{blablablba
}
892 $r zadd mytestzset a
10
893 $r zadd mytestzset b
20
894 $r zadd mytestzset c
30
903 unset -nocomplain myset
905 for {set i
0} {$i < 100} {incr i
} {
906 set myset
([$r srandmember myset
]) 1
908 lsort [array names myset
]
911 test
{Create a random
list and a random
set} {
913 array set seenrand
{}
914 for {set i
0} {$i < 10000} {incr i
} {
916 # Make sure all the weights are different because
917 # Redis does not use a stable sort but Tcl does.
919 set rint
[expr int
(rand
()*1000000)]
921 set rint
[expr rand
()]
923 if {![info exists seenrand
($rint)]} break
925 set seenrand
($rint) x
927 $r sadd tosort-set
$i
928 $r set weight_
$i $rint
929 lappend tosort
[list $i $rint]
931 set sorted
[lsort -index 1 -real $tosort]
933 for {set i
0} {$i < 10000} {incr i
} {
934 lappend res
[lindex $sorted $i 0]
939 test
{SORT with BY against the newly created
list} {
940 $r sort tosort
{BY weight_
*}
943 test
{the same SORT with BY
, but against the newly created
set} {
944 $r sort tosort-set
{BY weight_
*}
947 test
{SORT with BY and STORE against the newly created
list} {
948 $r sort tosort
{BY weight_
*} store sort-res
949 $r lrange sort-res
0 -1
952 test
{SORT direct
, numeric
, against the newly created
list} {
954 } [lsort -integer $res]
956 test
{SORT decreasing sort
} {
957 $r sort tosort
{DESC
}
958 } [lsort -decreasing -integer $res]
960 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
961 set start
[clock clicks
-milliseconds]
962 for {set i
0} {$i < 100} {incr i
} {
963 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
965 set elapsed
[expr [clock clicks
-milliseconds]-$start]
966 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
971 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
972 set start
[clock clicks
-milliseconds]
973 for {set i
0} {$i < 100} {incr i
} {
974 set sorted
[$r sort tosort
{LIMIT
0 10}]
976 set elapsed
[expr [clock clicks
-milliseconds]-$start]
977 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
982 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
983 set start
[clock clicks
-milliseconds]
984 for {set i
0} {$i < 100} {incr i
} {
985 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
987 set elapsed
[expr [clock clicks
-milliseconds]-$start]
988 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
993 test
{SORT regression
for issue
#19, sorting floats} {
995 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
999 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
1001 test
{SORT with GET
#} {
1006 $r mset weight_1
10 weight_2
5 weight_3
30
1007 $r sort mylist BY weight_
* GET
#
1010 test
{SORT with constant GET
} {
1011 $r sort mylist GET foo
1014 test
{LREM
, remove all the occurrences
} {
1018 $r rpush mylist foobar
1019 $r rpush mylist foobared
1022 $r rpush mylist test
1024 set res
[$r lrem mylist
0 bar
]
1025 list [$r lrange mylist
0 -1] $res
1026 } {{foo foobar foobared zap test foo
} 2}
1028 test
{LREM
, remove the first occurrence
} {
1029 set res
[$r lrem mylist
1 foo
]
1030 list [$r lrange mylist
0 -1] $res
1031 } {{foobar foobared zap test foo
} 1}
1033 test
{LREM
, remove non existing element
} {
1034 set res
[$r lrem mylist
1 nosuchelement
]
1035 list [$r lrange mylist
0 -1] $res
1036 } {{foobar foobared zap test foo
} 0}
1038 test
{LREM
, starting from tail with negative count
} {
1042 $r rpush mylist foobar
1043 $r rpush mylist foobared
1046 $r rpush mylist test
1049 set res
[$r lrem mylist
-1 bar
]
1050 list [$r lrange mylist
0 -1] $res
1051 } {{foo bar foobar foobared zap test foo foo
} 1}
1053 test
{LREM
, starting from tail with negative count
(2)} {
1054 set res
[$r lrem mylist
-2 foo
]
1055 list [$r lrange mylist
0 -1] $res
1056 } {{foo bar foobar foobared zap test
} 2}
1058 test
{LREM
, deleting objects that may be encoded as integers
} {
1059 $r lpush myotherlist
1
1060 $r lpush myotherlist
2
1061 $r lpush myotherlist
3
1062 $r lrem myotherlist
1 2
1073 test
{MGET against non existing key
} {
1074 $r mget foo baazz bar
1077 test
{MGET against non-string key
} {
1080 $r mget foo baazz bar myset
1089 for {set i
0} {$i < 100} {incr i
} {
1090 set rkey
[$r randomkey
]
1091 if {$rkey eq
{foo
}} {
1094 if {$rkey eq
{bar
}} {
1098 list $foo_seen $bar_seen
1101 test
{RANDOMKEY against empty DB
} {
1106 test
{RANDOMKEY regression
1} {
1113 test
{GETSET
(set new value
)} {
1114 list [$r getset foo xyz
] [$r get foo
]
1117 test
{GETSET
(replace old value
)} {
1119 list [$r getset foo xyz
] [$r get foo
]
1122 test
{SMOVE basics
} {
1129 $r smove myset1 myset2 a
1130 list [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1133 test
{SMOVE non existing key
} {
1134 list [$r smove myset1 myset2 foo
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
1135 } {0 {a x y z
} {b c
}}
1137 test
{SMOVE non existing src
set} {
1138 list [$r smove noset myset2 foo
] [lsort [$r smembers myset2
]]
1141 test
{SMOVE non existing dst
set} {
1142 list [$r smove myset2 myset3 y
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset3
]]
1145 test
{SMOVE wrong src key type
} {
1147 catch {$r smove x myset2 foo
} err
1151 test
{SMOVE wrong dst key type
} {
1153 catch {$r smove myset2 x foo
} err
1157 test
{MSET base case
} {
1158 $r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
1160 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
1162 test
{MSET wrong number of args
} {
1163 catch {$r mset x
10 y
"foo bar" z
} err
1167 test
{MSETNX with already existent key
} {
1168 list [$r msetnx x1 xxx y2 yyy x
20] [$r exists x1
] [$r exists y2
]
1171 test
{MSETNX with not existing keys
} {
1172 list [$r msetnx x1 xxx y2 yyy
] [$r get x1
] [$r get y2
]
1175 test
{MSETNX should remove all the volatile keys even on
failure} {
1179 list [$r msetnx x A y B z C
] [$r mget x y z
]
1182 test
{ZSET basic ZADD and score
update} {
1186 set aux1
[$r zrange ztmp
0 -1]
1188 set aux2
[$r zrange ztmp
0 -1]
1192 test
{ZCARD basics
} {
1196 test
{ZCARD non existing key
} {
1197 $r zcard ztmp-blabla
1200 test
{ZRANK basics
} {
1201 $r zadd zranktmp
10 x
1202 $r zadd zranktmp
20 y
1203 $r zadd zranktmp
30 z
1204 list [$r zrank zranktmp x
] [$r zrank zranktmp y
] [$r zrank zranktmp z
]
1207 test
{ZRANK
- after deletion
} {
1209 list [$r zrank zranktmp x
] [$r zrank zranktmp z
]
1215 for {set i
0} {$i < 1000} {incr i
} {
1216 set score
[expr rand
()]
1218 $r zadd zscoretest
$score $i
1220 for {set i
0} {$i < 1000} {incr i
} {
1221 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1222 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1229 test
{ZSCORE
after a DEBUG RELOAD
} {
1233 for {set i
0} {$i < 1000} {incr i
} {
1234 set score
[expr rand
()]
1236 $r zadd zscoretest
$score $i
1239 for {set i
0} {$i < 1000} {incr i
} {
1240 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
1241 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1248 test
{ZRANGE and ZREVRANGE basics
} {
1249 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1] \
1250 [$r zrange ztmp
1 -1] [$r zrevrange ztmp
1 -1]
1251 } {{y x z
} {z x y
} {x z
} {x y
}}
1253 test
{ZRANGE WITHSCORES
} {
1254 $r zrange ztmp
0 -1 withscores
1257 test
{ZSETs stress tester
- sorting is working well?
} {
1259 for {set test
0} {$test < 2} {incr test
} {
1260 unset -nocomplain auxarray
1261 array set auxarray
{}
1264 for {set i
0} {$i < 1000} {incr i
} {
1266 set score
[expr rand
()]
1268 set score
[expr int
(rand
()*10)]
1270 set auxarray
($i) $score
1271 $r zadd myzset
$score $i
1273 if {[expr rand
()] < .2} {
1274 set j
[expr int
(rand
()*1000)]
1276 set score
[expr rand
()]
1278 set score
[expr int
(rand
()*10)]
1280 set auxarray
($j) $score
1281 $r zadd myzset
$score $j
1284 foreach {item score
} [array get auxarray
] {
1285 lappend auxlist
[list $score $item]
1287 set sorted
[lsort -command zlistAlikeSort
$auxlist]
1290 lappend auxlist
[lindex $x 1]
1292 set fromredis
[$r zrange myzset
0 -1]
1294 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
1295 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1303 test
{ZINCRBY
- can create a new sorted
set} {
1305 $r zincrby zset
1 foo
1306 list [$r zrange zset
0 -1] [$r zscore zset foo
]
1309 test
{ZINCRBY
- increment and decrement
} {
1310 $r zincrby zset
2 foo
1311 $r zincrby zset
1 bar
1312 set v1
[$r zrange zset
0 -1]
1313 $r zincrby zset
10 bar
1314 $r zincrby zset
-5 foo
1315 $r zincrby zset
-5 bar
1316 set v2
[$r zrange zset
0 -1]
1317 list $v1 $v2 [$r zscore zset foo
] [$r zscore zset bar
]
1318 } {{bar foo
} {foo bar
} -2 6}
1320 test
{ZRANGEBYSCORE and ZCOUNT basics
} {
1327 list [$r zrangebyscore zset
2 4] [$r zrangebyscore zset
(2 (4] \
1328 [$r zcount zset
2 4] [$r zcount zset
(2 (4]
1331 test
{ZRANGEBYSCORE withscores
} {
1338 $r zrangebyscore zset
2 4 withscores
1341 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
1344 for {set i
0} {$i < 1000} {incr i
} {
1345 $r zadd zset
[expr rand
()] $i
1347 for {set i
0} {$i < 100} {incr i
} {
1348 set min
[expr rand
()]
1349 set max
[expr rand
()]
1355 set low
[$r zrangebyscore zset
-inf $min]
1356 set ok
[$r zrangebyscore zset
$min $max]
1357 set high
[$r zrangebyscore zset
$max +inf
]
1358 set lowx
[$r zrangebyscore zset
-inf ($min]
1359 set okx
[$r zrangebyscore zset
($min ($max]
1360 set highx
[$r zrangebyscore zset
($max +inf
]
1362 if {[$r zcount zset
-inf $min] != [llength $low]} {
1363 append err
"Error, len does not match zcount\n"
1365 if {[$r zcount zset
$min $max] != [llength $ok]} {
1366 append err
"Error, len does not match zcount\n"
1368 if {[$r zcount zset
$max +inf
] != [llength $high]} {
1369 append err
"Error, len does not match zcount\n"
1371 if {[$r zcount zset
-inf ($min] != [llength $lowx]} {
1372 append err
"Error, len does not match zcount\n"
1374 if {[$r zcount zset
($min ($max] != [llength $okx]} {
1375 append err
"Error, len does not match zcount\n"
1377 if {[$r zcount zset
($max +inf
] != [llength $highx]} {
1378 append err
"Error, len does not match zcount\n"
1382 set score
[$r zscore zset
$x]
1383 if {$score > $min} {
1384 append err
"Error, score for $x is $score > $min\n"
1388 set score
[$r zscore zset
$x]
1389 if {$score >= $min} {
1390 append err
"Error, score for $x is $score >= $min\n"
1394 set score
[$r zscore zset
$x]
1395 if {$score < $min ||
$score > $max} {
1396 append err
"Error, score for $x is $score outside $min-$max range\n"
1400 set score
[$r zscore zset
$x]
1401 if {$score <= $min ||
$score >= $max} {
1402 append err
"Error, score for $x is $score outside $min-$max open range\n"
1406 set score
[$r zscore zset
$x]
1407 if {$score < $max} {
1408 append err
"Error, score for $x is $score < $max\n"
1412 set score
[$r zscore zset
$x]
1413 if {$score <= $max} {
1414 append err
"Error, score for $x is $score <= $max\n"
1421 test
{ZRANGEBYSCORE with LIMIT
} {
1429 [$r zrangebyscore zset
0 10 LIMIT
0 2] \
1430 [$r zrangebyscore zset
0 10 LIMIT
2 3] \
1431 [$r zrangebyscore zset
0 10 LIMIT
2 10] \
1432 [$r zrangebyscore zset
0 10 LIMIT
20 10]
1433 } {{a b
} {c d e
} {c d e
} {}}
1435 test
{ZRANGEBYSCORE with LIMIT and withscores
} {
1442 $r zrangebyscore zset
20 50 LIMIT
2 3 withscores
1445 test
{ZREMRANGEBYSCORE basics
} {
1452 list [$r zremrangebyscore zset
2 4] [$r zrange zset
0 -1]
1455 test
{ZREMRANGEBYSCORE from
-inf to
+inf
} {
1462 list [$r zremrangebyscore zset
-inf +inf
] [$r zrange zset
0 -1]
1465 test
{ZREMRANGEBYRANK basics
} {
1472 list [$r zremrangebyrank zset
1 3] [$r zrange zset
0 -1]
1475 test
{SORT against sorted sets
} {
1482 $r sort zset alpha desc
1485 test
{Sorted sets
+inf and
-inf handling
} {
1490 $r zadd zset
1000000 d
1491 $r zadd zset
+inf max
1492 $r zadd zset
-inf min
1496 test
{EXPIRE
- don't
set timeouts multiple times
} {
1498 set v1
[$r expire x
5]
1500 set v3
[$r expire x
10]
1502 list $v1 $v2 $v3 $v4
1505 test
{EXPIRE
- It should be still possible to
read 'x'
} {
1509 test
{EXPIRE
- After
6 seconds the key should no longer be here
} {
1511 list [$r get x
] [$r exists x
]
1514 test
{EXPIRE
- Delete on write policy
} {
1522 test
{EXPIREAT
- Check
for EXPIRE alike behavior
} {
1525 $r expireat x
[expr [clock seconds
]+15]
1529 test
{ZSETs skiplist implementation backlink consistency test
} {
1532 for {set j
0} {$j < $elements} {incr j
} {
1533 $r zadd myzset
[expr rand
()] "Element-$j"
1534 $r zrem myzset
"Element-[expr int(rand()*$elements)]"
1536 set l1
[$r zrange myzset
0 -1]
1537 set l2
[$r zrevrange myzset
0 -1]
1538 for {set j
0} {$j < [llength $l1]} {incr j
} {
1539 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
1546 test
{ZSETs ZRANK augmented skip
list stress testing
} {
1549 for {set k
0} {$k < 10000} {incr k
} {
1550 set i
[expr {$k%1000}]
1551 if {[expr rand
()] < .2} {
1554 set score
[expr rand
()]
1555 $r zadd myzset
$score $i
1557 set card
[$r zcard myzset
]
1559 set index
[randomInt
$card]
1560 set ele
[lindex [$r zrange myzset
$index $index] 0]
1561 set rank
[$r zrank myzset
$ele]
1562 if {$rank != $index} {
1563 set err
"$ele RANK is wrong! ($rank != $index)"
1571 foreach fuzztype
{binary alpha compr
} {
1572 test
"FUZZ stresser with data model $fuzztype" {
1574 for {set i
0} {$i < 10000} {incr i
} {
1575 set fuzz
[randstring
0 512 $fuzztype]
1577 set got
[$r get foo
]
1578 if {$got ne
$fuzz} {
1579 set err
[list $fuzz $got]
1598 test
{Handle an empty query well
} {
1600 puts -nonewline $fd "\r\n"
1605 test
{Negative multi bulk command does not create problems
} {
1607 puts -nonewline $fd "*-10\r\n"
1612 test
{Negative multi bulk payload
} {
1614 puts -nonewline $fd "SET x -10\r\n"
1619 test
{Too big bulk payload
} {
1621 puts -nonewline $fd "SET x 2000000000\r\n"
1624 } {*invalid bulk
*count
*}
1626 test
{Multi bulk request not followed by bulk args
} {
1628 puts -nonewline $fd "*1\r\nfoo\r\n"
1631 } {*protocol
error*}
1633 test
{Generic wrong number of args
} {
1634 catch {$r ping x y z
} err
1636 } {*wrong
*arguments
*ping
*}
1638 test
{SELECT an out of range DB
} {
1639 catch {$r select
1000000} err
1643 if {![catch {package require sha1
}]} {
1644 test
{Check consistency of different data types
after a reload
} {
1646 createComplexDataset
$r 10000
1647 set sha1
[datasetDigest
$r]
1649 set sha1_after
[datasetDigest
$r]
1650 expr {$sha1 eq
$sha1_after}
1653 test
{Same dataset digest
if saving
/reloading as AOF?
} {
1655 waitForBgrewriteaof
$r
1657 set sha1_after
[datasetDigest
$r]
1658 expr {$sha1 eq
$sha1_after}
1662 test
{EXPIRES
after a reload
(snapshot
+ append only
file)} {
1669 set e1
[expr {$ttl > 900 && $ttl <= 1000}]
1671 waitForBgrewriteaof
$r
1673 set e2
[expr {$ttl > 900 && $ttl <= 1000}]
1677 test
{PIPELINING stresser
(also a regression
for the old epoll bug
)} {
1678 set fd2
[socket 127.0.0.1 6379]
1679 fconfigure $fd2 -encoding binary -translation binary
1680 puts -nonewline $fd2 "SELECT 9\r\n"
1684 for {set i
0} {$i < 100000} {incr i
} {
1686 set val
"0000${i}0000"
1687 append q
"SET key:$i [string length $val]\r\n$val\r\n"
1688 puts -nonewline $fd2 $q
1690 append q
"GET key:$i\r\n"
1691 puts -nonewline $fd2 $q
1695 for {set i
0} {$i < 100000} {incr i
} {
1698 set count
[string range
$count 1 end
]
1699 set val
[read $fd2 $count]
1706 test
{MUTLI
/ EXEC basics
} {
1712 set v1
[$r lrange mylist
0 -1]
1716 } {QUEUED QUEUED
{{a b c
} PONG
}}
1724 set v1
[$r del mylist
]
1726 set v3
[$r lrange mylist
0 -1]
1728 } {QUEUED OK
{a b c
}}
1730 test
{APPEND basics
} {
1731 list [$r append foo bar
] [$r get foo
] \
1732 [$r append foo
100] [$r get foo
]
1735 test
{APPEND fuzzing
} {
1737 foreach type
{binary alpha compr
} {
1740 for {set i
0} {$i < 1000} {incr i
} {
1741 set bin
[randstring
0 10 $type]
1745 if {$buf != [$r get x
]} {
1746 set err
"Expected '$buf' found '[$r get x]'"
1753 # Leave the user with a clean DB before to exit
1758 lappend aux
[$r dbsize
]
1761 lappend aux
[$r dbsize
]
1764 test
{Perform a final SAVE to leave a clean DB on disk
} {
1769 if {[string match
{*Darwin
*} [exec uname
-a]]} {
1770 test
{Check
for memory leaks
} {
1771 exec leaks redis-server
1776 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1777 if {$::failed > 0} {
1778 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1787 set randkey
[expr int
(rand
()*10000)]
1788 set randval
[expr int
(rand
()*10000)]
1789 set randidx0
[expr int
(rand
()*10)]
1790 set randidx1
[expr int
(rand
()*10)]
1791 set cmd
[expr int
(rand
()*20)]
1793 if {$cmd == 0} {$r set $randkey $randval}
1794 if {$cmd == 1} {$r get
$randkey}
1795 if {$cmd == 2} {$r incr $randkey}
1796 if {$cmd == 3} {$r lpush
$randkey $randval}
1797 if {$cmd == 4} {$r rpop
$randkey}
1798 if {$cmd == 5} {$r del
$randkey}
1799 if {$cmd == 6} {$r llen
$randkey}
1800 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1801 if {$cmd == 8} {$r ltrim
$randkey $randidx0 $randidx1}
1802 if {$cmd == 9} {$r lindex $randkey $randidx0}
1803 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1804 if {$cmd == 11} {$r sadd
$randkey $randval}
1805 if {$cmd == 12} {$r srem
$randkey $randval}
1806 if {$cmd == 13} {$r smove
$randkey $randval}
1807 if {$cmd == 14} {$r scard
$randkey}
1808 if {$cmd == 15} {$r expire
$randkey [expr $randval%60]}
1816 # Set a few configuration defaults
1817 set ::host 127.0.0.1
1825 for {set j
0} {$j < [llength $argv]} {incr j
} {
1826 set opt
[lindex $argv $j]
1827 set arg
[lindex $argv [expr $j+1]]
1828 set lastarg
[expr {$arg eq
{}}]
1829 if {$opt eq
{-h} && !$lastarg} {
1832 } elseif
{$opt eq
{-p} && !$lastarg} {
1835 } elseif
{$opt eq
{-stress}} {
1837 } elseif
{$opt eq
{--flush}} {
1839 } elseif
{$opt eq
{--first} && !$lastarg} {
1842 } elseif
{$opt eq
{--last} && !$lastarg} {
1846 puts "Wrong argument: $opt"
1851 # Before to run the test check if DB 9 and DB 10 are empty
1859 set db9size
[$r dbsize
]
1861 set db10size
[$r dbsize
]
1862 if {$db9size != 0 ||
$db10size != 0} {
1863 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1874 main
$::host $::port