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