]> git.saurik.com Git - redis.git/blob - test-redis.tcl
Remove trailing whitespace.
[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 {HGET against non existing key} {
1618 set rv {}
1619 lappend rv [$r hget smallhash __123123123__]
1620 lappend rv [$r hget bighash __123123123__]
1621 set _ $rv
1622 } {{} {}}
1623
1624 test {HSET in update and insert mode} {
1625 set rv {}
1626 set k [lindex [array names smallhash *] 0]
1627 lappend rv [$r hset smallhash $k newval1]
1628 set smallhash($k) newval1
1629 lappend rv [$r hget smallhash $k]
1630 lappend rv [$r hset smallhash __foobar123__ newval]
1631 set k [lindex [array names bighash *] 0]
1632 lappend rv [$r hset bighash $k newval2]
1633 set bighash($k) newval2
1634 lappend rv [$r hget bighash $k]
1635 lappend rv [$r hset bighash __foobar123__ newval]
1636 lappend rv [$r hdel smallhash __foobar123__]
1637 lappend rv [$r hdel bighash __foobar123__]
1638 set _ $rv
1639 } {0 newval1 1 0 newval2 1 1 1}
1640
1641 test {HMSET wrong number of args} {
1642 catch {$r hmset smallhash key1 val1 key2} err
1643 format $err
1644 } {*wrong number*}
1645
1646 test {HMSET - small hash} {
1647 set args {}
1648 foreach {k v} [array get smallhash] {
1649 set newval [randstring 0 8 alpha]
1650 set smallhash($k) $newval
1651 lappend args $k $newval
1652 }
1653 $r hmset smallhash {*}$args
1654 } {OK}
1655
1656 test {HMSET - big hash} {
1657 set args {}
1658 foreach {k v} [array get bighash] {
1659 set newval [randstring 0 8 alpha]
1660 set bighash($k) $newval
1661 lappend args $k $newval
1662 }
1663 $r hmset bighash {*}$args
1664 } {OK}
1665
1666 test {HKEYS - small hash} {
1667 lsort [$r hkeys smallhash]
1668 } [lsort [array names smallhash *]]
1669
1670 test {HKEYS - big hash} {
1671 lsort [$r hkeys bighash]
1672 } [lsort [array names bighash *]]
1673
1674 test {HVALS - small hash} {
1675 set vals {}
1676 foreach {k v} [array get smallhash] {
1677 lappend vals $v
1678 }
1679 set _ [lsort $vals]
1680 } [lsort [$r hvals smallhash]]
1681
1682 test {HVALS - big hash} {
1683 set vals {}
1684 foreach {k v} [array get bighash] {
1685 lappend vals $v
1686 }
1687 set _ [lsort $vals]
1688 } [lsort [$r hvals bighash]]
1689
1690 test {HGETALL - small hash} {
1691 lsort [$r hgetall smallhash]
1692 } [lsort [array get smallhash]]
1693
1694 test {HGETALL - big hash} {
1695 lsort [$r hgetall bighash]
1696 } [lsort [array get bighash]]
1697
1698 test {HDEL and return value} {
1699 set rv {}
1700 lappend rv [$r hdel smallhash nokey]
1701 lappend rv [$r hdel bighash nokey]
1702 set k [lindex [array names smallhash *] 0]
1703 lappend rv [$r hdel smallhash $k]
1704 lappend rv [$r hdel smallhash $k]
1705 lappend rv [$r hget smallhash $k]
1706 unset smallhash($k)
1707 set k [lindex [array names bighash *] 0]
1708 lappend rv [$r hdel bighash $k]
1709 lappend rv [$r hdel bighash $k]
1710 lappend rv [$r hget bighash $k]
1711 unset bighash($k)
1712 set _ $rv
1713 } {0 0 1 0 {} 1 0 {}}
1714
1715 test {HEXISTS} {
1716 set rv {}
1717 set k [lindex [array names smallhash *] 0]
1718 lappend rv [$r hexists smallhash $k]
1719 lappend rv [$r hexists smallhash nokey]
1720 set k [lindex [array names bighash *] 0]
1721 lappend rv [$r hexists bighash $k]
1722 lappend rv [$r hexists bighash nokey]
1723 } {1 0 1 0}
1724
1725 test {Is a zipmap encoded Hash promoted on big payload?} {
1726 $r hset smallhash foo [string repeat a 1024]
1727 $r debug object smallhash
1728 } {*hashtable*}
1729
1730 test {HINCRBY against non existing database key} {
1731 $r del htest
1732 list [$r hincrby htest foo 2]
1733 } {2}
1734
1735 test {HINCRBY against non existing hash key} {
1736 set rv {}
1737 $r hdel smallhash tmp
1738 $r hdel bighash tmp
1739 lappend rv [$r hincrby smallhash tmp 2]
1740 lappend rv [$r hget smallhash tmp]
1741 lappend rv [$r hincrby bighash tmp 2]
1742 lappend rv [$r hget bighash tmp]
1743 } {2 2 2 2}
1744
1745 test {HINCRBY against hash key created by hincrby itself} {
1746 set rv {}
1747 lappend rv [$r hincrby smallhash tmp 3]
1748 lappend rv [$r hget smallhash tmp]
1749 lappend rv [$r hincrby bighash tmp 3]
1750 lappend rv [$r hget bighash tmp]
1751 } {5 5 5 5}
1752
1753 test {HINCRBY against hash key originally set with HSET} {
1754 $r hset smallhash tmp 100
1755 $r hset bighash tmp 100
1756 list [$r hincrby smallhash tmp 2] [$r hincrby bighash tmp 2]
1757 } {102 102}
1758
1759 test {HINCRBY over 32bit value} {
1760 $r hset smallhash tmp 17179869184
1761 $r hset bighash tmp 17179869184
1762 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1763 } {17179869185 17179869185}
1764
1765 test {HINCRBY over 32bit value with over 32bit increment} {
1766 $r hset smallhash tmp 17179869184
1767 $r hset bighash tmp 17179869184
1768 list [$r hincrby smallhash tmp 17179869184] [$r hincrby bighash tmp 17179869184]
1769 } {34359738368 34359738368}
1770
1771 test {HINCRBY against key with spaces (no integer encoded)} {
1772 $r hset smallhash tmp " 11 "
1773 $r hset bighash tmp " 11 "
1774 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1775 } {12 12}
1776
1777 # TODO:
1778 # Randomized test, small and big
1779 # .rdb / AOF consistency test should include hashes
1780
1781 test {EXPIRE - don't set timeouts multiple times} {
1782 $r set x foobar
1783 set v1 [$r expire x 5]
1784 set v2 [$r ttl x]
1785 set v3 [$r expire x 10]
1786 set v4 [$r ttl x]
1787 list $v1 $v2 $v3 $v4
1788 } {1 5 0 5}
1789
1790 test {EXPIRE - It should be still possible to read 'x'} {
1791 $r get x
1792 } {foobar}
1793
1794 test {EXPIRE - After 6 seconds the key should no longer be here} {
1795 after 6000
1796 list [$r get x] [$r exists x]
1797 } {{} 0}
1798
1799 test {EXPIRE - Delete on write policy} {
1800 $r del x
1801 $r lpush x foo
1802 $r expire x 1000
1803 $r lpush x bar
1804 $r lrange x 0 -1
1805 } {bar}
1806
1807 test {EXPIREAT - Check for EXPIRE alike behavior} {
1808 $r del x
1809 $r set x foo
1810 $r expireat x [expr [clock seconds]+15]
1811 $r ttl x
1812 } {1[345]}
1813
1814 test {ZSETs skiplist implementation backlink consistency test} {
1815 set diff 0
1816 set elements 10000
1817 for {set j 0} {$j < $elements} {incr j} {
1818 $r zadd myzset [expr rand()] "Element-$j"
1819 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1820 }
1821 set l1 [$r zrange myzset 0 -1]
1822 set l2 [$r zrevrange myzset 0 -1]
1823 for {set j 0} {$j < [llength $l1]} {incr j} {
1824 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1825 incr diff
1826 }
1827 }
1828 format $diff
1829 } {0}
1830
1831 test {ZSETs ZRANK augmented skip list stress testing} {
1832 set err {}
1833 $r del myzset
1834 for {set k 0} {$k < 10000} {incr k} {
1835 set i [expr {$k%1000}]
1836 if {[expr rand()] < .2} {
1837 $r zrem myzset $i
1838 } else {
1839 set score [expr rand()]
1840 $r zadd myzset $score $i
1841 }
1842 set card [$r zcard myzset]
1843 if {$card > 0} {
1844 set index [randomInt $card]
1845 set ele [lindex [$r zrange myzset $index $index] 0]
1846 set rank [$r zrank myzset $ele]
1847 if {$rank != $index} {
1848 set err "$ele RANK is wrong! ($rank != $index)"
1849 break
1850 }
1851 }
1852 }
1853 set _ $err
1854 } {}
1855
1856 foreach fuzztype {binary alpha compr} {
1857 test "FUZZ stresser with data model $fuzztype" {
1858 set err 0
1859 for {set i 0} {$i < 10000} {incr i} {
1860 set fuzz [randstring 0 512 $fuzztype]
1861 $r set foo $fuzz
1862 set got [$r get foo]
1863 if {$got ne $fuzz} {
1864 set err [list $fuzz $got]
1865 break
1866 }
1867 }
1868 set _ $err
1869 } {0}
1870 }
1871
1872 test {BGSAVE} {
1873 waitForBgsave $r
1874 $r flushdb
1875 $r save
1876 $r set x 10
1877 $r bgsave
1878 waitForBgsave $r
1879 $r debug reload
1880 $r get x
1881 } {10}
1882
1883 test {Handle an empty query well} {
1884 set fd [$r channel]
1885 puts -nonewline $fd "\r\n"
1886 flush $fd
1887 $r ping
1888 } {PONG}
1889
1890 test {Negative multi bulk command does not create problems} {
1891 set fd [$r channel]
1892 puts -nonewline $fd "*-10\r\n"
1893 flush $fd
1894 $r ping
1895 } {PONG}
1896
1897 test {Negative multi bulk payload} {
1898 set fd [$r channel]
1899 puts -nonewline $fd "SET x -10\r\n"
1900 flush $fd
1901 gets $fd
1902 } {*invalid bulk*}
1903
1904 test {Too big bulk payload} {
1905 set fd [$r channel]
1906 puts -nonewline $fd "SET x 2000000000\r\n"
1907 flush $fd
1908 gets $fd
1909 } {*invalid bulk*count*}
1910
1911 test {Multi bulk request not followed by bulk args} {
1912 set fd [$r channel]
1913 puts -nonewline $fd "*1\r\nfoo\r\n"
1914 flush $fd
1915 gets $fd
1916 } {*protocol error*}
1917
1918 test {Generic wrong number of args} {
1919 catch {$r ping x y z} err
1920 set _ $err
1921 } {*wrong*arguments*ping*}
1922
1923 test {SELECT an out of range DB} {
1924 catch {$r select 1000000} err
1925 set _ $err
1926 } {*invalid*}
1927
1928 if {![catch {package require sha1}]} {
1929 test {Check consistency of different data types after a reload} {
1930 $r flushdb
1931 createComplexDataset $r 10000
1932 set sha1 [datasetDigest $r]
1933 $r debug reload
1934 set sha1_after [datasetDigest $r]
1935 expr {$sha1 eq $sha1_after}
1936 } {1}
1937
1938 test {Same dataset digest if saving/reloading as AOF?} {
1939 $r bgrewriteaof
1940 waitForBgrewriteaof $r
1941 $r debug loadaof
1942 set sha1_after [datasetDigest $r]
1943 expr {$sha1 eq $sha1_after}
1944 } {1}
1945 }
1946
1947 test {EXPIRES after a reload (snapshot + append only file)} {
1948 $r flushdb
1949 $r set x 10
1950 $r expire x 1000
1951 $r save
1952 $r debug reload
1953 set ttl [$r ttl x]
1954 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
1955 $r bgrewriteaof
1956 waitForBgrewriteaof $r
1957 set ttl [$r ttl x]
1958 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
1959 list $e1 $e2
1960 } {1 1}
1961
1962 test {PIPELINING stresser (also a regression for the old epoll bug)} {
1963 set fd2 [socket 127.0.0.1 6379]
1964 fconfigure $fd2 -encoding binary -translation binary
1965 puts -nonewline $fd2 "SELECT 9\r\n"
1966 flush $fd2
1967 gets $fd2
1968
1969 for {set i 0} {$i < 100000} {incr i} {
1970 set q {}
1971 set val "0000${i}0000"
1972 append q "SET key:$i [string length $val]\r\n$val\r\n"
1973 puts -nonewline $fd2 $q
1974 set q {}
1975 append q "GET key:$i\r\n"
1976 puts -nonewline $fd2 $q
1977 }
1978 flush $fd2
1979
1980 for {set i 0} {$i < 100000} {incr i} {
1981 gets $fd2 line
1982 gets $fd2 count
1983 set count [string range $count 1 end]
1984 set val [read $fd2 $count]
1985 read $fd2 2
1986 }
1987 close $fd2
1988 set _ 1
1989 } {1}
1990
1991 test {MUTLI / EXEC basics} {
1992 $r del mylist
1993 $r rpush mylist a
1994 $r rpush mylist b
1995 $r rpush mylist c
1996 $r multi
1997 set v1 [$r lrange mylist 0 -1]
1998 set v2 [$r ping]
1999 set v3 [$r exec]
2000 list $v1 $v2 $v3
2001 } {QUEUED QUEUED {{a b c} PONG}}
2002
2003 test {DISCARD} {
2004 $r del mylist
2005 $r rpush mylist a
2006 $r rpush mylist b
2007 $r rpush mylist c
2008 $r multi
2009 set v1 [$r del mylist]
2010 set v2 [$r discard]
2011 set v3 [$r lrange mylist 0 -1]
2012 list $v1 $v2 $v3
2013 } {QUEUED OK {a b c}}
2014
2015 test {APPEND basics} {
2016 list [$r append foo bar] [$r get foo] \
2017 [$r append foo 100] [$r get foo]
2018 } {3 bar 6 bar100}
2019
2020 test {APPEND fuzzing} {
2021 set err {}
2022 foreach type {binary alpha compr} {
2023 set buf {}
2024 $r del x
2025 for {set i 0} {$i < 1000} {incr i} {
2026 set bin [randstring 0 10 $type]
2027 append buf $bin
2028 $r append x $bin
2029 }
2030 if {$buf != [$r get x]} {
2031 set err "Expected '$buf' found '[$r get x]'"
2032 break
2033 }
2034 }
2035 set _ $err
2036 } {}
2037
2038 # Leave the user with a clean DB before to exit
2039 test {FLUSHDB} {
2040 set aux {}
2041 $r select 9
2042 $r flushdb
2043 lappend aux [$r dbsize]
2044 $r select 10
2045 $r flushdb
2046 lappend aux [$r dbsize]
2047 } {0 0}
2048
2049 test {Perform a final SAVE to leave a clean DB on disk} {
2050 $r save
2051 } {OK}
2052
2053 catch {
2054 if {[string match {*Darwin*} [exec uname -a]]} {
2055 test {Check for memory leaks} {
2056 exec leaks redis-server
2057 } {*0 leaks*}
2058 }
2059 }
2060
2061 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
2062 if {$::failed > 0} {
2063 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
2064 }
2065 }
2066
2067 proc stress {} {
2068 set r [redis]
2069 $r select 9
2070 $r flushdb
2071 while 1 {
2072 set randkey [expr int(rand()*10000)]
2073 set randval [expr int(rand()*10000)]
2074 set randidx0 [expr int(rand()*10)]
2075 set randidx1 [expr int(rand()*10)]
2076 set cmd [expr int(rand()*20)]
2077 catch {
2078 if {$cmd == 0} {$r set $randkey $randval}
2079 if {$cmd == 1} {$r get $randkey}
2080 if {$cmd == 2} {$r incr $randkey}
2081 if {$cmd == 3} {$r lpush $randkey $randval}
2082 if {$cmd == 4} {$r rpop $randkey}
2083 if {$cmd == 5} {$r del $randkey}
2084 if {$cmd == 6} {$r llen $randkey}
2085 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
2086 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
2087 if {$cmd == 9} {$r lindex $randkey $randidx0}
2088 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
2089 if {$cmd == 11} {$r sadd $randkey $randval}
2090 if {$cmd == 12} {$r srem $randkey $randval}
2091 if {$cmd == 13} {$r smove $randkey $randval}
2092 if {$cmd == 14} {$r scard $randkey}
2093 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
2094 }
2095 flush stdout
2096 }
2097 $r flushdb
2098 $r close
2099 }
2100
2101 # Set a few configuration defaults
2102 set ::host 127.0.0.1
2103 set ::port 6379
2104 set ::stress 0
2105 set ::flush 0
2106 set ::first 0
2107 set ::last 1000000
2108
2109 # Parse arguments
2110 for {set j 0} {$j < [llength $argv]} {incr j} {
2111 set opt [lindex $argv $j]
2112 set arg [lindex $argv [expr $j+1]]
2113 set lastarg [expr {$arg eq {}}]
2114 if {$opt eq {-h} && !$lastarg} {
2115 set ::host $arg
2116 incr j
2117 } elseif {$opt eq {-p} && !$lastarg} {
2118 set ::port $arg
2119 incr j
2120 } elseif {$opt eq {-stress}} {
2121 set ::stress 1
2122 } elseif {$opt eq {--flush}} {
2123 set ::flush 1
2124 } elseif {$opt eq {--first} && !$lastarg} {
2125 set ::first $arg
2126 incr j
2127 } elseif {$opt eq {--last} && !$lastarg} {
2128 set ::last $arg
2129 incr j
2130 } else {
2131 puts "Wrong argument: $opt"
2132 exit 1
2133 }
2134 }
2135
2136 # Before to run the test check if DB 9 and DB 10 are empty
2137 set r [redis]
2138
2139 if {$::flush} {
2140 $r flushall
2141 }
2142
2143 $r select 9
2144 set db9size [$r dbsize]
2145 $r select 10
2146 set db10size [$r dbsize]
2147 if {$db9size != 0 || $db10size != 0} {
2148 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
2149 exit 1
2150 }
2151 $r close
2152 unset r
2153 unset db9size
2154 unset db10size
2155
2156 if {$::stress} {
2157 stress
2158 } else {
2159 main $::host $::port
2160 }