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