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