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