]> git.saurik.com Git - redis.git/blame - test-redis.tcl
fix for test #11
[redis.git] / test-redis.tcl
CommitLineData
7a932b74 1# test-redis.tcl
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
4# more information.
ed9b544e 5
405b0a6a 6set tcl_precision 17
f89c3a35 7source redis.tcl
43c9dc7b 8
ed9b544e 9set ::passed 0
10set ::failed 0
fc77604c 11set ::testnum 0
ed9b544e 12
13proc test {name code okpattern} {
fc77604c 14 incr ::testnum
15 if {$::testnum < $::first || $::testnum > $::last} return
16 puts -nonewline [format "%-70s " "#$::testnum $name"]
ed9b544e 17 flush stdout
18 set retval [uplevel 1 $code]
19 if {$okpattern eq $retval || [string match $okpattern $retval]} {
20 puts "PASSED"
21 incr ::passed
22 } else {
23 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
24 incr ::failed
25 }
26}
27
75398fbc 28proc randstring {min max {type binary}} {
29 set len [expr {$min+int(rand()*($max-$min+1))}]
30 set output {}
31 if {$type eq {binary}} {
32 set minval 0
33 set maxval 255
34 } elseif {$type eq {alpha}} {
35 set minval 48
36 set maxval 122
37 } elseif {$type eq {compr}} {
38 set minval 48
39 set maxval 52
40 }
41 while {$len} {
42 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
43 incr len -1
44 }
45 return $output
46}
47
d7f43c08 48# Useful for some test
49proc 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]
53}
54
0d36ded0 55proc waitForBgsave r {
56 while 1 {
57 set i [$r info]
58 if {[string match {*bgsave_in_progress:1*} $i]} {
59 puts -nonewline "\nWaiting for background save to finish... "
60 flush stdout
61 after 1000
62 } else {
63 break
64 }
65 }
66}
67
e96e4fbf 68proc waitForBgrewriteaof r {
69 while 1 {
70 set i [$r info]
71 if {[string match {*bgrewriteaof_in_progress:1*} $i]} {
72 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
73 flush stdout
74 after 1000
75 } else {
76 break
77 }
78 }
79}
80
e054afda 81proc randomInt {max} {
82 expr {int(rand()*$max)}
83}
84
85proc randpath args {
86 set path [expr {int(rand()*[llength $args])}]
87 uplevel 1 [lindex $args $path]
88}
89
90proc randomValue {} {
91 randpath {
92 # Small enough to likely collide
93 randomInt 1000
94 } {
95 # 32 bit compressible signed/unsigned
96 randpath {randomInt 2000000000} {randomInt 4000000000}
97 } {
98 # 64 bit
99 randpath {randomInt 1000000000000}
100 } {
101 # Random string
102 randpath {randstring 0 256 alpha} \
103 {randstring 0 256 compr} \
104 {randstring 0 256 binary}
105 }
106}
107
108proc randomKey {} {
109 randpath {
110 # Small enough to likely collide
111 randomInt 1000
112 } {
113 # 32 bit compressible signed/unsigned
114 randpath {randomInt 2000000000} {randomInt 4000000000}
115 } {
116 # 64 bit
117 randpath {randomInt 1000000000000}
118 } {
119 # Random string
120 randpath {randstring 1 256 alpha} \
121 {randstring 1 256 compr}
122 }
123}
124
125proc createComplexDataset {r ops} {
126 for {set j 0} {$j < $ops} {incr j} {
127 set k [randomKey]
128 set v [randomValue]
ab9d4cb1 129 randpath {
130 set d [expr {rand()}]
131 } {
132 set d [expr {rand()}]
133 } {
134 set d [expr {rand()}]
135 } {
136 set d [expr {rand()}]
137 } {
138 set d [expr {rand()}]
139 } {
140 randpath {set d +inf} {set d -inf}
141 }
e054afda 142 set t [$r type $k]
143
144 if {$t eq {none}} {
145 randpath {
146 $r set $k $v
147 } {
148 $r lpush $k $v
149 } {
150 $r sadd $k $v
151 } {
152 $r zadd $k $d $v
153 }
154 set t [$r type $k]
155 }
156
157 switch $t {
158 {string} {
159 # Nothing to do
160 }
161 {list} {
162 randpath {$r lpush $k $v} \
163 {$r rpush $k $v} \
164 {$r lrem $k 0 $v} \
165 {$r rpop $k} \
166 {$r lpop $k}
167 }
168 {set} {
169 randpath {$r sadd $k $v} \
170 {$r srem $k $v}
171 }
172 {zset} {
173 randpath {$r zadd $k $d $v} \
174 {$r zrem $k $v}
175 }
176 }
177 }
178}
179
180proc datasetDigest r {
181 set keys [lsort [split [$r keys *] " "]]
e96e4fbf 182 set digest {}
e054afda 183 foreach k $keys {
184 set t [$r type $k]
e96e4fbf 185 switch $t {
186 {string} {
187 set aux [::sha1::sha1 -hex [$r get $k]]
188 } {list} {
189 if {[$r llen $k] == 0} {
190 set aux {}
191 } else {
192 set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]]
193 }
194 } {set} {
195 if {[$r scard $k] == 0} {
196 set aux {}
197 } else {
198 set aux [::sha1::sha1 -hex [lsort [$r smembers $k]]]
199 }
200 } {zset} {
201 if {[$r zcard $k] == 0} {
202 set aux {}
203 } else {
204 set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]]
205 }
206 } default {
207 error "Type not supported"
208 }
e054afda 209 }
e96e4fbf 210 if {$aux eq {}} continue
211 set digest [::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
e054afda 212 }
213 return $digest
214}
215
ed9b544e 216proc main {server port} {
43c9dc7b 217 set r [redis $server $port]
eea4baf7 218 $r select 9
43c9dc7b 219 set err ""
fc77604c 220 set res ""
ed9b544e 221
abcb223e
BH
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.
225
226 #test {AUTH with requirepass in redis.conf} {
227 # $r auth foobared
228 #} {OK}
229
ed9b544e 230 test {DEL all keys to start with a clean DB} {
43c9dc7b 231 foreach key [$r keys *] {$r del $key}
232 $r dbsize
ed9b544e 233 } {0}
234
235 test {SET and GET an item} {
43c9dc7b 236 $r set x foobar
237 $r get x
ed9b544e 238 } {foobar}
239
7c49733c 240 test {SET and GET an empty item} {
241 $r set x {}
242 $r get x
243 } {}
244
ed9b544e 245 test {DEL against a single item} {
43c9dc7b 246 $r del x
247 $r get x
ed9b544e 248 } {}
249
cc582a77 250 test {Vararg DEL} {
251 $r set foo1 a
252 $r set foo2 b
253 $r set foo3 c
254 list [$r del foo1 foo2 foo3 foo4] [$r mget foo1 foo2 foo3]
255 } {3 {{} {} {}}}
256
ed9b544e 257 test {KEYS with pattern} {
258 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
43c9dc7b 259 $r set $key hello
ed9b544e 260 }
43c9dc7b 261 lsort [$r keys foo*]
ed9b544e 262 } {foo_a foo_b foo_c}
263
264 test {KEYS to get all keys} {
43c9dc7b 265 lsort [$r keys *]
ed9b544e 266 } {foo_a foo_b foo_c key_x key_y key_z}
267
268 test {DBSIZE} {
43c9dc7b 269 $r dbsize
ed9b544e 270 } {6}
271
272 test {DEL all keys} {
43c9dc7b 273 foreach key [$r keys *] {$r del $key}
274 $r dbsize
ed9b544e 275 } {0}
276
277 test {Very big payload in GET/SET} {
278 set buf [string repeat "abcd" 1000000]
43c9dc7b 279 $r set foo $buf
280 $r get foo
ed9b544e 281 } [string repeat "abcd" 1000000]
282
283 test {SET 10000 numeric keys and access all them in reverse order} {
25fd2cb2 284 set err {}
ed9b544e 285 for {set x 0} {$x < 10000} {incr x} {
43c9dc7b 286 $r set $x $x
ed9b544e 287 }
288 set sum 0
289 for {set x 9999} {$x >= 0} {incr x -1} {
25fd2cb2 290 set val [$r get $x]
291 if {$val ne $x} {
292 set err "Eleemnt at position $x is $val instead of $x"
293 break
294 }
ed9b544e 295 }
25fd2cb2 296 set _ $err
d663729a 297 } {}
ed9b544e 298
299 test {DBSIZE should be 10001 now} {
43c9dc7b 300 $r dbsize
ed9b544e 301 } {10001}
302
303 test {INCR against non existing key} {
304 set res {}
43c9dc7b 305 append res [$r incr novar]
306 append res [$r get novar]
ed9b544e 307 } {11}
308
309 test {INCR against key created by incr itself} {
43c9dc7b 310 $r incr novar
ed9b544e 311 } {2}
312
313 test {INCR against key originally set with SET} {
43c9dc7b 314 $r set novar 100
315 $r incr novar
ed9b544e 316 } {101}
317
d68ed120 318 test {INCR over 32bit value} {
319 $r set novar 17179869184
320 $r incr novar
321 } {17179869185}
322
323 test {INCRBY over 32bit value with over 32bit increment} {
324 $r set novar 17179869184
325 $r incrby novar 17179869184
326 } {34359738368}
327
ab9d4cb1 328 test {INCR against key with spaces (no integer encoded)} {
329 $r set novar " 11 "
330 $r incr novar
331 } {12}
332
d68ed120 333 test {DECRBY over 32bit value with over 32bit increment, negative res} {
334 $r set novar 17179869184
335 $r decrby novar 17179869185
336 } {-1}
337
ed9b544e 338 test {SETNX target key missing} {
43c9dc7b 339 $r setnx novar2 foobared
340 $r get novar2
ed9b544e 341 } {foobared}
342
343 test {SETNX target key exists} {
43c9dc7b 344 $r setnx novar2 blabla
345 $r get novar2
ed9b544e 346 } {foobared}
347
5acdc75d 348 test {SETNX will overwrite EXPIREing key} {
349 $r set x 10
350 $r expire x 10000
351 $r setnx x 20
352 $r get x
353 } {20}
354
ed9b544e 355 test {EXISTS} {
356 set res {}
43c9dc7b 357 $r set newkey test
358 append res [$r exists newkey]
359 $r del newkey
360 append res [$r exists newkey]
ed9b544e 361 } {10}
362
363 test {Zero length value in key. SET/GET/EXISTS} {
43c9dc7b 364 $r set emptykey {}
365 set res [$r get emptykey]
366 append res [$r exists emptykey]
367 $r del emptykey
368 append res [$r exists emptykey]
ed9b544e 369 } {10}
370
371 test {Commands pipelining} {
43c9dc7b 372 set fd [$r channel]
ed9b544e 373 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
374 flush $fd
375 set res {}
43c9dc7b 376 append res [string match OK* [::redis::redis_read_reply $fd]]
377 append res [::redis::redis_read_reply $fd]
378 append res [string match PONG* [::redis::redis_read_reply $fd]]
ed9b544e 379 format $res
380 } {1xyzk1}
381
382 test {Non existing command} {
43c9dc7b 383 catch {$r foobaredcommand} err
384 string match ERR* $err
ed9b544e 385 } {1}
386
387 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
43c9dc7b 388 $r lpush mylist a
389 $r lpush mylist b
390 $r rpush mylist c
391 set res [$r llen mylist]
392 append res [$r lindex mylist 0]
393 append res [$r lindex mylist 1]
394 append res [$r lindex mylist 2]
ab9d4cb1 395 list $res [$r lindex mylist 100]
396 } {3bac {}}
ed9b544e 397
398 test {DEL a list} {
43c9dc7b 399 $r del mylist
400 $r exists mylist
ed9b544e 401 } {0}
402
403 test {Create a long list and check every single element with LINDEX} {
404 set ok 0
405 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 406 $r rpush mylist $i
ed9b544e 407 }
408 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 409 if {[$r lindex mylist $i] eq $i} {incr ok}
410 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
ed9b544e 411 incr ok
412 }
413 }
414 format $ok
415 } {2000}
416
417 test {Test elements with LINDEX in random access} {
418 set ok 0
419 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 420 set rint [expr int(rand()*1000)]
421 if {[$r lindex mylist $rint] eq $rint} {incr ok}
422 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
ed9b544e 423 incr ok
424 }
425 }
426 format $ok
427 } {2000}
428
210e29f7 429 test {Check if the list is still ok after a DEBUG RELOAD} {
430 $r debug reload
431 set ok 0
432 for {set i 0} {$i < 1000} {incr i} {
433 set rint [expr int(rand()*1000)]
434 if {[$r lindex mylist $rint] eq $rint} {incr ok}
435 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
436 incr ok
437 }
438 }
439 format $ok
440 } {2000}
441
ed9b544e 442 test {LLEN against non-list value error} {
43c9dc7b 443 $r del mylist
444 $r set mylist foobar
445 catch {$r llen mylist} err
446 format $err
c937aa89 447 } {ERR*}
ed9b544e 448
ab9d4cb1 449 test {LLEN against non existing key} {
450 $r llen not-a-key
451 } {0}
452
ed9b544e 453 test {LINDEX against non-list value error} {
43c9dc7b 454 catch {$r lindex mylist 0} err
455 format $err
c937aa89 456 } {ERR*}
ed9b544e 457
ab9d4cb1 458 test {LINDEX against non existing key} {
459 $r lindex not-a-key 10
460 } {}
461
ed9b544e 462 test {LPUSH against non-list value error} {
43c9dc7b 463 catch {$r lpush mylist 0} err
464 format $err
c937aa89 465 } {ERR*}
ed9b544e 466
467 test {RPUSH against non-list value error} {
43c9dc7b 468 catch {$r rpush mylist 0} err
469 format $err
c937aa89 470 } {ERR*}
ed9b544e 471
c08f1734 472 test {RPOPLPUSH base case} {
473 $r del mylist
474 $r rpush mylist a
475 $r rpush mylist b
476 $r rpush mylist c
477 $r rpush mylist d
478 set v1 [$r rpoplpush mylist newlist]
479 set v2 [$r rpoplpush mylist newlist]
480 set l1 [$r lrange mylist 0 -1]
481 set l2 [$r lrange newlist 0 -1]
482 list $v1 $v2 $l1 $l2
483 } {d c {a b} {c d}}
484
485 test {RPOPLPUSH with the same list as src and dst} {
486 $r del mylist
487 $r rpush mylist a
488 $r rpush mylist b
489 $r rpush mylist c
490 set l1 [$r lrange mylist 0 -1]
491 set v [$r rpoplpush mylist mylist]
492 set l2 [$r lrange mylist 0 -1]
493 list $l1 $v $l2
494 } {{a b c} c {c a b}}
495
496 test {RPOPLPUSH target list already exists} {
497 $r del mylist
498 $r del newlist
499 $r rpush mylist a
500 $r rpush mylist b
501 $r rpush mylist c
502 $r rpush mylist d
503 $r rpush newlist x
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]
508 list $v1 $v2 $l1 $l2
509 } {d c {a b} {c d x}}
510
b9bb7ba2 511 test {RPOPLPUSH against non existing key} {
512 $r del mylist
513 $r del newlist
514 set v1 [$r rpoplpush mylist newlist]
515 list $v1 [$r exists mylist] [$r exists newlist]
516 } {{} 0 0}
517
518 test {RPOPLPUSH against non list src key} {
519 $r del mylist
520 $r del newlist
521 $r set mylist x
522 catch {$r rpoplpush mylist newlist} err
523 list [$r type mylist] [$r exists newlist] [string range $err 0 2]
524 } {string 0 ERR}
525
526 test {RPOPLPUSH against non list dst key} {
527 $r del mylist
528 $r del newlist
529 $r rpush mylist a
530 $r rpush mylist b
531 $r rpush mylist c
532 $r rpush mylist d
533 $r set newlist x
534 catch {$r rpoplpush mylist newlist} err
535 list [$r lrange mylist 0 -1] [$r type newlist] [string range $err 0 2]
536 } {{a b c d} string ERR}
537
fdcaae84 538 test {RPOPLPUSH against non existing src key} {
539 $r del mylist
540 $r del newlist
541 $r rpoplpush mylist newlist
542 } {}
543
ed9b544e 544 test {RENAME basic usage} {
43c9dc7b 545 $r set mykey hello
546 $r rename mykey mykey1
547 $r rename mykey1 mykey2
548 $r get mykey2
ed9b544e 549 } {hello}
550
551 test {RENAME source key should no longer exist} {
43c9dc7b 552 $r exists mykey
ed9b544e 553 } {0}
554
555 test {RENAME against already existing key} {
43c9dc7b 556 $r set mykey a
557 $r set mykey2 b
558 $r rename mykey2 mykey
559 set res [$r get mykey]
560 append res [$r exists mykey2]
ed9b544e 561 } {b0}
562
563 test {RENAMENX basic usage} {
43c9dc7b 564 $r del mykey
565 $r del mykey2
566 $r set mykey foobar
567 $r renamenx mykey mykey2
568 set res [$r get mykey2]
569 append res [$r exists mykey]
ed9b544e 570 } {foobar0}
571
572 test {RENAMENX against already existing key} {
43c9dc7b 573 $r set mykey foo
574 $r set mykey2 bar
575 $r renamenx mykey mykey2
ed9b544e 576 } {0}
577
578 test {RENAMENX against already existing key (2)} {
43c9dc7b 579 set res [$r get mykey]
580 append res [$r get mykey2]
ed9b544e 581 } {foobar}
582
583 test {RENAME against non existing source key} {
43c9dc7b 584 catch {$r rename nokey foobar} err
585 format $err
c937aa89 586 } {ERR*}
ed9b544e 587
588 test {RENAME where source and dest key is the same} {
43c9dc7b 589 catch {$r rename mykey mykey} err
590 format $err
c937aa89 591 } {ERR*}
ed9b544e 592
593 test {DEL all keys again (DB 0)} {
43c9dc7b 594 foreach key [$r keys *] {
595 $r del $key
ed9b544e 596 }
43c9dc7b 597 $r dbsize
ed9b544e 598 } {0}
599
600 test {DEL all keys again (DB 1)} {
eea4baf7 601 $r select 10
43c9dc7b 602 foreach key [$r keys *] {
603 $r del $key
ed9b544e 604 }
43c9dc7b 605 set res [$r dbsize]
eea4baf7 606 $r select 9
ed9b544e 607 format $res
608 } {0}
609
610 test {MOVE basic usage} {
43c9dc7b 611 $r set mykey foobar
eea4baf7 612 $r move mykey 10
ed9b544e 613 set res {}
43c9dc7b 614 lappend res [$r exists mykey]
615 lappend res [$r dbsize]
eea4baf7 616 $r select 10
43c9dc7b 617 lappend res [$r get mykey]
618 lappend res [$r dbsize]
eea4baf7 619 $r select 9
ed9b544e 620 format $res
621 } [list 0 0 foobar 1]
622
623 test {MOVE against key existing in the target DB} {
43c9dc7b 624 $r set mykey hello
eea4baf7 625 $r move mykey 10
ed9b544e 626 } {0}
627
628 test {SET/GET keys in different DBs} {
43c9dc7b 629 $r set a hello
630 $r set b world
eea4baf7 631 $r select 10
43c9dc7b 632 $r set a foo
633 $r set b bared
eea4baf7 634 $r select 9
ed9b544e 635 set res {}
43c9dc7b 636 lappend res [$r get a]
637 lappend res [$r get b]
eea4baf7 638 $r select 10
43c9dc7b 639 lappend res [$r get a]
640 lappend res [$r get b]
eea4baf7 641 $r select 9
ed9b544e 642 format $res
643 } {hello world foo bared}
644
645 test {Basic LPOP/RPOP} {
43c9dc7b 646 $r del mylist
647 $r rpush mylist 1
648 $r rpush mylist 2
649 $r lpush mylist 0
650 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
ed9b544e 651 } [list 0 2 1 0]
652
653 test {LPOP/RPOP against empty list} {
43c9dc7b 654 $r lpop mylist
ed9b544e 655 } {}
656
657 test {LPOP against non list value} {
43c9dc7b 658 $r set notalist foo
659 catch {$r lpop notalist} err
660 format $err
c937aa89 661 } {ERR*kind*}
ed9b544e 662
663 test {Mass LPUSH/LPOP} {
664 set sum 0
665 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 666 $r lpush mylist $i
ed9b544e 667 incr sum $i
668 }
669 set sum2 0
670 for {set i 0} {$i < 500} {incr i} {
43c9dc7b 671 incr sum2 [$r lpop mylist]
672 incr sum2 [$r rpop mylist]
ed9b544e 673 }
674 expr $sum == $sum2
675 } {1}
676
677 test {LRANGE basics} {
678 for {set i 0} {$i < 10} {incr i} {
43c9dc7b 679 $r rpush mylist $i
ed9b544e 680 }
43c9dc7b 681 list [$r lrange mylist 1 -2] \
682 [$r lrange mylist -3 -1] \
683 [$r lrange mylist 4 4]
ed9b544e 684 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
685
686 test {LRANGE inverted indexes} {
43c9dc7b 687 $r lrange mylist 6 2
ed9b544e 688 } {}
689
690 test {LRANGE out of range indexes including the full list} {
43c9dc7b 691 $r lrange mylist -1000 1000
ed9b544e 692 } {0 1 2 3 4 5 6 7 8 9}
693
694 test {LRANGE against non existing key} {
43c9dc7b 695 $r lrange nosuchkey 0 1
ed9b544e 696 } {}
697
698 test {LTRIM basics} {
43c9dc7b 699 $r del mylist
ed9b544e 700 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 701 $r lpush mylist $i
702 $r ltrim mylist 0 4
ed9b544e 703 }
43c9dc7b 704 $r lrange mylist 0 -1
ed9b544e 705 } {99 98 97 96 95}
706
c632369b 707 test {LTRIM stress testing} {
708 set mylist {}
709 set err {}
710 for {set i 0} {$i < 20} {incr i} {
711 lappend mylist $i
712 }
713
714 for {set j 0} {$j < 100} {incr j} {
715 # Fill the list
716 $r del mylist
717 for {set i 0} {$i < 20} {incr i} {
718 $r rpush mylist $i
719 }
720 # Trim at random
721 set a [randomInt 20]
722 set b [randomInt 20]
723 $r ltrim mylist $a $b
724 if {[$r lrange mylist 0 -1] ne [lrange $mylist $a $b]} {
725 set err "[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
726 break
727 }
728 }
729 set _ $err
730 } {}
731
ed9b544e 732 test {LSET} {
c632369b 733 $r del mylist
734 foreach x {99 98 97 96 95} {
735 $r rpush mylist $x
736 }
43c9dc7b 737 $r lset mylist 1 foo
738 $r lset mylist -1 bar
739 $r lrange mylist 0 -1
ed9b544e 740 } {99 foo 97 96 bar}
741
742 test {LSET out of range index} {
43c9dc7b 743 catch {$r lset mylist 10 foo} err
744 format $err
c937aa89 745 } {ERR*range*}
ed9b544e 746
747 test {LSET against non existing key} {
43c9dc7b 748 catch {$r lset nosuchkey 10 foo} err
749 format $err
c937aa89 750 } {ERR*key*}
ed9b544e 751
752 test {LSET against non list value} {
43c9dc7b 753 $r set nolist foobar
754 catch {$r lset nolist 0 foo} err
755 format $err
c937aa89 756 } {ERR*value*}
ed9b544e 757
758 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
43c9dc7b 759 $r sadd myset foo
760 $r sadd myset bar
761 list [$r scard myset] [$r sismember myset foo] \
762 [$r sismember myset bar] [$r sismember myset bla] \
763 [lsort [$r smembers myset]]
ed9b544e 764 } {2 1 1 0 {bar foo}}
765
766 test {SADD adding the same element multiple times} {
43c9dc7b 767 $r sadd myset foo
768 $r sadd myset foo
769 $r sadd myset foo
770 $r scard myset
ed9b544e 771 } {2}
772
773 test {SADD against non set} {
43c9dc7b 774 catch {$r sadd mylist foo} err
775 format $err
c937aa89 776 } {ERR*kind*}
ed9b544e 777
778 test {SREM basics} {
43c9dc7b 779 $r sadd myset ciao
780 $r srem myset foo
781 lsort [$r smembers myset]
ed9b544e 782 } {bar ciao}
783
784 test {Mass SADD and SINTER with two sets} {
785 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 786 $r sadd set1 $i
787 $r sadd set2 [expr $i+995]
ed9b544e 788 }
43c9dc7b 789 lsort [$r sinter set1 set2]
ed9b544e 790 } {995 996 997 998 999}
40d224a9 791
792 test {SUNION with two sets} {
793 lsort [$r sunion set1 set2]
794 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
ed9b544e 795
796 test {SINTERSTORE with two sets} {
43c9dc7b 797 $r sinterstore setres set1 set2
798 lsort [$r smembers setres]
ed9b544e 799 } {995 996 997 998 999}
800
210e29f7 801 test {SINTERSTORE with two sets, after a DEBUG RELOAD} {
802 $r debug reload
803 $r sinterstore setres set1 set2
804 lsort [$r smembers setres]
805 } {995 996 997 998 999}
806
40d224a9 807 test {SUNIONSTORE with two sets} {
808 $r sunionstore setres set1 set2
809 lsort [$r smembers setres]
810 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
811
f4d9b3c6 812 test {SUNIONSTORE against non existing keys} {
813 $r set setres xxx
814 list [$r sunionstore setres foo111 bar222] [$r exists xxx]
815 } {0 0}
816
ed9b544e 817 test {SINTER against three sets} {
43c9dc7b 818 $r sadd set3 999
819 $r sadd set3 995
820 $r sadd set3 1000
821 $r sadd set3 2000
822 lsort [$r sinter set1 set2 set3]
ed9b544e 823 } {995 999}
824
825 test {SINTERSTORE with three sets} {
43c9dc7b 826 $r sinterstore setres set1 set2 set3
827 lsort [$r smembers setres]
ed9b544e 828 } {995 999}
40d224a9 829
830 test {SUNION with non existing keys} {
831 lsort [$r sunion nokey1 set1 set2 nokey2]
832 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
833
f4f56e1d 834 test {SDIFF with two sets} {
835 for {set i 5} {$i < 1000} {incr i} {
836 $r sadd set4 $i
837 }
838 lsort [$r sdiff set1 set4]
839 } {0 1 2 3 4}
840
841 test {SDIFF with three sets} {
842 $r sadd set5 0
843 lsort [$r sdiff set1 set4 set5]
844 } {1 2 3 4}
845
846 test {SDIFFSTORE with three sets} {
847 $r sdiffstore sres set1 set4 set5
848 lsort [$r smembers sres]
849 } {1 2 3 4}
850
12fea928 851 test {SPOP basics} {
852 $r del myset
853 $r sadd myset 1
854 $r sadd myset 2
855 $r sadd myset 3
856 list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset]
857 } {{1 2 3} 0}
858
ed9b544e 859 test {SAVE - make sure there are all the types as values} {
bbf44ecf 860 # Wait for a background saving in progress to terminate
0d36ded0 861 waitForBgsave $r
43c9dc7b 862 $r lpush mysavelist hello
863 $r lpush mysavelist world
864 $r set myemptykey {}
865 $r set mynormalkey {blablablba}
d7f43c08 866 $r zadd mytestzset a 10
867 $r zadd mytestzset b 20
868 $r zadd mytestzset c 30
43c9dc7b 869 $r save
c937aa89 870 } {OK}
6cbfd2b3 871
872 test {SRANDMEMBER} {
873 $r del myset
874 $r sadd myset a
875 $r sadd myset b
876 $r sadd myset c
877 unset -nocomplain myset
878 array set myset {}
879 for {set i 0} {$i < 100} {incr i} {
880 set myset([$r srandmember myset]) 1
881 }
882 lsort [array names myset]
883 } {a b c}
ed9b544e 884
98e1bb81 885 test {Create a random list and a random set} {
ed9b544e 886 set tosort {}
887 array set seenrand {}
888 for {set i 0} {$i < 10000} {incr i} {
889 while 1 {
890 # Make sure all the weights are different because
891 # Redis does not use a stable sort but Tcl does.
98e1bb81 892 randpath {
893 set rint [expr int(rand()*1000000)]
894 } {
895 set rint [expr rand()]
896 }
43c9dc7b 897 if {![info exists seenrand($rint)]} break
ed9b544e 898 }
43c9dc7b 899 set seenrand($rint) x
900 $r lpush tosort $i
98e1bb81 901 $r sadd tosort-set $i
43c9dc7b 902 $r set weight_$i $rint
903 lappend tosort [list $i $rint]
ed9b544e 904 }
905 set sorted [lsort -index 1 -real $tosort]
906 set res {}
907 for {set i 0} {$i < 10000} {incr i} {
908 lappend res [lindex $sorted $i 0]
909 }
910 format {}
911 } {}
912
913 test {SORT with BY against the newly created list} {
43c9dc7b 914 $r sort tosort {BY weight_*}
ed9b544e 915 } $res
916
98e1bb81 917 test {the same SORT with BY, but against the newly created set} {
918 $r sort tosort-set {BY weight_*}
919 } $res
920
5384a2d8 921 test {SORT with BY and STORE against the newly created list} {
922 $r sort tosort {BY weight_*} store sort-res
923 $r lrange sort-res 0 -1
924 } $res
925
ed9b544e 926 test {SORT direct, numeric, against the newly created list} {
43c9dc7b 927 $r sort tosort
ed9b544e 928 } [lsort -integer $res]
929
930 test {SORT decreasing sort} {
43c9dc7b 931 $r sort tosort {DESC}
ed9b544e 932 } [lsort -decreasing -integer $res]
933
934 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
935 set start [clock clicks -milliseconds]
936 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 937 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
ed9b544e 938 }
939 set elapsed [expr [clock clicks -milliseconds]-$start]
940 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
941 flush stdout
942 format {}
943 } {}
944
945 test {SORT speed, sorting 10000 elements list directly, 100 times} {
946 set start [clock clicks -milliseconds]
947 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 948 set sorted [$r sort tosort {LIMIT 0 10}]
ed9b544e 949 }
950 set elapsed [expr [clock clicks -milliseconds]-$start]
951 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
952 flush stdout
953 format {}
954 } {}
955
956 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
957 set start [clock clicks -milliseconds]
958 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 959 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
ed9b544e 960 }
961 set elapsed [expr [clock clicks -milliseconds]-$start]
962 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
963 flush stdout
964 format {}
965 } {}
966
967 test {SORT regression for issue #19, sorting floats} {
43c9dc7b 968 $r flushdb
ed9b544e 969 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
43c9dc7b 970 $r lpush mylist $x
ed9b544e 971 }
43c9dc7b 972 $r sort mylist
ed9b544e 973 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
974
28173a49 975 test {SORT with GET #} {
976 $r del mylist
977 $r lpush mylist 1
978 $r lpush mylist 2
979 $r lpush mylist 3
980 $r mset weight_1 10 weight_2 5 weight_3 30
981 $r sort mylist BY weight_* GET #
982 } {2 1 3}
983
d922ae65 984 test {SORT with constant GET} {
985 $r sort mylist GET foo
986 } {{} {} {}}
987
ed9b544e 988 test {LREM, remove all the occurrences} {
eea4baf7 989 $r flushdb
43c9dc7b 990 $r rpush mylist foo
991 $r rpush mylist bar
992 $r rpush mylist foobar
993 $r rpush mylist foobared
994 $r rpush mylist zap
995 $r rpush mylist bar
996 $r rpush mylist test
997 $r rpush mylist foo
998 set res [$r lrem mylist 0 bar]
999 list [$r lrange mylist 0 -1] $res
ed9b544e 1000 } {{foo foobar foobared zap test foo} 2}
1001
1002 test {LREM, remove the first occurrence} {
43c9dc7b 1003 set res [$r lrem mylist 1 foo]
1004 list [$r lrange mylist 0 -1] $res
ed9b544e 1005 } {{foobar foobared zap test foo} 1}
1006
1007 test {LREM, remove non existing element} {
43c9dc7b 1008 set res [$r lrem mylist 1 nosuchelement]
1009 list [$r lrange mylist 0 -1] $res
ed9b544e 1010 } {{foobar foobared zap test foo} 0}
1011
1012 test {LREM, starting from tail with negative count} {
eea4baf7 1013 $r flushdb
43c9dc7b 1014 $r rpush mylist foo
1015 $r rpush mylist bar
1016 $r rpush mylist foobar
1017 $r rpush mylist foobared
1018 $r rpush mylist zap
1019 $r rpush mylist bar
1020 $r rpush mylist test
1021 $r rpush mylist foo
1022 $r rpush mylist foo
1023 set res [$r lrem mylist -1 bar]
1024 list [$r lrange mylist 0 -1] $res
ed9b544e 1025 } {{foo bar foobar foobared zap test foo foo} 1}
1026
1027 test {LREM, starting from tail with negative count (2)} {
43c9dc7b 1028 set res [$r lrem mylist -2 foo]
1029 list [$r lrange mylist 0 -1] $res
ed9b544e 1030 } {{foo bar foobar foobared zap test} 2}
1031
724a51b1 1032 test {LREM, deleting objects that may be encoded as integers} {
1033 $r lpush myotherlist 1
1034 $r lpush myotherlist 2
1035 $r lpush myotherlist 3
1036 $r lrem myotherlist 1 2
1037 $r llen myotherlist
1038 } {2}
1039
5b19bd72 1040 test {MGET} {
eea4baf7 1041 $r flushdb
43c9dc7b 1042 $r set foo BAR
1043 $r set bar FOO
1044 $r mget foo bar
5b19bd72 1045 } {BAR FOO}
1046
1047 test {MGET against non existing key} {
43c9dc7b 1048 $r mget foo baazz bar
5b19bd72 1049 } {BAR {} FOO}
1050
1051 test {MGET against non-string key} {
43c9dc7b 1052 $r sadd myset ciao
1053 $r sadd myset bau
1054 $r mget foo baazz bar myset
5b19bd72 1055 } {BAR {} FOO {}}
1056
ce7bef07 1057 test {RANDOMKEY} {
eea4baf7 1058 $r flushdb
ce7bef07 1059 $r set foo x
1060 $r set bar y
1061 set foo_seen 0
1062 set bar_seen 0
1063 for {set i 0} {$i < 100} {incr i} {
1064 set rkey [$r randomkey]
1065 if {$rkey eq {foo}} {
1066 set foo_seen 1
1067 }
1068 if {$rkey eq {bar}} {
1069 set bar_seen 1
1070 }
1071 }
1072 list $foo_seen $bar_seen
1073 } {1 1}
1074
1075 test {RANDOMKEY against empty DB} {
eea4baf7 1076 $r flushdb
ce7bef07 1077 $r randomkey
1078 } {}
1079
f5785ae9 1080 test {RANDOMKEY regression 1} {
eea4baf7 1081 $r flushdb
f5785ae9 1082 $r set x 10
1083 $r del x
1084 $r randomkey
1085 } {}
1086
7ac6d461 1087 test {GETSET (set new value)} {
1088 list [$r getset foo xyz] [$r get foo]
1089 } {{} xyz}
1090
1091 test {GETSET (replace old value)} {
1092 $r set foo bar
1093 list [$r getset foo xyz] [$r get foo]
1094 } {bar xyz}
1095
0eeb2a4b 1096 test {SMOVE basics} {
1097 $r sadd myset1 a
1098 $r sadd myset1 b
1099 $r sadd myset1 c
1100 $r sadd myset2 x
1101 $r sadd myset2 y
1102 $r sadd myset2 z
1103 $r smove myset1 myset2 a
1104 list [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1105 } {{a x y z} {b c}}
1106
1107 test {SMOVE non existing key} {
1108 list [$r smove myset1 myset2 foo] [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1109 } {0 {a x y z} {b c}}
1110
1111 test {SMOVE non existing src set} {
1112 list [$r smove noset myset2 foo] [lsort [$r smembers myset2]]
1113 } {0 {a x y z}}
1114
1115 test {SMOVE non existing dst set} {
1116 list [$r smove myset2 myset3 y] [lsort [$r smembers myset2]] [lsort [$r smembers myset3]]
1117 } {1 {a x z} y}
1118
1119 test {SMOVE wrong src key type} {
1120 $r set x 10
1121 catch {$r smove x myset2 foo} err
1122 format $err
1123 } {ERR*}
1124
1125 test {SMOVE wrong dst key type} {
1126 $r set x 10
1127 catch {$r smove myset2 x foo} err
1128 format $err
1129 } {ERR*}
1130
f69f2cba 1131 test {MSET base case} {
1132 $r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n"
1133 $r mget x y z
1134 } [list 10 {foo bar} "x x x x x x x\n\n\r\n"]
1135
1136 test {MSET wrong number of args} {
1137 catch {$r mset x 10 y "foo bar" z} err
1138 format $err
1139 } {*wrong number*}
1140
1141 test {MSETNX with already existent key} {
1142 list [$r msetnx x1 xxx y2 yyy x 20] [$r exists x1] [$r exists y2]
1143 } {0 0 0}
1144
1145 test {MSETNX with not existing keys} {
1146 list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2]
1147 } {1 xxx yyy}
1148
5acdc75d 1149 test {MSETNX should remove all the volatile keys even on failure} {
1150 $r mset x 1 y 2 z 3
1151 $r expire y 10000
1152 $r expire z 10000
1153 list [$r msetnx x A y B z C] [$r mget x y z]
1154 } {0 {1 {} {}}}
1155
d7f43c08 1156 test {ZSET basic ZADD and score update} {
1157 $r zadd ztmp 10 x
1158 $r zadd ztmp 20 y
1159 $r zadd ztmp 30 z
1160 set aux1 [$r zrange ztmp 0 -1]
1161 $r zadd ztmp 1 y
1162 set aux2 [$r zrange ztmp 0 -1]
1163 list $aux1 $aux2
1164 } {{x y z} {y x z}}
1165
fc77604c 1166 test {ZCARD basics} {
1167 $r zcard ztmp
1168 } {3}
1169
1170 test {ZCARD non existing key} {
1171 $r zcard ztmp-blabla
1172 } {0}
1173
d7f43c08 1174 test {ZSCORE} {
eaa256ad 1175 set aux {}
1176 set err {}
1177 for {set i 0} {$i < 1000} {incr i} {
1178 set score [expr rand()]
1179 lappend aux $score
1180 $r zadd zscoretest $score $i
1181 }
1182 for {set i 0} {$i < 1000} {incr i} {
1183 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1184 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1185 break
1186 }
1187 }
1188 set _ $err
1189 } {}
d7f43c08 1190
210e29f7 1191 test {ZSCORE after a DEBUG RELOAD} {
1192 set aux {}
1193 set err {}
1194 $r del zscoretest
1195 for {set i 0} {$i < 1000} {incr i} {
1196 set score [expr rand()]
1197 lappend aux $score
1198 $r zadd zscoretest $score $i
1199 }
1200 $r debug reload
1201 for {set i 0} {$i < 1000} {incr i} {
1202 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1203 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1204 break
1205 }
1206 }
1207 set _ $err
1208 } {}
1209
ac945e2d 1210 test {ZRANGE and ZREVRANGE basics} {
04c71068 1211 list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1] \
1212 [$r zrange ztmp 1 -1] [$r zrevrange ztmp 1 -1]
1213 } {{y x z} {z x y} {x z} {x y}}
d7f43c08 1214
81d45645 1215 test {ZRANGE WITHSCORES} {
1216 $r zrange ztmp 0 -1 withscores
1217 } {y 1 x 10 z 30}
1218
d7f43c08 1219 test {ZSETs stress tester - sorting is working well?} {
1220 set delta 0
1221 for {set test 0} {$test < 2} {incr test} {
1222 unset -nocomplain auxarray
1223 array set auxarray {}
1224 set auxlist {}
1225 $r del myzset
1226 for {set i 0} {$i < 1000} {incr i} {
1227 if {$test == 0} {
1228 set score [expr rand()]
1229 } else {
1230 set score [expr int(rand()*10)]
1231 }
1232 set auxarray($i) $score
1233 $r zadd myzset $score $i
1234 # Random update
1235 if {[expr rand()] < .2} {
1236 set j [expr int(rand()*1000)]
1237 if {$test == 0} {
1238 set score [expr rand()]
1239 } else {
1240 set score [expr int(rand()*10)]
1241 }
1242 set auxarray($j) $score
1243 $r zadd myzset $score $j
1244 }
1245 }
1246 foreach {item score} [array get auxarray] {
1247 lappend auxlist [list $score $item]
1248 }
1249 set sorted [lsort -command zlistAlikeSort $auxlist]
1250 set auxlist {}
1251 foreach x $sorted {
1252 lappend auxlist [lindex $x 1]
1253 }
1254 set fromredis [$r zrange myzset 0 -1]
1255 set delta 0
1256 for {set i 0} {$i < [llength $fromredis]} {incr i} {
1257 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1258 incr delta
1259 }
1260 }
1261 }
1262 format $delta
1263 } {0}
1264
28173a49 1265 test {ZINCRBY - can create a new sorted set} {
1266 $r del zset
1267 $r zincrby zset 1 foo
1268 list [$r zrange zset 0 -1] [$r zscore zset foo]
1269 } {foo 1}
1270
1271 test {ZINCRBY - increment and decrement} {
1272 $r zincrby zset 2 foo
1273 $r zincrby zset 1 bar
1274 set v1 [$r zrange zset 0 -1]
1275 $r zincrby zset 10 bar
1276 $r zincrby zset -5 foo
1277 $r zincrby zset -5 bar
1278 set v2 [$r zrange zset 0 -1]
1279 list $v1 $v2 [$r zscore zset foo] [$r zscore zset bar]
1280 } {{bar foo} {foo bar} -2 6}
1281
c74e7c77 1282 test {ZRANGEBYSCORE basics} {
1283 $r del zset
1284 $r zadd zset 1 a
1285 $r zadd zset 2 b
1286 $r zadd zset 3 c
1287 $r zadd zset 4 d
1288 $r zadd zset 5 e
1289 $r zrangebyscore zset 2 4
1290 } {b c d}
1291
5b1207c6 1292 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
1293 set err {}
1294 $r del zset
1295 for {set i 0} {$i < 1000} {incr i} {
1296 $r zadd zset [expr rand()] $i
1297 }
1298 for {set i 0} {$i < 100} {incr i} {
1299 set min [expr rand()]
1300 set max [expr rand()]
1301 if {$min > $max} {
1302 set aux $min
1303 set min $max
1304 set max $aux
1305 }
1306 set low [$r zrangebyscore zset -inf $min]
1307 set ok [$r zrangebyscore zset $min $max]
1308 set high [$r zrangebyscore zset $max +inf]
1309 foreach x $low {
1310 set score [$r zscore zset $x]
1311 if {$score > $min} {
1312 append err "Error, score for $x is $score > $min\n"
1313 }
1314 }
1315 foreach x $ok {
1316 set score [$r zscore zset $x]
1317 if {$score < $min || $score > $max} {
1318 append err "Error, score for $x is $score outside $min-$max range\n"
1319 }
1320 }
1321 foreach x $high {
1322 set score [$r zscore zset $x]
1323 if {$score < $max} {
1324 append err "Error, score for $x is $score < $max\n"
1325 }
1326 }
1327 }
1328 set _ $err
1329 } {}
1330
0b13687c 1331 test {ZRANGEBYSCORE with LIMIT} {
1332 $r del zset
1333 $r zadd zset 1 a
1334 $r zadd zset 2 b
1335 $r zadd zset 3 c
1336 $r zadd zset 4 d
1337 $r zadd zset 5 e
1338 list \
1339 [$r zrangebyscore zset 0 10 LIMIT 0 2] \
1340 [$r zrangebyscore zset 0 10 LIMIT 2 3] \
1341 [$r zrangebyscore zset 0 10 LIMIT 2 10] \
1342 [$r zrangebyscore zset 0 10 LIMIT 20 10]
1343 } {{a b} {c d e} {c d e} {}}
1344
9c21a518 1345 test {ZREMRANGE basics} {
1346 $r del zset
1347 $r zadd zset 1 a
1348 $r zadd zset 2 b
1349 $r zadd zset 3 c
1350 $r zadd zset 4 d
1351 $r zadd zset 5 e
1352 list [$r zremrangebyscore zset 2 4] [$r zrange zset 0 -1]
1353 } {3 {a e}}
1354
1355 test {ZREMRANGE from -inf to +inf} {
1356 $r del zset
1357 $r zadd zset 1 a
1358 $r zadd zset 2 b
1359 $r zadd zset 3 c
1360 $r zadd zset 4 d
1361 $r zadd zset 5 e
1362 list [$r zremrangebyscore zset -inf +inf] [$r zrange zset 0 -1]
1363 } {5 {}}
1364
0b13687c 1365 test {SORT against sorted sets} {
1366 $r del zset
1367 $r zadd zset 1 a
1368 $r zadd zset 5 b
1369 $r zadd zset 2 c
1370 $r zadd zset 10 d
1371 $r zadd zset 3 e
1372 $r sort zset alpha desc
1373 } {e d c b a}
1374
c74e7c77 1375 test {Sorted sets +inf and -inf handling} {
1376 $r del zset
1377 $r zadd zset -100 a
1378 $r zadd zset 200 b
1379 $r zadd zset -300 c
1380 $r zadd zset 1000000 d
1381 $r zadd zset +inf max
1382 $r zadd zset -inf min
1383 $r zrange zset 0 -1
1384 } {min c a b d max}
1385
b9febaab 1386 test {EXPIRE - don't set timeouts multiple times} {
1387 $r set x foobar
1388 set v1 [$r expire x 5]
1389 set v2 [$r ttl x]
1390 set v3 [$r expire x 10]
1391 set v4 [$r ttl x]
1392 list $v1 $v2 $v3 $v4
1393 } {1 5 0 5}
1394
1395 test {EXPIRE - It should be still possible to read 'x'} {
1396 $r get x
1397 } {foobar}
1398
1399 test {EXPIRE - After 6 seconds the key should no longer be here} {
1400 after 6000
1401 list [$r get x] [$r exists x]
1402 } {{} 0}
1403
1404 test {EXPIRE - Delete on write policy} {
1405 $r del x
1406 $r lpush x foo
1407 $r expire x 1000
1408 $r lpush x bar
1409 $r lrange x 0 -1
1410 } {bar}
1411
5446315f 1412 test {EXPIREAT - Check for EXPIRE alike behavior} {
1413 $r del x
1414 $r set x foo
1415 $r expireat x [expr [clock seconds]+15]
1416 $r ttl x
1417 } {1[345]}
1418
d7f43c08 1419 test {ZSETs skiplist implementation backlink consistency test} {
1420 set diff 0
1421 set elements 10000
1422 for {set j 0} {$j < $elements} {incr j} {
1423 $r zadd myzset [expr rand()] "Element-$j"
1424 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1425 }
1426 set l1 [$r zrange myzset 0 -1]
1427 set l2 [$r zrevrange myzset 0 -1]
1428 for {set j 0} {$j < [llength $l1]} {incr j} {
1429 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1430 incr diff
1431 }
1432 }
1433 format $diff
1434 } {0}
1435
75398fbc 1436 foreach fuzztype {binary alpha compr} {
1437 test "FUZZ stresser with data model $fuzztype" {
1438 set err 0
f69f2cba 1439 for {set i 0} {$i < 10000} {incr i} {
75398fbc 1440 set fuzz [randstring 0 512 $fuzztype]
1441 $r set foo $fuzz
1442 set got [$r get foo]
1443 if {$got ne $fuzz} {
f69f2cba 1444 set err [list $fuzz $got]
75398fbc 1445 break
1446 }
1447 }
f69f2cba 1448 set _ $err
75398fbc 1449 } {0}
1450 }
1451
0d36ded0 1452 test {BGSAVE} {
1453 $r flushdb
1454 $r save
1455 $r set x 10
1456 $r bgsave
1457 waitForBgsave $r
1458 $r debug reload
1459 $r get x
1460 } {10}
1461
7c49733c 1462 test {Handle an empty query well} {
1463 set fd [$r channel]
1464 puts -nonewline $fd "\r\n"
1465 flush $fd
1466 $r ping
1467 } {PONG}
1468
1469 test {Negative multi bulk command does not create problems} {
1470 set fd [$r channel]
1471 puts -nonewline $fd "*-10\r\n"
1472 flush $fd
1473 $r ping
1474 } {PONG}
1475
1476 test {Negative multi bulk payload} {
1477 set fd [$r channel]
1478 puts -nonewline $fd "SET x -10\r\n"
1479 flush $fd
1480 gets $fd
1481 } {*invalid bulk*}
1482
1483 test {Too big bulk payload} {
1484 set fd [$r channel]
1485 puts -nonewline $fd "SET x 2000000000\r\n"
1486 flush $fd
1487 gets $fd
1488 } {*invalid bulk*count*}
1489
1490 test {Multi bulk request not followed by bulk args} {
1491 set fd [$r channel]
1492 puts -nonewline $fd "*1\r\nfoo\r\n"
1493 flush $fd
1494 gets $fd
1495 } {*protocol error*}
1496
1497 test {Generic wrong number of args} {
1498 catch {$r ping x y z} err
1499 set _ $err
1500 } {*wrong*arguments*ping*}
1501
1502 test {SELECT an out of range DB} {
1503 catch {$r select 1000000} err
1504 set _ $err
1505 } {*invalid*}
1506
e054afda 1507 if {![catch {package require sha1}]} {
1508 test {Check consistency of different data types after a reload} {
1509 $r flushdb
1510 createComplexDataset $r 10000
1511 set sha1 [datasetDigest $r]
1512 $r debug reload
1513 set sha1_after [datasetDigest $r]
1514 expr {$sha1 eq $sha1_after}
1515 } {1}
e96e4fbf 1516
1517 test {Same dataset digest if saving/reloading as AOF?} {
1518 $r bgrewriteaof
1519 waitForBgrewriteaof $r
1520 $r debug loadaof
1521 set sha1_after [datasetDigest $r]
1522 expr {$sha1 eq $sha1_after}
1523 } {1}
e054afda 1524 }
1525
e96e4fbf 1526 test {EXPIRES after a reload (snapshot + append only file)} {
71c2b467 1527 $r flushdb
1528 $r set x 10
1529 $r expire x 1000
1530 $r save
1531 $r debug reload
1532 set ttl [$r ttl x]
e96e4fbf 1533 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
1534 $r bgrewriteaof
1535 waitForBgrewriteaof $r
1536 set ttl [$r ttl x]
1537 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
1538 list $e1 $e2
1539 } {1 1}
71c2b467 1540
483049a7 1541 test {PIPELINING stresser (also a regression for the old epoll bug)} {
1542 set fd2 [socket 127.0.0.1 6379]
1543 fconfigure $fd2 -encoding binary -translation binary
0447be2e 1544 puts -nonewline $fd2 "SELECT 9\r\n"
1545 flush $fd2
1546 gets $fd2
483049a7 1547
1548 for {set i 0} {$i < 100000} {incr i} {
1549 set q {}
1550 set val "0000${i}0000"
1551 append q "SET key:$i [string length $val]\r\n$val\r\n"
1552 puts -nonewline $fd2 $q
1553 set q {}
1554 append q "GET key:$i\r\n"
1555 puts -nonewline $fd2 $q
1556 }
1557 flush $fd2
1558
1559 for {set i 0} {$i < 100000} {incr i} {
1560 gets $fd2 line
1561 gets $fd2 count
1562 set count [string range $count 1 end]
1563 set val [read $fd2 $count]
1564 read $fd2 2
1565 }
1566 close $fd2
1567 set _ 1
1568 } {1}
1569
4409877e 1570 test {MUTLI / EXEC basics} {
1571 $r del mylist
1572 $r rpush mylist a
1573 $r rpush mylist b
1574 $r rpush mylist c
1575 $r multi
1576 set v1 [$r lrange mylist 0 -1]
1577 set v2 [$r ping]
1578 set v3 [$r exec]
1579 list $v1 $v2 $v3
1580 } {QUEUED QUEUED {{a b c} PONG}}
1581
ed9b544e 1582 # Leave the user with a clean DB before to exit
eea4baf7 1583 test {FLUSHDB} {
1584 set aux {}
1585 $r select 9
1586 $r flushdb
1587 lappend aux [$r dbsize]
1588 $r select 10
1589 $r flushdb
1590 lappend aux [$r dbsize]
1591 } {0 0}
ed9b544e 1592
c28b42ac 1593 test {Perform a final SAVE to leave a clean DB on disk} {
1594 $r save
1595 } {OK}
1596
17511391 1597 catch {
1598 if {[string match {*Darwin*} [exec uname -a]]} {
1599 test {Check for memory leaks} {
1600 exec leaks redis-server
1601 } {*0 leaks*}
1602 }
1603 }
1604
ed9b544e 1605 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1606 if {$::failed > 0} {
1607 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1608 }
ed9b544e 1609}
1610
5a6948fb 1611proc stress {} {
43c9dc7b 1612 set r [redis]
eea4baf7 1613 $r select 9
1614 $r flushdb
5a6948fb 1615 while 1 {
1616 set randkey [expr int(rand()*10000)]
1617 set randval [expr int(rand()*10000)]
1618 set randidx0 [expr int(rand()*10)]
1619 set randidx1 [expr int(rand()*10)]
12f9d551 1620 set cmd [expr int(rand()*20)]
43c9dc7b 1621 catch {
1622 if {$cmd == 0} {$r set $randkey $randval}
1623 if {$cmd == 1} {$r get $randkey}
1624 if {$cmd == 2} {$r incr $randkey}
1625 if {$cmd == 3} {$r lpush $randkey $randval}
1626 if {$cmd == 4} {$r rpop $randkey}
1627 if {$cmd == 5} {$r del $randkey}
12f9d551 1628 if {$cmd == 6} {$r llen $randkey}
1629 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1630 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
1631 if {$cmd == 9} {$r lindex $randkey $randidx0}
1632 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1633 if {$cmd == 11} {$r sadd $randkey $randval}
1634 if {$cmd == 12} {$r srem $randkey $randval}
1635 if {$cmd == 13} {$r smove $randkey $randval}
1636 if {$cmd == 14} {$r scard $randkey}
1637 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
43c9dc7b 1638 }
5a6948fb 1639 flush stdout
1640 }
eea4baf7 1641 $r flushdb
43c9dc7b 1642 $r close
5a6948fb 1643}
1644
fc77604c 1645# Set a few configuration defaults
1646set ::host 127.0.0.1
1647set ::port 6379
1648set ::stress 0
1649set ::flush 0
1650set ::first 0
1651set ::last 1000000
1652
1653# Parse arguments
1654for {set j 0} {$j < [llength $argv]} {incr j} {
1655 set opt [lindex $argv $j]
1656 set arg [lindex $argv [expr $j+1]]
1657 set lastarg [expr {$arg eq {}}]
1658 if {$opt eq {-h} && !$lastarg} {
1659 set ::host $arg
1660 incr j
1661 } elseif {$opt eq {-p} && !$lastarg} {
1662 set ::port $arg
1663 incr j
1664 } elseif {$opt eq {-stress}} {
1665 set ::stress 1
1666 } elseif {$opt eq {--flush}} {
1667 set ::flush 1
1668 } elseif {$opt eq {--first} && !$lastarg} {
1669 set ::first $arg
1670 incr j
1671 } elseif {$opt eq {--last} && !$lastarg} {
1672 set ::last $arg
1673 incr j
1674 } else {
7c49733c 1675 puts "Wrong argument: $opt"
fc77604c 1676 exit 1
1677 }
1678}
1679
eea4baf7 1680# Before to run the test check if DB 9 and DB 10 are empty
1681set r [redis]
fc77604c 1682
1683if {$::flush} {
1684 $r flushall
1685}
1686
eea4baf7 1687$r select 9
1688set db9size [$r dbsize]
1689$r select 10
1690set db10size [$r dbsize]
1691if {$db9size != 0 || $db10size != 0} {
1692 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1693 exit 1
1694}
1695$r close
1696unset r
1697unset db9size
1698unset db10size
1699
fc77604c 1700if {$::stress} {
5a6948fb 1701 stress
ed9b544e 1702} else {
fc77604c 1703 main $::host $::port
ed9b544e 1704}