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