]> git.saurik.com Git - redis.git/blob - test-redis.tcl
fix small error and memory leaks in SORT
[redis.git] / test-redis.tcl
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.
5
6 set tcl_precision 17
7 source redis.tcl
8
9 set ::passed 0
10 set ::failed 0
11 set ::testnum 0
12
13 proc test {name code okpattern} {
14 incr ::testnum
15 if {$::testnum < $::first || $::testnum > $::last} return
16 puts -nonewline [format "%-70s " "#$::testnum $name"]
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 if {$::traceleaks} {
27 if {![string match {*0 leaks*} [exec leaks redis-server]]} {
28 puts "--------- Test $::testnum LEAKED! --------"
29 exit 1
30 }
31 }
32 }
33
34 proc randstring {min max {type binary}} {
35 set len [expr {$min+int(rand()*($max-$min+1))}]
36 set output {}
37 if {$type eq {binary}} {
38 set minval 0
39 set maxval 255
40 } elseif {$type eq {alpha}} {
41 set minval 48
42 set maxval 122
43 } elseif {$type eq {compr}} {
44 set minval 48
45 set maxval 52
46 }
47 while {$len} {
48 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
49 incr len -1
50 }
51 return $output
52 }
53
54 # Useful for some test
55 proc zlistAlikeSort {a b} {
56 if {[lindex $a 0] > [lindex $b 0]} {return 1}
57 if {[lindex $a 0] < [lindex $b 0]} {return -1}
58 string compare [lindex $a 1] [lindex $b 1]
59 }
60
61 proc waitForBgsave r {
62 while 1 {
63 set i [$r info]
64 if {[string match {*bgsave_in_progress:1*} $i]} {
65 puts -nonewline "\nWaiting for background save to finish... "
66 flush stdout
67 after 1000
68 } else {
69 break
70 }
71 }
72 }
73
74 proc waitForBgrewriteaof r {
75 while 1 {
76 set i [$r info]
77 if {[string match {*bgrewriteaof_in_progress:1*} $i]} {
78 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
79 flush stdout
80 after 1000
81 } else {
82 break
83 }
84 }
85 }
86
87 proc randomInt {max} {
88 expr {int(rand()*$max)}
89 }
90
91 proc randpath args {
92 set path [expr {int(rand()*[llength $args])}]
93 uplevel 1 [lindex $args $path]
94 }
95
96 proc randomValue {} {
97 randpath {
98 # Small enough to likely collide
99 randomInt 1000
100 } {
101 # 32 bit compressible signed/unsigned
102 randpath {randomInt 2000000000} {randomInt 4000000000}
103 } {
104 # 64 bit
105 randpath {randomInt 1000000000000}
106 } {
107 # Random string
108 randpath {randstring 0 256 alpha} \
109 {randstring 0 256 compr} \
110 {randstring 0 256 binary}
111 }
112 }
113
114 proc randomKey {} {
115 randpath {
116 # Small enough to likely collide
117 randomInt 1000
118 } {
119 # 32 bit compressible signed/unsigned
120 randpath {randomInt 2000000000} {randomInt 4000000000}
121 } {
122 # 64 bit
123 randpath {randomInt 1000000000000}
124 } {
125 # Random string
126 randpath {randstring 1 256 alpha} \
127 {randstring 1 256 compr}
128 }
129 }
130
131 proc createComplexDataset {r ops} {
132 for {set j 0} {$j < $ops} {incr j} {
133 set k [randomKey]
134 set f [randomValue]
135 set v [randomValue]
136 randpath {
137 set d [expr {rand()}]
138 } {
139 set d [expr {rand()}]
140 } {
141 set d [expr {rand()}]
142 } {
143 set d [expr {rand()}]
144 } {
145 set d [expr {rand()}]
146 } {
147 randpath {set d +inf} {set d -inf}
148 }
149 set t [$r type $k]
150
151 if {$t eq {none}} {
152 randpath {
153 $r set $k $v
154 } {
155 $r lpush $k $v
156 } {
157 $r sadd $k $v
158 } {
159 $r zadd $k $d $v
160 } {
161 $r hset $k $f $v
162 }
163 set t [$r type $k]
164 }
165
166 switch $t {
167 {string} {
168 # Nothing to do
169 }
170 {list} {
171 randpath {$r lpush $k $v} \
172 {$r rpush $k $v} \
173 {$r lrem $k 0 $v} \
174 {$r rpop $k} \
175 {$r lpop $k}
176 }
177 {set} {
178 randpath {$r sadd $k $v} \
179 {$r srem $k $v}
180 }
181 {zset} {
182 randpath {$r zadd $k $d $v} \
183 {$r zrem $k $v}
184 }
185 {hash} {
186 randpath {$r hset $k $f $v} \
187 {$r hdel $k $f}
188 }
189 }
190 }
191 }
192
193 proc datasetDigest r {
194 set keys [lsort [$r keys *]]
195 set digest {}
196 foreach k $keys {
197 set t [$r type $k]
198 switch $t {
199 {string} {
200 set aux [::sha1::sha1 -hex [$r get $k]]
201 } {list} {
202 if {[$r llen $k] == 0} {
203 set aux {}
204 } else {
205 set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]]
206 }
207 } {set} {
208 if {[$r scard $k] == 0} {
209 set aux {}
210 } else {
211 set aux [::sha1::sha1 -hex [lsort [$r smembers $k]]]
212 }
213 } {zset} {
214 if {[$r zcard $k] == 0} {
215 set aux {}
216 } else {
217 set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]]
218 }
219 } {hash} {
220 if {[$r hlen $k] == 0} {
221 set aux {}
222 } else {
223 set aux [::sha1::sha1 -hex [lsort [$r hgetall $k]]]
224 }
225 } default {
226 error "Type not supported: $t"
227 }
228 }
229 if {$aux eq {}} continue
230 set digest [::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
231 }
232 return $digest
233 }
234
235 proc main {} {
236 set r [redis $::host $::port]
237 $r select 9
238 set err ""
239 set res ""
240
241 # The following AUTH test should be enabled only when requirepass
242 # <PASSWORD> is set in redis.conf and redis-server was started with
243 # redis.conf as the first argument.
244
245 #test {AUTH with requirepass in redis.conf} {
246 # $r auth foobared
247 #} {OK}
248
249 test {DEL all keys to start with a clean DB} {
250 foreach key [$r keys *] {$r del $key}
251 $r dbsize
252 } {0}
253
254 test {SET and GET an item} {
255 $r set x foobar
256 $r get x
257 } {foobar}
258
259 test {SET and GET an empty item} {
260 $r set x {}
261 $r get x
262 } {}
263
264 test {DEL against a single item} {
265 $r del x
266 $r get x
267 } {}
268
269 test {Vararg DEL} {
270 $r set foo1 a
271 $r set foo2 b
272 $r set foo3 c
273 list [$r del foo1 foo2 foo3 foo4] [$r mget foo1 foo2 foo3]
274 } {3 {{} {} {}}}
275
276 test {KEYS with pattern} {
277 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
278 $r set $key hello
279 }
280 lsort [$r keys foo*]
281 } {foo_a foo_b foo_c}
282
283 test {KEYS to get all keys} {
284 lsort [$r keys *]
285 } {foo_a foo_b foo_c key_x key_y key_z}
286
287 test {DBSIZE} {
288 $r dbsize
289 } {6}
290
291 test {DEL all keys} {
292 foreach key [$r keys *] {$r del $key}
293 $r dbsize
294 } {0}
295
296 test {Very big payload in GET/SET} {
297 set buf [string repeat "abcd" 1000000]
298 $r set foo $buf
299 $r get foo
300 } [string repeat "abcd" 1000000]
301
302 test {Very big payload random access} {
303 set err {}
304 array set payload {}
305 for {set j 0} {$j < 100} {incr j} {
306 set size [expr 1+[randomInt 100000]]
307 set buf [string repeat "pl-$j" $size]
308 set payload($j) $buf
309 $r set bigpayload_$j $buf
310 }
311 for {set j 0} {$j < 1000} {incr j} {
312 set index [randomInt 100]
313 set buf [$r get bigpayload_$index]
314 if {$buf != $payload($index)} {
315 set err "Values differ: I set '$payload($index)' but I read back '$buf'"
316 break
317 }
318 }
319 unset payload
320 set _ $err
321 } {}
322
323 test {SET 10000 numeric keys and access all them in reverse order} {
324 set err {}
325 for {set x 0} {$x < 10000} {incr x} {
326 $r set $x $x
327 }
328 set sum 0
329 for {set x 9999} {$x >= 0} {incr x -1} {
330 set val [$r get $x]
331 if {$val ne $x} {
332 set err "Eleemnt at position $x is $val instead of $x"
333 break
334 }
335 }
336 set _ $err
337 } {}
338
339 test {DBSIZE should be 10101 now} {
340 $r dbsize
341 } {10101}
342
343 test {INCR against non existing key} {
344 set res {}
345 append res [$r incr novar]
346 append res [$r get novar]
347 } {11}
348
349 test {INCR against key created by incr itself} {
350 $r incr novar
351 } {2}
352
353 test {INCR against key originally set with SET} {
354 $r set novar 100
355 $r incr novar
356 } {101}
357
358 test {INCR over 32bit value} {
359 $r set novar 17179869184
360 $r incr novar
361 } {17179869185}
362
363 test {INCRBY over 32bit value with over 32bit increment} {
364 $r set novar 17179869184
365 $r incrby novar 17179869184
366 } {34359738368}
367
368 test {INCR fails against key with spaces (no integer encoded)} {
369 $r set novar " 11 "
370 catch {$r incr novar} err
371 format $err
372 } {ERR*}
373
374 test {INCR fails against a key holding a list} {
375 $r rpush mylist 1
376 catch {$r incr novar} err
377 $r rpop mylist
378 format $err
379 } {ERR*}
380
381 test {DECRBY over 32bit value with over 32bit increment, negative res} {
382 $r set novar 17179869184
383 $r decrby novar 17179869185
384 } {-1}
385
386 test {SETNX target key missing} {
387 $r setnx novar2 foobared
388 $r get novar2
389 } {foobared}
390
391 test {SETNX target key exists} {
392 $r setnx novar2 blabla
393 $r get novar2
394 } {foobared}
395
396 test {SETNX will overwrite EXPIREing key} {
397 $r set x 10
398 $r expire x 10000
399 $r setnx x 20
400 $r get x
401 } {20}
402
403 test {EXISTS} {
404 set res {}
405 $r set newkey test
406 append res [$r exists newkey]
407 $r del newkey
408 append res [$r exists newkey]
409 } {10}
410
411 test {Zero length value in key. SET/GET/EXISTS} {
412 $r set emptykey {}
413 set res [$r get emptykey]
414 append res [$r exists emptykey]
415 $r del emptykey
416 append res [$r exists emptykey]
417 } {10}
418
419 test {Commands pipelining} {
420 set fd [$r channel]
421 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
422 flush $fd
423 set res {}
424 append res [string match OK* [::redis::redis_read_reply $fd]]
425 append res [::redis::redis_read_reply $fd]
426 append res [string match PONG* [::redis::redis_read_reply $fd]]
427 format $res
428 } {1xyzk1}
429
430 test {Non existing command} {
431 catch {$r foobaredcommand} err
432 string match ERR* $err
433 } {1}
434
435 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
436 set res [$r lpush mylist a]
437 append res [$r lpush mylist b]
438 append res [$r rpush mylist c]
439 append res [$r llen mylist]
440 append res [$r rpush anotherlist d]
441 append res [$r lpush anotherlist e]
442 append res [$r llen anotherlist]
443 append res [$r lindex mylist 0]
444 append res [$r lindex mylist 1]
445 append res [$r lindex mylist 2]
446 append res [$r lindex anotherlist 0]
447 append res [$r lindex anotherlist 1]
448 list $res [$r lindex mylist 100]
449 } {1233122baced {}}
450
451 test {DEL a list} {
452 $r del mylist
453 $r exists mylist
454 } {0}
455
456 test {Create a long list and check every single element with LINDEX} {
457 set ok 0
458 for {set i 0} {$i < 1000} {incr i} {
459 $r rpush mylist $i
460 }
461 for {set i 0} {$i < 1000} {incr i} {
462 if {[$r lindex mylist $i] eq $i} {incr ok}
463 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
464 incr ok
465 }
466 }
467 format $ok
468 } {2000}
469
470 test {Test elements with LINDEX in random access} {
471 set ok 0
472 for {set i 0} {$i < 1000} {incr i} {
473 set rint [expr int(rand()*1000)]
474 if {[$r lindex mylist $rint] eq $rint} {incr ok}
475 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
476 incr ok
477 }
478 }
479 format $ok
480 } {2000}
481
482 test {Check if the list is still ok after a DEBUG RELOAD} {
483 $r debug reload
484 set ok 0
485 for {set i 0} {$i < 1000} {incr i} {
486 set rint [expr int(rand()*1000)]
487 if {[$r lindex mylist $rint] eq $rint} {incr ok}
488 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
489 incr ok
490 }
491 }
492 format $ok
493 } {2000}
494
495 test {LLEN against non-list value error} {
496 $r del mylist
497 $r set mylist foobar
498 catch {$r llen mylist} err
499 format $err
500 } {ERR*}
501
502 test {LLEN against non existing key} {
503 $r llen not-a-key
504 } {0}
505
506 test {LINDEX against non-list value error} {
507 catch {$r lindex mylist 0} err
508 format $err
509 } {ERR*}
510
511 test {LINDEX against non existing key} {
512 $r lindex not-a-key 10
513 } {}
514
515 test {LPUSH against non-list value error} {
516 catch {$r lpush mylist 0} err
517 format $err
518 } {ERR*}
519
520 test {RPUSH against non-list value error} {
521 catch {$r rpush mylist 0} err
522 format $err
523 } {ERR*}
524
525 test {RPOPLPUSH base case} {
526 $r del mylist
527 $r rpush mylist a
528 $r rpush mylist b
529 $r rpush mylist c
530 $r rpush mylist d
531 set v1 [$r rpoplpush mylist newlist]
532 set v2 [$r rpoplpush mylist newlist]
533 set l1 [$r lrange mylist 0 -1]
534 set l2 [$r lrange newlist 0 -1]
535 list $v1 $v2 $l1 $l2
536 } {d c {a b} {c d}}
537
538 test {RPOPLPUSH with the same list as src and dst} {
539 $r del mylist
540 $r rpush mylist a
541 $r rpush mylist b
542 $r rpush mylist c
543 set l1 [$r lrange mylist 0 -1]
544 set v [$r rpoplpush mylist mylist]
545 set l2 [$r lrange mylist 0 -1]
546 list $l1 $v $l2
547 } {{a b c} c {c a b}}
548
549 test {RPOPLPUSH target list already exists} {
550 $r del mylist
551 $r del newlist
552 $r rpush mylist a
553 $r rpush mylist b
554 $r rpush mylist c
555 $r rpush mylist d
556 $r rpush newlist x
557 set v1 [$r rpoplpush mylist newlist]
558 set v2 [$r rpoplpush mylist newlist]
559 set l1 [$r lrange mylist 0 -1]
560 set l2 [$r lrange newlist 0 -1]
561 list $v1 $v2 $l1 $l2
562 } {d c {a b} {c d x}}
563
564 test {RPOPLPUSH against non existing key} {
565 $r del mylist
566 $r del newlist
567 set v1 [$r rpoplpush mylist newlist]
568 list $v1 [$r exists mylist] [$r exists newlist]
569 } {{} 0 0}
570
571 test {RPOPLPUSH against non list src key} {
572 $r del mylist
573 $r del newlist
574 $r set mylist x
575 catch {$r rpoplpush mylist newlist} err
576 list [$r type mylist] [$r exists newlist] [string range $err 0 2]
577 } {string 0 ERR}
578
579 test {RPOPLPUSH against non list dst key} {
580 $r del mylist
581 $r del newlist
582 $r rpush mylist a
583 $r rpush mylist b
584 $r rpush mylist c
585 $r rpush mylist d
586 $r set newlist x
587 catch {$r rpoplpush mylist newlist} err
588 list [$r lrange mylist 0 -1] [$r type newlist] [string range $err 0 2]
589 } {{a b c d} string ERR}
590
591 test {RPOPLPUSH against non existing src key} {
592 $r del mylist
593 $r del newlist
594 $r rpoplpush mylist newlist
595 } {}
596
597 test {RENAME basic usage} {
598 $r set mykey hello
599 $r rename mykey mykey1
600 $r rename mykey1 mykey2
601 $r get mykey2
602 } {hello}
603
604 test {RENAME source key should no longer exist} {
605 $r exists mykey
606 } {0}
607
608 test {RENAME against already existing key} {
609 $r set mykey a
610 $r set mykey2 b
611 $r rename mykey2 mykey
612 set res [$r get mykey]
613 append res [$r exists mykey2]
614 } {b0}
615
616 test {RENAMENX basic usage} {
617 $r del mykey
618 $r del mykey2
619 $r set mykey foobar
620 $r renamenx mykey mykey2
621 set res [$r get mykey2]
622 append res [$r exists mykey]
623 } {foobar0}
624
625 test {RENAMENX against already existing key} {
626 $r set mykey foo
627 $r set mykey2 bar
628 $r renamenx mykey mykey2
629 } {0}
630
631 test {RENAMENX against already existing key (2)} {
632 set res [$r get mykey]
633 append res [$r get mykey2]
634 } {foobar}
635
636 test {RENAME against non existing source key} {
637 catch {$r rename nokey foobar} err
638 format $err
639 } {ERR*}
640
641 test {RENAME where source and dest key is the same} {
642 catch {$r rename mykey mykey} err
643 format $err
644 } {ERR*}
645
646 test {DEL all keys again (DB 0)} {
647 foreach key [$r keys *] {
648 $r del $key
649 }
650 $r dbsize
651 } {0}
652
653 test {DEL all keys again (DB 1)} {
654 $r select 10
655 foreach key [$r keys *] {
656 $r del $key
657 }
658 set res [$r dbsize]
659 $r select 9
660 format $res
661 } {0}
662
663 test {MOVE basic usage} {
664 $r set mykey foobar
665 $r move mykey 10
666 set res {}
667 lappend res [$r exists mykey]
668 lappend res [$r dbsize]
669 $r select 10
670 lappend res [$r get mykey]
671 lappend res [$r dbsize]
672 $r select 9
673 format $res
674 } [list 0 0 foobar 1]
675
676 test {MOVE against key existing in the target DB} {
677 $r set mykey hello
678 $r move mykey 10
679 } {0}
680
681 test {SET/GET keys in different DBs} {
682 $r set a hello
683 $r set b world
684 $r select 10
685 $r set a foo
686 $r set b bared
687 $r select 9
688 set res {}
689 lappend res [$r get a]
690 lappend res [$r get b]
691 $r select 10
692 lappend res [$r get a]
693 lappend res [$r get b]
694 $r select 9
695 format $res
696 } {hello world foo bared}
697
698 test {Basic LPOP/RPOP} {
699 $r del mylist
700 $r rpush mylist 1
701 $r rpush mylist 2
702 $r lpush mylist 0
703 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
704 } [list 0 2 1 0]
705
706 test {LPOP/RPOP against empty list} {
707 $r lpop mylist
708 } {}
709
710 test {LPOP against non list value} {
711 $r set notalist foo
712 catch {$r lpop notalist} err
713 format $err
714 } {ERR*kind*}
715
716 test {Mass LPUSH/LPOP} {
717 set sum 0
718 for {set i 0} {$i < 1000} {incr i} {
719 $r lpush mylist $i
720 incr sum $i
721 }
722 set sum2 0
723 for {set i 0} {$i < 500} {incr i} {
724 incr sum2 [$r lpop mylist]
725 incr sum2 [$r rpop mylist]
726 }
727 expr $sum == $sum2
728 } {1}
729
730 test {LRANGE basics} {
731 for {set i 0} {$i < 10} {incr i} {
732 $r rpush mylist $i
733 }
734 list [$r lrange mylist 1 -2] \
735 [$r lrange mylist -3 -1] \
736 [$r lrange mylist 4 4]
737 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
738
739 test {LRANGE inverted indexes} {
740 $r lrange mylist 6 2
741 } {}
742
743 test {LRANGE out of range indexes including the full list} {
744 $r lrange mylist -1000 1000
745 } {0 1 2 3 4 5 6 7 8 9}
746
747 test {LRANGE against non existing key} {
748 $r lrange nosuchkey 0 1
749 } {}
750
751 test {LTRIM basics} {
752 $r del mylist
753 for {set i 0} {$i < 100} {incr i} {
754 $r lpush mylist $i
755 $r ltrim mylist 0 4
756 }
757 $r lrange mylist 0 -1
758 } {99 98 97 96 95}
759
760 test {LTRIM stress testing} {
761 set mylist {}
762 set err {}
763 for {set i 0} {$i < 20} {incr i} {
764 lappend mylist $i
765 }
766
767 for {set j 0} {$j < 100} {incr j} {
768 # Fill the list
769 $r del mylist
770 for {set i 0} {$i < 20} {incr i} {
771 $r rpush mylist $i
772 }
773 # Trim at random
774 set a [randomInt 20]
775 set b [randomInt 20]
776 $r ltrim mylist $a $b
777 if {[$r lrange mylist 0 -1] ne [lrange $mylist $a $b]} {
778 set err "[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
779 break
780 }
781 }
782 set _ $err
783 } {}
784
785 test {LSET} {
786 $r del mylist
787 foreach x {99 98 97 96 95} {
788 $r rpush mylist $x
789 }
790 $r lset mylist 1 foo
791 $r lset mylist -1 bar
792 $r lrange mylist 0 -1
793 } {99 foo 97 96 bar}
794
795 test {LSET out of range index} {
796 catch {$r lset mylist 10 foo} err
797 format $err
798 } {ERR*range*}
799
800 test {LSET against non existing key} {
801 catch {$r lset nosuchkey 10 foo} err
802 format $err
803 } {ERR*key*}
804
805 test {LSET against non list value} {
806 $r set nolist foobar
807 catch {$r lset nolist 0 foo} err
808 format $err
809 } {ERR*value*}
810
811 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
812 $r sadd myset foo
813 $r sadd myset bar
814 list [$r scard myset] [$r sismember myset foo] \
815 [$r sismember myset bar] [$r sismember myset bla] \
816 [lsort [$r smembers myset]]
817 } {2 1 1 0 {bar foo}}
818
819 test {SADD adding the same element multiple times} {
820 $r sadd myset foo
821 $r sadd myset foo
822 $r sadd myset foo
823 $r scard myset
824 } {2}
825
826 test {SADD against non set} {
827 catch {$r sadd mylist foo} err
828 format $err
829 } {ERR*kind*}
830
831 test {SREM basics} {
832 $r sadd myset ciao
833 $r srem myset foo
834 lsort [$r smembers myset]
835 } {bar ciao}
836
837 test {Mass SADD and SINTER with two sets} {
838 for {set i 0} {$i < 1000} {incr i} {
839 $r sadd set1 $i
840 $r sadd set2 [expr $i+995]
841 }
842 lsort [$r sinter set1 set2]
843 } {995 996 997 998 999}
844
845 test {SUNION with two sets} {
846 lsort [$r sunion set1 set2]
847 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
848
849 test {SINTERSTORE with two sets} {
850 $r sinterstore setres set1 set2
851 lsort [$r smembers setres]
852 } {995 996 997 998 999}
853
854 test {SINTERSTORE with two sets, after a DEBUG RELOAD} {
855 $r debug reload
856 $r sinterstore setres set1 set2
857 lsort [$r smembers setres]
858 } {995 996 997 998 999}
859
860 test {SUNIONSTORE with two sets} {
861 $r sunionstore setres set1 set2
862 lsort [$r smembers setres]
863 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
864
865 test {SUNIONSTORE against non existing keys} {
866 $r set setres xxx
867 list [$r sunionstore setres foo111 bar222] [$r exists xxx]
868 } {0 0}
869
870 test {SINTER against three sets} {
871 $r sadd set3 999
872 $r sadd set3 995
873 $r sadd set3 1000
874 $r sadd set3 2000
875 lsort [$r sinter set1 set2 set3]
876 } {995 999}
877
878 test {SINTERSTORE with three sets} {
879 $r sinterstore setres set1 set2 set3
880 lsort [$r smembers setres]
881 } {995 999}
882
883 test {SUNION with non existing keys} {
884 lsort [$r sunion nokey1 set1 set2 nokey2]
885 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
886
887 test {SDIFF with two sets} {
888 for {set i 5} {$i < 1000} {incr i} {
889 $r sadd set4 $i
890 }
891 lsort [$r sdiff set1 set4]
892 } {0 1 2 3 4}
893
894 test {SDIFF with three sets} {
895 $r sadd set5 0
896 lsort [$r sdiff set1 set4 set5]
897 } {1 2 3 4}
898
899 test {SDIFFSTORE with three sets} {
900 $r sdiffstore sres set1 set4 set5
901 lsort [$r smembers sres]
902 } {1 2 3 4}
903
904 test {SPOP basics} {
905 $r del myset
906 $r sadd myset 1
907 $r sadd myset 2
908 $r sadd myset 3
909 list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset]
910 } {{1 2 3} 0}
911
912 test {SAVE - make sure there are all the types as values} {
913 # Wait for a background saving in progress to terminate
914 waitForBgsave $r
915 $r lpush mysavelist hello
916 $r lpush mysavelist world
917 $r set myemptykey {}
918 $r set mynormalkey {blablablba}
919 $r zadd mytestzset 10 a
920 $r zadd mytestzset 20 b
921 $r zadd mytestzset 30 c
922 $r save
923 } {OK}
924
925 test {SRANDMEMBER} {
926 $r del myset
927 $r sadd myset a
928 $r sadd myset b
929 $r sadd myset c
930 unset -nocomplain myset
931 array set myset {}
932 for {set i 0} {$i < 100} {incr i} {
933 set myset([$r srandmember myset]) 1
934 }
935 lsort [array names myset]
936 } {a b c}
937
938 test {Create a random list and a random set} {
939 set tosort {}
940 array set seenrand {}
941 for {set i 0} {$i < 10000} {incr i} {
942 while 1 {
943 # Make sure all the weights are different because
944 # Redis does not use a stable sort but Tcl does.
945 randpath {
946 set rint [expr int(rand()*1000000)]
947 } {
948 set rint [expr rand()]
949 }
950 if {![info exists seenrand($rint)]} break
951 }
952 set seenrand($rint) x
953 $r lpush tosort $i
954 $r sadd tosort-set $i
955 $r set weight_$i $rint
956 $r hset wobj_$i weight $rint
957 lappend tosort [list $i $rint]
958 }
959 set sorted [lsort -index 1 -real $tosort]
960 set res {}
961 for {set i 0} {$i < 10000} {incr i} {
962 lappend res [lindex $sorted $i 0]
963 }
964 format {}
965 } {}
966
967 test {SORT with BY against the newly created list} {
968 $r sort tosort {BY weight_*}
969 } $res
970
971 test {SORT with BY (hash field) against the newly created list} {
972 $r sort tosort {BY wobj_*->weight}
973 } $res
974
975 test {SORT with GET (key+hash) with sanity check of each element (list)} {
976 set err {}
977 set l1 [$r sort tosort GET # GET weight_*]
978 set l2 [$r sort tosort GET # GET wobj_*->weight]
979 foreach {id1 w1} $l1 {id2 w2} $l2 {
980 set realweight [$r get weight_$id1]
981 if {$id1 != $id2} {
982 set err "ID mismatch $id1 != $id2"
983 break
984 }
985 if {$realweight != $w1 || $realweight != $w2} {
986 set err "Weights mismatch! w1: $w1 w2: $w2 real: $realweight"
987 break
988 }
989 }
990 set _ $err
991 } {}
992
993 test {SORT with BY, but against the newly created set} {
994 $r sort tosort-set {BY weight_*}
995 } $res
996
997 test {SORT with BY (hash field), but against the newly created set} {
998 $r sort tosort-set {BY wobj_*->weight}
999 } $res
1000
1001 test {SORT with BY and STORE against the newly created list} {
1002 $r sort tosort {BY weight_*} store sort-res
1003 $r lrange sort-res 0 -1
1004 } $res
1005
1006 test {SORT with BY (hash field) and STORE against the newly created list} {
1007 $r sort tosort {BY wobj_*->weight} store sort-res
1008 $r lrange sort-res 0 -1
1009 } $res
1010
1011 test {SORT direct, numeric, against the newly created list} {
1012 $r sort tosort
1013 } [lsort -integer $res]
1014
1015 test {SORT decreasing sort} {
1016 $r sort tosort {DESC}
1017 } [lsort -decreasing -integer $res]
1018
1019 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
1020 set start [clock clicks -milliseconds]
1021 for {set i 0} {$i < 100} {incr i} {
1022 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
1023 }
1024 set elapsed [expr [clock clicks -milliseconds]-$start]
1025 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
1026 flush stdout
1027 format {}
1028 } {}
1029
1030 test {SORT speed, as above but against hash field} {
1031 set start [clock clicks -milliseconds]
1032 for {set i 0} {$i < 100} {incr i} {
1033 set sorted [$r sort tosort {BY wobj_*->weight LIMIT 0 10}]
1034 }
1035 set elapsed [expr [clock clicks -milliseconds]-$start]
1036 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
1037 flush stdout
1038 format {}
1039 } {}
1040
1041 test {SORT speed, sorting 10000 elements list directly, 100 times} {
1042 set start [clock clicks -milliseconds]
1043 for {set i 0} {$i < 100} {incr i} {
1044 set sorted [$r sort tosort {LIMIT 0 10}]
1045 }
1046 set elapsed [expr [clock clicks -milliseconds]-$start]
1047 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
1048 flush stdout
1049 format {}
1050 } {}
1051
1052 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
1053 set start [clock clicks -milliseconds]
1054 for {set i 0} {$i < 100} {incr i} {
1055 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
1056 }
1057 set elapsed [expr [clock clicks -milliseconds]-$start]
1058 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
1059 flush stdout
1060 format {}
1061 } {}
1062
1063 test {SORT regression for issue #19, sorting floats} {
1064 $r flushdb
1065 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
1066 $r lpush mylist $x
1067 }
1068 $r sort mylist
1069 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
1070
1071 test {SORT with GET #} {
1072 $r del mylist
1073 $r lpush mylist 1
1074 $r lpush mylist 2
1075 $r lpush mylist 3
1076 $r mset weight_1 10 weight_2 5 weight_3 30
1077 $r sort mylist BY weight_* GET #
1078 } {2 1 3}
1079
1080 test {SORT with constant GET} {
1081 $r sort mylist GET foo
1082 } {{} {} {}}
1083
1084 test {LREM, remove all the occurrences} {
1085 $r flushdb
1086 $r rpush mylist foo
1087 $r rpush mylist bar
1088 $r rpush mylist foobar
1089 $r rpush mylist foobared
1090 $r rpush mylist zap
1091 $r rpush mylist bar
1092 $r rpush mylist test
1093 $r rpush mylist foo
1094 set res [$r lrem mylist 0 bar]
1095 list [$r lrange mylist 0 -1] $res
1096 } {{foo foobar foobared zap test foo} 2}
1097
1098 test {LREM, remove the first occurrence} {
1099 set res [$r lrem mylist 1 foo]
1100 list [$r lrange mylist 0 -1] $res
1101 } {{foobar foobared zap test foo} 1}
1102
1103 test {LREM, remove non existing element} {
1104 set res [$r lrem mylist 1 nosuchelement]
1105 list [$r lrange mylist 0 -1] $res
1106 } {{foobar foobared zap test foo} 0}
1107
1108 test {LREM, starting from tail with negative count} {
1109 $r flushdb
1110 $r rpush mylist foo
1111 $r rpush mylist bar
1112 $r rpush mylist foobar
1113 $r rpush mylist foobared
1114 $r rpush mylist zap
1115 $r rpush mylist bar
1116 $r rpush mylist test
1117 $r rpush mylist foo
1118 $r rpush mylist foo
1119 set res [$r lrem mylist -1 bar]
1120 list [$r lrange mylist 0 -1] $res
1121 } {{foo bar foobar foobared zap test foo foo} 1}
1122
1123 test {LREM, starting from tail with negative count (2)} {
1124 set res [$r lrem mylist -2 foo]
1125 list [$r lrange mylist 0 -1] $res
1126 } {{foo bar foobar foobared zap test} 2}
1127
1128 test {LREM, deleting objects that may be encoded as integers} {
1129 $r lpush myotherlist 1
1130 $r lpush myotherlist 2
1131 $r lpush myotherlist 3
1132 $r lrem myotherlist 1 2
1133 $r llen myotherlist
1134 } {2}
1135
1136 test {MGET} {
1137 $r flushdb
1138 $r set foo BAR
1139 $r set bar FOO
1140 $r mget foo bar
1141 } {BAR FOO}
1142
1143 test {MGET against non existing key} {
1144 $r mget foo baazz bar
1145 } {BAR {} FOO}
1146
1147 test {MGET against non-string key} {
1148 $r sadd myset ciao
1149 $r sadd myset bau
1150 $r mget foo baazz bar myset
1151 } {BAR {} FOO {}}
1152
1153 test {RANDOMKEY} {
1154 $r flushdb
1155 $r set foo x
1156 $r set bar y
1157 set foo_seen 0
1158 set bar_seen 0
1159 for {set i 0} {$i < 100} {incr i} {
1160 set rkey [$r randomkey]
1161 if {$rkey eq {foo}} {
1162 set foo_seen 1
1163 }
1164 if {$rkey eq {bar}} {
1165 set bar_seen 1
1166 }
1167 }
1168 list $foo_seen $bar_seen
1169 } {1 1}
1170
1171 test {RANDOMKEY against empty DB} {
1172 $r flushdb
1173 $r randomkey
1174 } {}
1175
1176 test {RANDOMKEY regression 1} {
1177 $r flushdb
1178 $r set x 10
1179 $r del x
1180 $r randomkey
1181 } {}
1182
1183 test {GETSET (set new value)} {
1184 list [$r getset foo xyz] [$r get foo]
1185 } {{} xyz}
1186
1187 test {GETSET (replace old value)} {
1188 $r set foo bar
1189 list [$r getset foo xyz] [$r get foo]
1190 } {bar xyz}
1191
1192 test {SMOVE basics} {
1193 $r sadd myset1 a
1194 $r sadd myset1 b
1195 $r sadd myset1 c
1196 $r sadd myset2 x
1197 $r sadd myset2 y
1198 $r sadd myset2 z
1199 $r smove myset1 myset2 a
1200 list [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1201 } {{a x y z} {b c}}
1202
1203 test {SMOVE non existing key} {
1204 list [$r smove myset1 myset2 foo] [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1205 } {0 {a x y z} {b c}}
1206
1207 test {SMOVE non existing src set} {
1208 list [$r smove noset myset2 foo] [lsort [$r smembers myset2]]
1209 } {0 {a x y z}}
1210
1211 test {SMOVE non existing dst set} {
1212 list [$r smove myset2 myset3 y] [lsort [$r smembers myset2]] [lsort [$r smembers myset3]]
1213 } {1 {a x z} y}
1214
1215 test {SMOVE wrong src key type} {
1216 $r set x 10
1217 catch {$r smove x myset2 foo} err
1218 format $err
1219 } {ERR*}
1220
1221 test {SMOVE wrong dst key type} {
1222 $r set x 10
1223 catch {$r smove myset2 x foo} err
1224 format $err
1225 } {ERR*}
1226
1227 test {MSET base case} {
1228 $r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n"
1229 $r mget x y z
1230 } [list 10 {foo bar} "x x x x x x x\n\n\r\n"]
1231
1232 test {MSET wrong number of args} {
1233 catch {$r mset x 10 y "foo bar" z} err
1234 format $err
1235 } {*wrong number*}
1236
1237 test {MSETNX with already existent key} {
1238 list [$r msetnx x1 xxx y2 yyy x 20] [$r exists x1] [$r exists y2]
1239 } {0 0 0}
1240
1241 test {MSETNX with not existing keys} {
1242 list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2]
1243 } {1 xxx yyy}
1244
1245 test {MSETNX should remove all the volatile keys even on failure} {
1246 $r mset x 1 y 2 z 3
1247 $r expire y 10000
1248 $r expire z 10000
1249 list [$r msetnx x A y B z C] [$r mget x y z]
1250 } {0 {1 {} {}}}
1251
1252 test {ZSET basic ZADD and score update} {
1253 $r zadd ztmp 10 x
1254 $r zadd ztmp 20 y
1255 $r zadd ztmp 30 z
1256 set aux1 [$r zrange ztmp 0 -1]
1257 $r zadd ztmp 1 y
1258 set aux2 [$r zrange ztmp 0 -1]
1259 list $aux1 $aux2
1260 } {{x y z} {y x z}}
1261
1262 test {ZCARD basics} {
1263 $r zcard ztmp
1264 } {3}
1265
1266 test {ZCARD non existing key} {
1267 $r zcard ztmp-blabla
1268 } {0}
1269
1270 test {ZRANK basics} {
1271 $r zadd zranktmp 10 x
1272 $r zadd zranktmp 20 y
1273 $r zadd zranktmp 30 z
1274 list [$r zrank zranktmp x] [$r zrank zranktmp y] [$r zrank zranktmp z]
1275 } {0 1 2}
1276
1277 test {ZREVRANK basics} {
1278 list [$r zrevrank zranktmp x] [$r zrevrank zranktmp y] [$r zrevrank zranktmp z]
1279 } {2 1 0}
1280
1281 test {ZRANK - after deletion} {
1282 $r zrem zranktmp y
1283 list [$r zrank zranktmp x] [$r zrank zranktmp z]
1284 } {0 1}
1285
1286 test {ZSCORE} {
1287 set aux {}
1288 set err {}
1289 for {set i 0} {$i < 1000} {incr i} {
1290 set score [expr rand()]
1291 lappend aux $score
1292 $r zadd zscoretest $score $i
1293 }
1294 for {set i 0} {$i < 1000} {incr i} {
1295 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1296 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1297 break
1298 }
1299 }
1300 set _ $err
1301 } {}
1302
1303 test {ZSCORE after a DEBUG RELOAD} {
1304 set aux {}
1305 set err {}
1306 $r del zscoretest
1307 for {set i 0} {$i < 1000} {incr i} {
1308 set score [expr rand()]
1309 lappend aux $score
1310 $r zadd zscoretest $score $i
1311 }
1312 $r debug reload
1313 for {set i 0} {$i < 1000} {incr i} {
1314 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1315 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1316 break
1317 }
1318 }
1319 set _ $err
1320 } {}
1321
1322 test {ZRANGE and ZREVRANGE basics} {
1323 list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1] \
1324 [$r zrange ztmp 1 -1] [$r zrevrange ztmp 1 -1]
1325 } {{y x z} {z x y} {x z} {x y}}
1326
1327 test {ZRANGE WITHSCORES} {
1328 $r zrange ztmp 0 -1 withscores
1329 } {y 1 x 10 z 30}
1330
1331 test {ZSETs stress tester - sorting is working well?} {
1332 set delta 0
1333 for {set test 0} {$test < 2} {incr test} {
1334 unset -nocomplain auxarray
1335 array set auxarray {}
1336 set auxlist {}
1337 $r del myzset
1338 for {set i 0} {$i < 1000} {incr i} {
1339 if {$test == 0} {
1340 set score [expr rand()]
1341 } else {
1342 set score [expr int(rand()*10)]
1343 }
1344 set auxarray($i) $score
1345 $r zadd myzset $score $i
1346 # Random update
1347 if {[expr rand()] < .2} {
1348 set j [expr int(rand()*1000)]
1349 if {$test == 0} {
1350 set score [expr rand()]
1351 } else {
1352 set score [expr int(rand()*10)]
1353 }
1354 set auxarray($j) $score
1355 $r zadd myzset $score $j
1356 }
1357 }
1358 foreach {item score} [array get auxarray] {
1359 lappend auxlist [list $score $item]
1360 }
1361 set sorted [lsort -command zlistAlikeSort $auxlist]
1362 set auxlist {}
1363 foreach x $sorted {
1364 lappend auxlist [lindex $x 1]
1365 }
1366 set fromredis [$r zrange myzset 0 -1]
1367 set delta 0
1368 for {set i 0} {$i < [llength $fromredis]} {incr i} {
1369 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1370 incr delta
1371 }
1372 }
1373 }
1374 format $delta
1375 } {0}
1376
1377 test {ZINCRBY - can create a new sorted set} {
1378 $r del zset
1379 $r zincrby zset 1 foo
1380 list [$r zrange zset 0 -1] [$r zscore zset foo]
1381 } {foo 1}
1382
1383 test {ZINCRBY - increment and decrement} {
1384 $r zincrby zset 2 foo
1385 $r zincrby zset 1 bar
1386 set v1 [$r zrange zset 0 -1]
1387 $r zincrby zset 10 bar
1388 $r zincrby zset -5 foo
1389 $r zincrby zset -5 bar
1390 set v2 [$r zrange zset 0 -1]
1391 list $v1 $v2 [$r zscore zset foo] [$r zscore zset bar]
1392 } {{bar foo} {foo bar} -2 6}
1393
1394 test {ZRANGEBYSCORE and ZCOUNT basics} {
1395 $r del zset
1396 $r zadd zset 1 a
1397 $r zadd zset 2 b
1398 $r zadd zset 3 c
1399 $r zadd zset 4 d
1400 $r zadd zset 5 e
1401 list [$r zrangebyscore zset 2 4] [$r zrangebyscore zset (2 (4] \
1402 [$r zcount zset 2 4] [$r zcount zset (2 (4]
1403 } {{b c d} c 3 1}
1404
1405 test {ZRANGEBYSCORE withscores} {
1406 $r del zset
1407 $r zadd zset 1 a
1408 $r zadd zset 2 b
1409 $r zadd zset 3 c
1410 $r zadd zset 4 d
1411 $r zadd zset 5 e
1412 $r zrangebyscore zset 2 4 withscores
1413 } {b 2 c 3 d 4}
1414
1415 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
1416 set err {}
1417 $r del zset
1418 for {set i 0} {$i < 1000} {incr i} {
1419 $r zadd zset [expr rand()] $i
1420 }
1421 for {set i 0} {$i < 100} {incr i} {
1422 set min [expr rand()]
1423 set max [expr rand()]
1424 if {$min > $max} {
1425 set aux $min
1426 set min $max
1427 set max $aux
1428 }
1429 set low [$r zrangebyscore zset -inf $min]
1430 set ok [$r zrangebyscore zset $min $max]
1431 set high [$r zrangebyscore zset $max +inf]
1432 set lowx [$r zrangebyscore zset -inf ($min]
1433 set okx [$r zrangebyscore zset ($min ($max]
1434 set highx [$r zrangebyscore zset ($max +inf]
1435
1436 if {[$r zcount zset -inf $min] != [llength $low]} {
1437 append err "Error, len does not match zcount\n"
1438 }
1439 if {[$r zcount zset $min $max] != [llength $ok]} {
1440 append err "Error, len does not match zcount\n"
1441 }
1442 if {[$r zcount zset $max +inf] != [llength $high]} {
1443 append err "Error, len does not match zcount\n"
1444 }
1445 if {[$r zcount zset -inf ($min] != [llength $lowx]} {
1446 append err "Error, len does not match zcount\n"
1447 }
1448 if {[$r zcount zset ($min ($max] != [llength $okx]} {
1449 append err "Error, len does not match zcount\n"
1450 }
1451 if {[$r zcount zset ($max +inf] != [llength $highx]} {
1452 append err "Error, len does not match zcount\n"
1453 }
1454
1455 foreach x $low {
1456 set score [$r zscore zset $x]
1457 if {$score > $min} {
1458 append err "Error, score for $x is $score > $min\n"
1459 }
1460 }
1461 foreach x $lowx {
1462 set score [$r zscore zset $x]
1463 if {$score >= $min} {
1464 append err "Error, score for $x is $score >= $min\n"
1465 }
1466 }
1467 foreach x $ok {
1468 set score [$r zscore zset $x]
1469 if {$score < $min || $score > $max} {
1470 append err "Error, score for $x is $score outside $min-$max range\n"
1471 }
1472 }
1473 foreach x $okx {
1474 set score [$r zscore zset $x]
1475 if {$score <= $min || $score >= $max} {
1476 append err "Error, score for $x is $score outside $min-$max open range\n"
1477 }
1478 }
1479 foreach x $high {
1480 set score [$r zscore zset $x]
1481 if {$score < $max} {
1482 append err "Error, score for $x is $score < $max\n"
1483 }
1484 }
1485 foreach x $highx {
1486 set score [$r zscore zset $x]
1487 if {$score <= $max} {
1488 append err "Error, score for $x is $score <= $max\n"
1489 }
1490 }
1491 }
1492 set _ $err
1493 } {}
1494
1495 test {ZRANGEBYSCORE with LIMIT} {
1496 $r del zset
1497 $r zadd zset 1 a
1498 $r zadd zset 2 b
1499 $r zadd zset 3 c
1500 $r zadd zset 4 d
1501 $r zadd zset 5 e
1502 list \
1503 [$r zrangebyscore zset 0 10 LIMIT 0 2] \
1504 [$r zrangebyscore zset 0 10 LIMIT 2 3] \
1505 [$r zrangebyscore zset 0 10 LIMIT 2 10] \
1506 [$r zrangebyscore zset 0 10 LIMIT 20 10]
1507 } {{a b} {c d e} {c d e} {}}
1508
1509 test {ZRANGEBYSCORE with LIMIT and withscores} {
1510 $r del zset
1511 $r zadd zset 10 a
1512 $r zadd zset 20 b
1513 $r zadd zset 30 c
1514 $r zadd zset 40 d
1515 $r zadd zset 50 e
1516 $r zrangebyscore zset 20 50 LIMIT 2 3 withscores
1517 } {d 40 e 50}
1518
1519 test {ZREMRANGEBYSCORE basics} {
1520 $r del zset
1521 $r zadd zset 1 a
1522 $r zadd zset 2 b
1523 $r zadd zset 3 c
1524 $r zadd zset 4 d
1525 $r zadd zset 5 e
1526 list [$r zremrangebyscore zset 2 4] [$r zrange zset 0 -1]
1527 } {3 {a e}}
1528
1529 test {ZREMRANGEBYSCORE from -inf to +inf} {
1530 $r del zset
1531 $r zadd zset 1 a
1532 $r zadd zset 2 b
1533 $r zadd zset 3 c
1534 $r zadd zset 4 d
1535 $r zadd zset 5 e
1536 list [$r zremrangebyscore zset -inf +inf] [$r zrange zset 0 -1]
1537 } {5 {}}
1538
1539 test {ZREMRANGEBYRANK basics} {
1540 $r del zset
1541 $r zadd zset 1 a
1542 $r zadd zset 2 b
1543 $r zadd zset 3 c
1544 $r zadd zset 4 d
1545 $r zadd zset 5 e
1546 list [$r zremrangebyrank zset 1 3] [$r zrange zset 0 -1]
1547 } {3 {a e}}
1548
1549 test {ZUNION against non-existing key doesn't set destination} {
1550 $r del zseta
1551 list [$r zunion dst_key 1 zseta] [$r exists dst_key]
1552 } {0 0}
1553
1554 test {ZUNION basics} {
1555 $r del zseta zsetb zsetc
1556 $r zadd zseta 1 a
1557 $r zadd zseta 2 b
1558 $r zadd zseta 3 c
1559 $r zadd zsetb 1 b
1560 $r zadd zsetb 2 c
1561 $r zadd zsetb 3 d
1562 list [$r zunion zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
1563 } {4 {a 1 b 3 d 3 c 5}}
1564
1565 test {ZUNION with weights} {
1566 list [$r zunion zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
1567 } {4 {a 2 b 7 d 9 c 12}}
1568
1569 test {ZUNION with AGGREGATE MIN} {
1570 list [$r zunion zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1571 } {4 {a 1 b 1 c 2 d 3}}
1572
1573 test {ZUNION with AGGREGATE MAX} {
1574 list [$r zunion zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1575 } {4 {a 1 b 2 c 3 d 3}}
1576
1577 test {ZINTER basics} {
1578 list [$r zinter zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
1579 } {2 {b 3 c 5}}
1580
1581 test {ZINTER with weights} {
1582 list [$r zinter zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
1583 } {2 {b 7 c 12}}
1584
1585 test {ZINTER with AGGREGATE MIN} {
1586 list [$r zinter zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1587 } {2 {b 1 c 2}}
1588
1589 test {ZINTER with AGGREGATE MAX} {
1590 list [$r zinter zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1591 } {2 {b 2 c 3}}
1592
1593 test {SORT against sorted sets} {
1594 $r del zset
1595 $r zadd zset 1 a
1596 $r zadd zset 5 b
1597 $r zadd zset 2 c
1598 $r zadd zset 10 d
1599 $r zadd zset 3 e
1600 $r sort zset alpha desc
1601 } {e d c b a}
1602
1603 test {Sorted sets +inf and -inf handling} {
1604 $r del zset
1605 $r zadd zset -100 a
1606 $r zadd zset 200 b
1607 $r zadd zset -300 c
1608 $r zadd zset 1000000 d
1609 $r zadd zset +inf max
1610 $r zadd zset -inf min
1611 $r zrange zset 0 -1
1612 } {min c a b d max}
1613
1614 test {HSET/HLEN - Small hash creation} {
1615 array set smallhash {}
1616 for {set i 0} {$i < 8} {incr i} {
1617 set key [randstring 0 8 alpha]
1618 set val [randstring 0 8 alpha]
1619 if {[info exists smallhash($key)]} {
1620 incr i -1
1621 continue
1622 }
1623 $r hset smallhash $key $val
1624 set smallhash($key) $val
1625 }
1626 list [$r hlen smallhash]
1627 } {8}
1628
1629 test {Is the small hash encoded with a zipmap?} {
1630 $r debug object smallhash
1631 } {*zipmap*}
1632
1633 test {HSET/HLEN - Big hash creation} {
1634 array set bighash {}
1635 for {set i 0} {$i < 1024} {incr i} {
1636 set key [randstring 0 8 alpha]
1637 set val [randstring 0 8 alpha]
1638 if {[info exists bighash($key)]} {
1639 incr i -1
1640 continue
1641 }
1642 $r hset bighash $key $val
1643 set bighash($key) $val
1644 }
1645 list [$r hlen bighash]
1646 } {1024}
1647
1648 test {Is the big hash encoded with a zipmap?} {
1649 $r debug object bighash
1650 } {*hashtable*}
1651
1652 test {HGET against the small hash} {
1653 set err {}
1654 foreach k [array names smallhash *] {
1655 if {$smallhash($k) ne [$r hget smallhash $k]} {
1656 set err "$smallhash($k) != [$r hget smallhash $k]"
1657 break
1658 }
1659 }
1660 set _ $err
1661 } {}
1662
1663 test {HGET against the big hash} {
1664 set err {}
1665 foreach k [array names bighash *] {
1666 if {$bighash($k) ne [$r hget bighash $k]} {
1667 set err "$bighash($k) != [$r hget bighash $k]"
1668 break
1669 }
1670 }
1671 set _ $err
1672 } {}
1673
1674 test {HGET against non existing key} {
1675 set rv {}
1676 lappend rv [$r hget smallhash __123123123__]
1677 lappend rv [$r hget bighash __123123123__]
1678 set _ $rv
1679 } {{} {}}
1680
1681 test {HSET in update and insert mode} {
1682 set rv {}
1683 set k [lindex [array names smallhash *] 0]
1684 lappend rv [$r hset smallhash $k newval1]
1685 set smallhash($k) newval1
1686 lappend rv [$r hget smallhash $k]
1687 lappend rv [$r hset smallhash __foobar123__ newval]
1688 set k [lindex [array names bighash *] 0]
1689 lappend rv [$r hset bighash $k newval2]
1690 set bighash($k) newval2
1691 lappend rv [$r hget bighash $k]
1692 lappend rv [$r hset bighash __foobar123__ newval]
1693 lappend rv [$r hdel smallhash __foobar123__]
1694 lappend rv [$r hdel bighash __foobar123__]
1695 set _ $rv
1696 } {0 newval1 1 0 newval2 1 1 1}
1697
1698 test {HSETNX target key missing - small hash} {
1699 $r hsetnx smallhash __123123123__ foo
1700 $r hget smallhash __123123123__
1701 } {foo}
1702
1703 test {HSETNX target key exists - small hash} {
1704 $r hsetnx smallhash __123123123__ bar
1705 set result [$r hget smallhash __123123123__]
1706 $r hdel smallhash __123123123__
1707 set _ $result
1708 } {foo}
1709
1710 test {HSETNX target key missing - big hash} {
1711 $r hsetnx bighash __123123123__ foo
1712 $r hget bighash __123123123__
1713 } {foo}
1714
1715 test {HSETNX target key exists - big hash} {
1716 $r hsetnx bighash __123123123__ bar
1717 set result [$r hget bighash __123123123__]
1718 $r hdel bighash __123123123__
1719 set _ $result
1720 } {foo}
1721
1722 test {HMSET wrong number of args} {
1723 catch {$r hmset smallhash key1 val1 key2} err
1724 format $err
1725 } {*wrong number*}
1726
1727 test {HMSET - small hash} {
1728 set args {}
1729 foreach {k v} [array get smallhash] {
1730 set newval [randstring 0 8 alpha]
1731 set smallhash($k) $newval
1732 lappend args $k $newval
1733 }
1734 $r hmset smallhash {*}$args
1735 } {OK}
1736
1737 test {HMSET - big hash} {
1738 set args {}
1739 foreach {k v} [array get bighash] {
1740 set newval [randstring 0 8 alpha]
1741 set bighash($k) $newval
1742 lappend args $k $newval
1743 }
1744 $r hmset bighash {*}$args
1745 } {OK}
1746
1747 test {HMGET against non existing key and fields} {
1748 set rv {}
1749 lappend rv [$r hmget doesntexist __123123123__ __456456456__]
1750 lappend rv [$r hmget smallhash __123123123__ __456456456__]
1751 lappend rv [$r hmget bighash __123123123__ __456456456__]
1752 set _ $rv
1753 } {{{} {}} {{} {}} {{} {}}}
1754
1755 test {HMGET - small hash} {
1756 set keys {}
1757 set vals {}
1758 foreach {k v} [array get smallhash] {
1759 lappend keys $k
1760 lappend vals $v
1761 }
1762 set err {}
1763 set result [$r hmget smallhash {*}$keys]
1764 if {$vals ne $result} {
1765 set err "$vals != $result"
1766 break
1767 }
1768 set _ $err
1769 } {}
1770
1771 test {HMGET - big hash} {
1772 set keys {}
1773 set vals {}
1774 foreach {k v} [array get bighash] {
1775 lappend keys $k
1776 lappend vals $v
1777 }
1778 set err {}
1779 set result [$r hmget bighash {*}$keys]
1780 if {$vals ne $result} {
1781 set err "$vals != $result"
1782 break
1783 }
1784 set _ $err
1785 } {}
1786
1787 test {HKEYS - small hash} {
1788 lsort [$r hkeys smallhash]
1789 } [lsort [array names smallhash *]]
1790
1791 test {HKEYS - big hash} {
1792 lsort [$r hkeys bighash]
1793 } [lsort [array names bighash *]]
1794
1795 test {HVALS - small hash} {
1796 set vals {}
1797 foreach {k v} [array get smallhash] {
1798 lappend vals $v
1799 }
1800 set _ [lsort $vals]
1801 } [lsort [$r hvals smallhash]]
1802
1803 test {HVALS - big hash} {
1804 set vals {}
1805 foreach {k v} [array get bighash] {
1806 lappend vals $v
1807 }
1808 set _ [lsort $vals]
1809 } [lsort [$r hvals bighash]]
1810
1811 test {HGETALL - small hash} {
1812 lsort [$r hgetall smallhash]
1813 } [lsort [array get smallhash]]
1814
1815 test {HGETALL - big hash} {
1816 lsort [$r hgetall bighash]
1817 } [lsort [array get bighash]]
1818
1819 test {HDEL and return value} {
1820 set rv {}
1821 lappend rv [$r hdel smallhash nokey]
1822 lappend rv [$r hdel bighash nokey]
1823 set k [lindex [array names smallhash *] 0]
1824 lappend rv [$r hdel smallhash $k]
1825 lappend rv [$r hdel smallhash $k]
1826 lappend rv [$r hget smallhash $k]
1827 unset smallhash($k)
1828 set k [lindex [array names bighash *] 0]
1829 lappend rv [$r hdel bighash $k]
1830 lappend rv [$r hdel bighash $k]
1831 lappend rv [$r hget bighash $k]
1832 unset bighash($k)
1833 set _ $rv
1834 } {0 0 1 0 {} 1 0 {}}
1835
1836 test {HEXISTS} {
1837 set rv {}
1838 set k [lindex [array names smallhash *] 0]
1839 lappend rv [$r hexists smallhash $k]
1840 lappend rv [$r hexists smallhash nokey]
1841 set k [lindex [array names bighash *] 0]
1842 lappend rv [$r hexists bighash $k]
1843 lappend rv [$r hexists bighash nokey]
1844 } {1 0 1 0}
1845
1846 test {Is a zipmap encoded Hash promoted on big payload?} {
1847 $r hset smallhash foo [string repeat a 1024]
1848 $r debug object smallhash
1849 } {*hashtable*}
1850
1851 test {HINCRBY against non existing database key} {
1852 $r del htest
1853 list [$r hincrby htest foo 2]
1854 } {2}
1855
1856 test {HINCRBY against non existing hash key} {
1857 set rv {}
1858 $r hdel smallhash tmp
1859 $r hdel bighash tmp
1860 lappend rv [$r hincrby smallhash tmp 2]
1861 lappend rv [$r hget smallhash tmp]
1862 lappend rv [$r hincrby bighash tmp 2]
1863 lappend rv [$r hget bighash tmp]
1864 } {2 2 2 2}
1865
1866 test {HINCRBY against hash key created by hincrby itself} {
1867 set rv {}
1868 lappend rv [$r hincrby smallhash tmp 3]
1869 lappend rv [$r hget smallhash tmp]
1870 lappend rv [$r hincrby bighash tmp 3]
1871 lappend rv [$r hget bighash tmp]
1872 } {5 5 5 5}
1873
1874 test {HINCRBY against hash key originally set with HSET} {
1875 $r hset smallhash tmp 100
1876 $r hset bighash tmp 100
1877 list [$r hincrby smallhash tmp 2] [$r hincrby bighash tmp 2]
1878 } {102 102}
1879
1880 test {HINCRBY over 32bit value} {
1881 $r hset smallhash tmp 17179869184
1882 $r hset bighash tmp 17179869184
1883 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1884 } {17179869185 17179869185}
1885
1886 test {HINCRBY over 32bit value with over 32bit increment} {
1887 $r hset smallhash tmp 17179869184
1888 $r hset bighash tmp 17179869184
1889 list [$r hincrby smallhash tmp 17179869184] [$r hincrby bighash tmp 17179869184]
1890 } {34359738368 34359738368}
1891
1892 test {HINCRBY against key with spaces (no integer encoded)} {
1893 $r hset smallhash tmp " 11 "
1894 $r hset bighash tmp " 11 "
1895 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1896 } {12 12}
1897
1898 # TODO:
1899 # Randomized test, small and big
1900 # .rdb / AOF consistency test should include hashes
1901
1902 test {EXPIRE - don't set timeouts multiple times} {
1903 $r set x foobar
1904 set v1 [$r expire x 5]
1905 set v2 [$r ttl x]
1906 set v3 [$r expire x 10]
1907 set v4 [$r ttl x]
1908 list $v1 $v2 $v3 $v4
1909 } {1 5 0 5}
1910
1911 test {EXPIRE - It should be still possible to read 'x'} {
1912 $r get x
1913 } {foobar}
1914
1915 test {EXPIRE - After 6 seconds the key should no longer be here} {
1916 after 6000
1917 list [$r get x] [$r exists x]
1918 } {{} 0}
1919
1920 test {EXPIRE - Delete on write policy} {
1921 $r del x
1922 $r lpush x foo
1923 $r expire x 1000
1924 $r lpush x bar
1925 $r lrange x 0 -1
1926 } {bar}
1927
1928 test {EXPIREAT - Check for EXPIRE alike behavior} {
1929 $r del x
1930 $r set x foo
1931 $r expireat x [expr [clock seconds]+15]
1932 $r ttl x
1933 } {1[345]}
1934
1935 test {ZSETs skiplist implementation backlink consistency test} {
1936 set diff 0
1937 set elements 10000
1938 for {set j 0} {$j < $elements} {incr j} {
1939 $r zadd myzset [expr rand()] "Element-$j"
1940 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1941 }
1942 set l1 [$r zrange myzset 0 -1]
1943 set l2 [$r zrevrange myzset 0 -1]
1944 for {set j 0} {$j < [llength $l1]} {incr j} {
1945 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1946 incr diff
1947 }
1948 }
1949 format $diff
1950 } {0}
1951
1952 test {ZSETs ZRANK augmented skip list stress testing} {
1953 set err {}
1954 $r del myzset
1955 for {set k 0} {$k < 10000} {incr k} {
1956 set i [expr {$k%1000}]
1957 if {[expr rand()] < .2} {
1958 $r zrem myzset $i
1959 } else {
1960 set score [expr rand()]
1961 $r zadd myzset $score $i
1962 }
1963 set card [$r zcard myzset]
1964 if {$card > 0} {
1965 set index [randomInt $card]
1966 set ele [lindex [$r zrange myzset $index $index] 0]
1967 set rank [$r zrank myzset $ele]
1968 if {$rank != $index} {
1969 set err "$ele RANK is wrong! ($rank != $index)"
1970 break
1971 }
1972 }
1973 }
1974 set _ $err
1975 } {}
1976
1977 foreach fuzztype {binary alpha compr} {
1978 test "FUZZ stresser with data model $fuzztype" {
1979 set err 0
1980 for {set i 0} {$i < 10000} {incr i} {
1981 set fuzz [randstring 0 512 $fuzztype]
1982 $r set foo $fuzz
1983 set got [$r get foo]
1984 if {$got ne $fuzz} {
1985 set err [list $fuzz $got]
1986 break
1987 }
1988 }
1989 set _ $err
1990 } {0}
1991 }
1992
1993 test {BGSAVE} {
1994 waitForBgsave $r
1995 $r flushdb
1996 $r save
1997 $r set x 10
1998 $r bgsave
1999 waitForBgsave $r
2000 $r debug reload
2001 $r get x
2002 } {10}
2003
2004 test {Handle an empty query well} {
2005 set fd [$r channel]
2006 puts -nonewline $fd "\r\n"
2007 flush $fd
2008 $r ping
2009 } {PONG}
2010
2011 test {Negative multi bulk command does not create problems} {
2012 set fd [$r channel]
2013 puts -nonewline $fd "*-10\r\n"
2014 flush $fd
2015 $r ping
2016 } {PONG}
2017
2018 test {Negative multi bulk payload} {
2019 set fd [$r channel]
2020 puts -nonewline $fd "SET x -10\r\n"
2021 flush $fd
2022 gets $fd
2023 } {*invalid bulk*}
2024
2025 test {Too big bulk payload} {
2026 set fd [$r channel]
2027 puts -nonewline $fd "SET x 2000000000\r\n"
2028 flush $fd
2029 gets $fd
2030 } {*invalid bulk*count*}
2031
2032 test {Multi bulk request not followed by bulk args} {
2033 set fd [$r channel]
2034 puts -nonewline $fd "*1\r\nfoo\r\n"
2035 flush $fd
2036 gets $fd
2037 } {*protocol error*}
2038
2039 test {Generic wrong number of args} {
2040 catch {$r ping x y z} err
2041 set _ $err
2042 } {*wrong*arguments*ping*}
2043
2044 test {SELECT an out of range DB} {
2045 catch {$r select 1000000} err
2046 set _ $err
2047 } {*invalid*}
2048
2049 if {![catch {package require sha1}]} {
2050 test {Check consistency of different data types after a reload} {
2051 $r flushdb
2052 createComplexDataset $r 10000
2053 set sha1 [datasetDigest $r]
2054 $r debug reload
2055 set sha1_after [datasetDigest $r]
2056 expr {$sha1 eq $sha1_after}
2057 } {1}
2058
2059 test {Same dataset digest if saving/reloading as AOF?} {
2060 $r bgrewriteaof
2061 waitForBgrewriteaof $r
2062 $r debug loadaof
2063 set sha1_after [datasetDigest $r]
2064 expr {$sha1 eq $sha1_after}
2065 } {1}
2066 }
2067
2068 test {EXPIRES after a reload (snapshot + append only file)} {
2069 $r flushdb
2070 $r set x 10
2071 $r expire x 1000
2072 $r save
2073 $r debug reload
2074 set ttl [$r ttl x]
2075 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
2076 $r bgrewriteaof
2077 waitForBgrewriteaof $r
2078 set ttl [$r ttl x]
2079 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
2080 list $e1 $e2
2081 } {1 1}
2082
2083 test {PIPELINING stresser (also a regression for the old epoll bug)} {
2084 set fd2 [socket $::host $::port]
2085 fconfigure $fd2 -encoding binary -translation binary
2086 puts -nonewline $fd2 "SELECT 9\r\n"
2087 flush $fd2
2088 gets $fd2
2089
2090 for {set i 0} {$i < 100000} {incr i} {
2091 set q {}
2092 set val "0000${i}0000"
2093 append q "SET key:$i [string length $val]\r\n$val\r\n"
2094 puts -nonewline $fd2 $q
2095 set q {}
2096 append q "GET key:$i\r\n"
2097 puts -nonewline $fd2 $q
2098 }
2099 flush $fd2
2100
2101 for {set i 0} {$i < 100000} {incr i} {
2102 gets $fd2 line
2103 gets $fd2 count
2104 set count [string range $count 1 end]
2105 set val [read $fd2 $count]
2106 read $fd2 2
2107 }
2108 close $fd2
2109 set _ 1
2110 } {1}
2111
2112 test {MUTLI / EXEC basics} {
2113 $r del mylist
2114 $r rpush mylist a
2115 $r rpush mylist b
2116 $r rpush mylist c
2117 $r multi
2118 set v1 [$r lrange mylist 0 -1]
2119 set v2 [$r ping]
2120 set v3 [$r exec]
2121 list $v1 $v2 $v3
2122 } {QUEUED QUEUED {{a b c} PONG}}
2123
2124 test {DISCARD} {
2125 $r del mylist
2126 $r rpush mylist a
2127 $r rpush mylist b
2128 $r rpush mylist c
2129 $r multi
2130 set v1 [$r del mylist]
2131 set v2 [$r discard]
2132 set v3 [$r lrange mylist 0 -1]
2133 list $v1 $v2 $v3
2134 } {QUEUED OK {a b c}}
2135
2136 test {APPEND basics} {
2137 list [$r append foo bar] [$r get foo] \
2138 [$r append foo 100] [$r get foo]
2139 } {3 bar 6 bar100}
2140
2141 test {APPEND fuzzing} {
2142 set err {}
2143 foreach type {binary alpha compr} {
2144 set buf {}
2145 $r del x
2146 for {set i 0} {$i < 1000} {incr i} {
2147 set bin [randstring 0 10 $type]
2148 append buf $bin
2149 $r append x $bin
2150 }
2151 if {$buf != [$r get x]} {
2152 set err "Expected '$buf' found '[$r get x]'"
2153 break
2154 }
2155 }
2156 set _ $err
2157 } {}
2158
2159 # Leave the user with a clean DB before to exit
2160 test {FLUSHDB} {
2161 set aux {}
2162 $r select 9
2163 $r flushdb
2164 lappend aux [$r dbsize]
2165 $r select 10
2166 $r flushdb
2167 lappend aux [$r dbsize]
2168 } {0 0}
2169
2170 test {Perform a final SAVE to leave a clean DB on disk} {
2171 $r save
2172 } {OK}
2173
2174 catch {
2175 if {[string match {*Darwin*} [exec uname -a]]} {
2176 test {Check for memory leaks} {
2177 exec leaks redis-server
2178 } {*0 leaks*}
2179 }
2180 }
2181
2182 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
2183 if {$::failed > 0} {
2184 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
2185 }
2186 }
2187
2188 proc stress {} {
2189 set r [redis $::host $::port]
2190 $r select 9
2191 $r flushdb
2192 while 1 {
2193 set randkey [expr int(rand()*10000)]
2194 set randval [expr int(rand()*10000)]
2195 set randidx0 [expr int(rand()*10)]
2196 set randidx1 [expr int(rand()*10)]
2197 set cmd [expr int(rand()*20)]
2198 catch {
2199 if {$cmd == 0} {$r set $randkey $randval}
2200 if {$cmd == 1} {$r get $randkey}
2201 if {$cmd == 2} {$r incr $randkey}
2202 if {$cmd == 3} {$r lpush $randkey $randval}
2203 if {$cmd == 4} {$r rpop $randkey}
2204 if {$cmd == 5} {$r del $randkey}
2205 if {$cmd == 6} {$r llen $randkey}
2206 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
2207 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
2208 if {$cmd == 9} {$r lindex $randkey $randidx0}
2209 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
2210 if {$cmd == 11} {$r sadd $randkey $randval}
2211 if {$cmd == 12} {$r srem $randkey $randval}
2212 if {$cmd == 13} {$r smove $randkey $randval}
2213 if {$cmd == 14} {$r scard $randkey}
2214 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
2215 }
2216 flush stdout
2217 }
2218 $r flushdb
2219 $r close
2220 }
2221
2222 # Set a few configuration defaults
2223 set ::host 127.0.0.1
2224 set ::port 6379
2225 set ::stress 0
2226 set ::traceleaks 0
2227 set ::flush 0
2228 set ::first 0
2229 set ::last 1000000
2230
2231 # Parse arguments
2232 for {set j 0} {$j < [llength $argv]} {incr j} {
2233 set opt [lindex $argv $j]
2234 set arg [lindex $argv [expr $j+1]]
2235 set lastarg [expr {$arg eq {}}]
2236 if {$opt eq {-h} && !$lastarg} {
2237 set ::host $arg
2238 incr j
2239 } elseif {$opt eq {-p} && !$lastarg} {
2240 set ::port $arg
2241 incr j
2242 } elseif {$opt eq {--stress}} {
2243 set ::stress 1
2244 } elseif {$opt eq {--trace-leaks}} {
2245 set ::traceleaks 1
2246 } elseif {$opt eq {--flush}} {
2247 set ::flush 1
2248 } elseif {$opt eq {--first} && !$lastarg} {
2249 set ::first $arg
2250 incr j
2251 } elseif {$opt eq {--last} && !$lastarg} {
2252 set ::last $arg
2253 incr j
2254 } else {
2255 puts "Wrong argument: $opt"
2256 exit 1
2257 }
2258 }
2259
2260 # Before to run the test check if DB 9 and DB 10 are empty
2261 set r [redis $::host $::port]
2262
2263 if {$::flush} {
2264 $r flushall
2265 }
2266
2267 $r select 9
2268 set db9size [$r dbsize]
2269 $r select 10
2270 set db10size [$r dbsize]
2271 if {$db9size != 0 || $db10size != 0} {
2272 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
2273 exit 1
2274 }
2275 $r close
2276 unset r
2277 unset db9size
2278 unset db10size
2279
2280 puts "Testing Redis, host $::host, port $::port"
2281 if {$::stress} {
2282 stress
2283 } else {
2284 main
2285 }