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