1 # TODO # test pipelining
9 proc test
{name code okpattern
} {
10 puts -nonewline [format "%-70s " $name]
12 set retval
[uplevel 1 $code]
13 if {$okpattern eq
$retval ||
[string match
$okpattern $retval]} {
17 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
22 proc randstring
{min max
{type
binary}} {
23 set len
[expr {$min+int
(rand
()*($max-$min+1))}]
25 if {$type eq
{binary}} {
28 } elseif
{$type eq
{alpha
}} {
31 } elseif
{$type eq
{compr
}} {
36 append output
[format "%c" [expr {$minval+int
(rand
()*($maxval-$minval+1))}]]
42 # Useful for some test
43 proc zlistAlikeSort
{a b
} {
44 if {[lindex $a 0] > [lindex $b 0]} {return 1}
45 if {[lindex $a 0] < [lindex $b 0]} {return -1}
46 string compare
[lindex $a 1] [lindex $b 1]
49 proc main
{server port
} {
50 set r
[redis
$server $port]
54 # The following AUTH test should be enabled only when requirepass
55 # <PASSWORD> is set in redis.conf and redis-server was started with
56 # redis.conf as the first argument.
58 #test {AUTH with requirepass in redis.conf} {
62 test
{DEL all keys to start with a clean DB
} {
63 foreach key
[$r keys
*] {$r del
$key}
67 test
{SET and GET an item
} {
72 test
{DEL against a single item
} {
81 list [$r del foo1 foo2 foo3 foo4
] [$r mget foo1 foo2 foo3
]
84 test
{KEYS with pattern
} {
85 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
91 test
{KEYS to get all keys
} {
93 } {foo_a foo_b foo_c key_x key_y key_z
}
100 foreach key
[$r keys
*] {$r del
$key}
104 test
{Very big payload in GET
/SET
} {
105 set buf
[string repeat
"abcd" 1000000]
108 } [string repeat
"abcd" 1000000]
110 test
{SET
10000 numeric keys and access all them in reverse order
} {
111 for {set x
0} {$x < 10000} {incr x
} {
115 for {set x
9999} {$x >= 0} {incr x
-1} {
121 test
{DBSIZE should be
10001 now
} {
125 test
{INCR against non existing key
} {
127 append res
[$r incr novar
]
128 append res
[$r get novar
]
131 test
{INCR against key created by
incr itself
} {
135 test
{INCR against key originally
set with SET
} {
140 test
{INCR over
32bit value
} {
141 $r set novar
17179869184
145 test
{INCRBY over
32bit value with over
32bit increment
} {
146 $r set novar
17179869184
147 $r incrby novar
17179869184
150 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
151 $r set novar
17179869184
152 $r decrby novar
17179869185
155 test
{SETNX target key missing
} {
156 $r setnx novar2 foobared
160 test
{SETNX target key exists
} {
161 $r setnx novar2 blabla
168 append res
[$r exists newkey
]
170 append res
[$r exists newkey
]
173 test
{Zero length value in key. SET
/GET
/EXISTS
} {
175 set res
[$r get emptykey
]
176 append res
[$r exists emptykey
]
178 append res
[$r exists emptykey
]
181 test
{Commands pipelining
} {
183 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
186 append res
[string match OK
* [::redis::redis_read_reply $fd]]
187 append res
[::redis::redis_read_reply $fd]
188 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
192 test
{Non existing command
} {
193 catch {$r foobaredcommand
} err
194 string match ERR
* $err
197 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
201 set res
[$r llen mylist
]
202 append res
[$r lindex mylist
0]
203 append res
[$r lindex mylist
1]
204 append res
[$r lindex mylist
2]
212 test
{Create a long
list and check every single element with LINDEX
} {
214 for {set i
0} {$i < 1000} {incr i
} {
217 for {set i
0} {$i < 1000} {incr i
} {
218 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
219 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
226 test
{Test elements with LINDEX in random access
} {
228 for {set i
0} {$i < 1000} {incr i
} {
229 set rint
[expr int
(rand
()*1000)]
230 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
231 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
238 test
{LLEN against non-list value
error} {
241 catch {$r llen mylist
} err
245 test
{LINDEX against non-list value
error} {
246 catch {$r lindex mylist
0} err
250 test
{LPUSH against non-list value
error} {
251 catch {$r lpush mylist
0} err
255 test
{RPUSH against non-list value
error} {
256 catch {$r rpush mylist
0} err
260 test
{RENAME basic usage
} {
262 $r rename mykey mykey1
263 $r rename mykey1 mykey2
267 test
{RENAME
source key should no longer exist
} {
271 test
{RENAME against already existing key
} {
274 $r rename mykey2 mykey
275 set res
[$r get mykey
]
276 append res
[$r exists mykey2
]
279 test
{RENAMENX basic usage
} {
283 $r renamenx mykey mykey2
284 set res
[$r get mykey2
]
285 append res
[$r exists mykey
]
288 test
{RENAMENX against already existing key
} {
291 $r renamenx mykey mykey2
294 test
{RENAMENX against already existing key
(2)} {
295 set res
[$r get mykey
]
296 append res
[$r get mykey2
]
299 test
{RENAME against non existing
source key
} {
300 catch {$r rename nokey foobar
} err
304 test
{RENAME where
source and dest key is the same
} {
305 catch {$r rename mykey mykey
} err
309 test
{DEL all keys again
(DB
0)} {
310 foreach key
[$r keys
*] {
316 test
{DEL all keys again
(DB
1)} {
318 foreach key
[$r keys
*] {
326 test
{MOVE basic usage
} {
330 lappend res
[$r exists mykey
]
331 lappend res
[$r dbsize
]
333 lappend res
[$r get mykey
]
334 lappend res
[$r dbsize
]
337 } [list 0 0 foobar
1]
339 test
{MOVE against key existing in the target DB
} {
344 test
{SET
/GET keys in different DBs
} {
352 lappend res
[$r get a
]
353 lappend res
[$r get b
]
355 lappend res
[$r get a
]
356 lappend res
[$r get b
]
359 } {hello world foo bared
}
361 test
{Basic LPOP
/RPOP
} {
366 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
369 test
{LPOP
/RPOP against empty
list} {
373 test
{LPOP against non
list value
} {
375 catch {$r lpop notalist
} err
379 test
{Mass LPUSH
/LPOP
} {
381 for {set i
0} {$i < 1000} {incr i
} {
386 for {set i
0} {$i < 500} {incr i
} {
387 incr sum2
[$r lpop mylist
]
388 incr sum2
[$r rpop mylist
]
393 test
{LRANGE basics
} {
394 for {set i
0} {$i < 10} {incr i
} {
397 list [$r lrange mylist
1 -2] \
398 [$r lrange mylist
-3 -1] \
399 [$r lrange mylist
4 4]
400 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
402 test
{LRANGE inverted indexes
} {
406 test
{LRANGE out of range indexes including the full
list} {
407 $r lrange mylist
-1000 1000
408 } {0 1 2 3 4 5 6 7 8 9}
410 test
{LRANGE against non existing key
} {
411 $r lrange nosuchkey
0 1
414 test
{LTRIM basics
} {
416 for {set i
0} {$i < 100} {incr i
} {
420 $r lrange mylist
0 -1
425 $r lset mylist
-1 bar
426 $r lrange mylist
0 -1
429 test
{LSET out of range index
} {
430 catch {$r lset mylist
10 foo
} err
434 test
{LSET against non existing key
} {
435 catch {$r lset nosuchkey
10 foo
} err
439 test
{LSET against non
list value
} {
441 catch {$r lset nolist
0 foo
} err
445 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
448 list [$r scard myset
] [$r sismember myset foo
] \
449 [$r sismember myset bar
] [$r sismember myset bla
] \
450 [lsort [$r smembers myset
]]
451 } {2 1 1 0 {bar foo
}}
453 test
{SADD adding the same element multiple times
} {
460 test
{SADD against non
set} {
461 catch {$r sadd mylist foo
} err
468 lsort [$r smembers myset
]
471 test
{Mass SADD and SINTER with two sets
} {
472 for {set i
0} {$i < 1000} {incr i
} {
474 $r sadd set2
[expr $i+995]
476 lsort [$r sinter set1 set2
]
477 } {995 996 997 998 999}
479 test
{SUNION with two sets
} {
480 lsort [$r sunion set1 set2
]
481 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
483 test
{SINTERSTORE with two sets
} {
484 $r sinterstore setres set1 set2
485 lsort [$r smembers setres
]
486 } {995 996 997 998 999}
488 test
{SUNIONSTORE with two sets
} {
489 $r sunionstore setres set1 set2
490 lsort [$r smembers setres
]
491 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
493 test
{SINTER against three sets
} {
498 lsort [$r sinter set1 set2 set3
]
501 test
{SINTERSTORE with three sets
} {
502 $r sinterstore setres set1 set2 set3
503 lsort [$r smembers setres
]
506 test
{SUNION with non existing keys
} {
507 lsort [$r sunion nokey1 set1 set2 nokey2
]
508 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
510 test
{SDIFF with two sets
} {
511 for {set i
5} {$i < 1000} {incr i
} {
514 lsort [$r sdiff set1 set4
]
517 test
{SDIFF with three sets
} {
519 lsort [$r sdiff set1 set4 set5
]
522 test
{SDIFFSTORE with three sets
} {
523 $r sdiffstore sres set1 set4 set5
524 lsort [$r smembers sres
]
532 list [lsort [list [$r spop myset
] [$r spop myset
] [$r spop myset
]]] [$r scard myset
]
535 test
{SAVE
- make sure there are all the types as values
} {
536 # Wait for a background saving in progress to terminate
539 if {[string match
{*bgsave_in_progress
:1*} $i]} {
540 puts -nonewline "\nWaiting for background save to finish... "
547 $r lpush mysavelist hello
548 $r lpush mysavelist world
550 $r set mynormalkey
{blablablba
}
551 $r zadd mytestzset a
10
552 $r zadd mytestzset b
20
553 $r zadd mytestzset c
30
557 test
{Create a random
list} {
559 array set seenrand
{}
560 for {set i
0} {$i < 10000} {incr i
} {
562 # Make sure all the weights are different because
563 # Redis does not use a stable sort but Tcl does.
564 set rint
[expr int
(rand
()*1000000)]
565 if {![info exists seenrand
($rint)]} break
567 set seenrand
($rint) x
569 $r set weight_
$i $rint
570 lappend tosort
[list $i $rint]
572 set sorted
[lsort -index 1 -real $tosort]
574 for {set i
0} {$i < 10000} {incr i
} {
575 lappend res
[lindex $sorted $i 0]
580 test
{SORT with BY against the newly created
list} {
581 $r sort tosort
{BY weight_
*}
584 test
{SORT direct
, numeric
, against the newly created
list} {
586 } [lsort -integer $res]
588 test
{SORT decreasing sort
} {
589 $r sort tosort
{DESC
}
590 } [lsort -decreasing -integer $res]
592 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
593 set start
[clock clicks
-milliseconds]
594 for {set i
0} {$i < 100} {incr i
} {
595 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
597 set elapsed
[expr [clock clicks
-milliseconds]-$start]
598 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
603 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
604 set start
[clock clicks
-milliseconds]
605 for {set i
0} {$i < 100} {incr i
} {
606 set sorted
[$r sort tosort
{LIMIT
0 10}]
608 set elapsed
[expr [clock clicks
-milliseconds]-$start]
609 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
614 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
615 set start
[clock clicks
-milliseconds]
616 for {set i
0} {$i < 100} {incr i
} {
617 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
619 set elapsed
[expr [clock clicks
-milliseconds]-$start]
620 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
625 test
{SORT regression
for issue
#19, sorting floats} {
627 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
631 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
633 test
{LREM
, remove all the occurrences
} {
637 $r rpush mylist foobar
638 $r rpush mylist foobared
643 set res
[$r lrem mylist
0 bar
]
644 list [$r lrange mylist
0 -1] $res
645 } {{foo foobar foobared zap test foo
} 2}
647 test
{LREM
, remove the first occurrence
} {
648 set res
[$r lrem mylist
1 foo
]
649 list [$r lrange mylist
0 -1] $res
650 } {{foobar foobared zap test foo
} 1}
652 test
{LREM
, remove non existing element
} {
653 set res
[$r lrem mylist
1 nosuchelement
]
654 list [$r lrange mylist
0 -1] $res
655 } {{foobar foobared zap test foo
} 0}
657 test
{LREM
, starting from tail with negative count
} {
661 $r rpush mylist foobar
662 $r rpush mylist foobared
668 set res
[$r lrem mylist
-1 bar
]
669 list [$r lrange mylist
0 -1] $res
670 } {{foo bar foobar foobared zap test foo foo
} 1}
672 test
{LREM
, starting from tail with negative count
(2)} {
673 set res
[$r lrem mylist
-2 foo
]
674 list [$r lrange mylist
0 -1] $res
675 } {{foo bar foobar foobared zap test
} 2}
677 test
{LREM
, deleting objects that may be encoded as integers
} {
678 $r lpush myotherlist
1
679 $r lpush myotherlist
2
680 $r lpush myotherlist
3
681 $r lrem myotherlist
1 2
692 test
{MGET against non existing key
} {
693 $r mget foo baazz bar
696 test
{MGET against non-string key
} {
699 $r mget foo baazz bar myset
708 for {set i
0} {$i < 100} {incr i
} {
709 set rkey
[$r randomkey
]
710 if {$rkey eq
{foo
}} {
713 if {$rkey eq
{bar
}} {
717 list $foo_seen $bar_seen
720 test
{RANDOMKEY against empty DB
} {
725 test
{RANDOMKEY regression
1} {
732 test
{GETSET
(set new value
)} {
733 list [$r getset foo xyz
] [$r get foo
]
736 test
{GETSET
(replace old value
)} {
738 list [$r getset foo xyz
] [$r get foo
]
741 test
{SMOVE basics
} {
748 $r smove myset1 myset2 a
749 list [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
752 test
{SMOVE non existing key
} {
753 list [$r smove myset1 myset2 foo
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset1
]]
754 } {0 {a x y z
} {b c
}}
756 test
{SMOVE non existing src
set} {
757 list [$r smove noset myset2 foo
] [lsort [$r smembers myset2
]]
760 test
{SMOVE non existing dst
set} {
761 list [$r smove myset2 myset3 y
] [lsort [$r smembers myset2
]] [lsort [$r smembers myset3
]]
764 test
{SMOVE wrong src key type
} {
766 catch {$r smove x myset2 foo
} err
770 test
{SMOVE wrong dst key type
} {
772 catch {$r smove myset2 x foo
} err
776 test
{MSET base case
} {
777 $r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
779 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
781 test
{MSET wrong number of args
} {
782 catch {$r mset x
10 y
"foo bar" z
} err
786 test
{MSETNX with already existent key
} {
787 list [$r msetnx x1 xxx y2 yyy x
20] [$r exists x1
] [$r exists y2
]
790 test
{MSETNX with not existing keys
} {
791 list [$r msetnx x1 xxx y2 yyy
] [$r get x1
] [$r get y2
]
794 test
{ZSET basic ZADD and score
update} {
798 set aux1
[$r zrange ztmp
0 -1]
800 set aux2
[$r zrange ztmp
0 -1]
807 for {set i
0} {$i < 1000} {incr i
} {
808 set score
[expr rand
()]
810 $r zadd zscoretest
$score $i
812 for {set i
0} {$i < 1000} {incr i
} {
813 if {[$r zscore zscoretest
$i] != [lindex $aux $i]} {
814 set err
"Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
821 test
{ZRANGE and ZREVRANGE
} {
822 list [$r zrange ztmp
0 -1] [$r zrevrange ztmp
0 -1]
825 test
{ZSETs stress tester
- sorting is working well?
} {
827 for {set test
0} {$test < 2} {incr test
} {
828 unset -nocomplain auxarray
829 array set auxarray
{}
832 for {set i
0} {$i < 1000} {incr i
} {
834 set score
[expr rand
()]
836 set score
[expr int
(rand
()*10)]
838 set auxarray
($i) $score
839 $r zadd myzset
$score $i
841 if {[expr rand
()] < .2} {
842 set j
[expr int
(rand
()*1000)]
844 set score
[expr rand
()]
846 set score
[expr int
(rand
()*10)]
848 set auxarray
($j) $score
849 $r zadd myzset
$score $j
852 foreach {item score
} [array get auxarray
] {
853 lappend auxlist
[list $score $item]
855 set sorted
[lsort -command zlistAlikeSort
$auxlist]
858 lappend auxlist
[lindex $x 1]
860 set fromredis
[$r zrange myzset
0 -1]
862 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
863 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
871 test
{ZSETs skiplist implementation backlink consistency test
} {
874 for {set j
0} {$j < $elements} {incr j
} {
875 $r zadd myzset
[expr rand
()] "Element-$j"
876 $r zrem myzset
"Element-[expr int(rand()*$elements)]"
878 set l1
[$r zrange myzset
0 -1]
879 set l2
[$r zrevrange myzset
0 -1]
880 for {set j
0} {$j < [llength $l1]} {incr j
} {
881 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
888 foreach fuzztype
{binary alpha compr
} {
889 test
"FUZZ stresser with data model $fuzztype" {
891 for {set i
0} {$i < 10000} {incr i
} {
892 set fuzz
[randstring
0 512 $fuzztype]
896 set err
[list $fuzz $got]
904 # Leave the user with a clean DB before to exit
909 lappend aux
[$r dbsize
]
912 lappend aux
[$r dbsize
]
915 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
917 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
927 set randkey
[expr int
(rand
()*10000)]
928 set randval
[expr int
(rand
()*10000)]
929 set randidx0
[expr int
(rand
()*10)]
930 set randidx1
[expr int
(rand
()*10)]
931 set cmd
[expr int
(rand
()*20)]
933 if {$cmd == 0} {$r set $randkey $randval}
934 if {$cmd == 1} {$r get
$randkey}
935 if {$cmd == 2} {$r incr $randkey}
936 if {$cmd == 3} {$r lpush
$randkey $randval}
937 if {$cmd == 4} {$r rpop
$randkey}
938 if {$cmd == 5} {$r del
$randkey}
939 if {$cmd == 6} {$r llen
$randkey}
940 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
941 if {$cmd == 8} {$r ltrim
$randkey $randidx0 $randidx1}
942 if {$cmd == 9} {$r lindex $randkey $randidx0}
943 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
944 if {$cmd == 11} {$r sadd
$randkey $randval}
945 if {$cmd == 12} {$r srem
$randkey $randval}
946 if {$cmd == 13} {$r smove
$randkey $randval}
947 if {$cmd == 14} {$r scard
$randkey}
948 if {$cmd == 15} {$r expire
$randkey [expr $randval%60]}
956 # Before to run the test check if DB 9 and DB 10 are empty
959 set db9size
[$r dbsize
]
961 set db10size
[$r dbsize
]
962 if {$db9size != 0 ||
$db10size != 0} {
963 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
971 if {[llength $argv] == 0} {
973 } elseif
{[llength $argv] == 1 && [lindex $argv 0] eq
{stress
}} {
976 main
[lindex $argv 0] [lindex $argv 1]