]> git.saurik.com Git - redis.git/blob - test-redis.tcl
751059de26a2634766973a428a80fed50fc23e34
[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 {SINTER against three sets} {
606 $r sadd set3 999
607 $r sadd set3 995
608 $r sadd set3 1000
609 $r sadd set3 2000
610 lsort [$r sinter set1 set2 set3]
611 } {995 999}
612
613 test {SINTERSTORE with three sets} {
614 $r sinterstore setres set1 set2 set3
615 lsort [$r smembers setres]
616 } {995 999}
617
618 test {SUNION with non existing keys} {
619 lsort [$r sunion nokey1 set1 set2 nokey2]
620 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
621
622 test {SDIFF with two sets} {
623 for {set i 5} {$i < 1000} {incr i} {
624 $r sadd set4 $i
625 }
626 lsort [$r sdiff set1 set4]
627 } {0 1 2 3 4}
628
629 test {SDIFF with three sets} {
630 $r sadd set5 0
631 lsort [$r sdiff set1 set4 set5]
632 } {1 2 3 4}
633
634 test {SDIFFSTORE with three sets} {
635 $r sdiffstore sres set1 set4 set5
636 lsort [$r smembers sres]
637 } {1 2 3 4}
638
639 test {SPOP basics} {
640 $r del myset
641 $r sadd myset 1
642 $r sadd myset 2
643 $r sadd myset 3
644 list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset]
645 } {{1 2 3} 0}
646
647 test {SAVE - make sure there are all the types as values} {
648 # Wait for a background saving in progress to terminate
649 waitForBgsave $r
650 $r lpush mysavelist hello
651 $r lpush mysavelist world
652 $r set myemptykey {}
653 $r set mynormalkey {blablablba}
654 $r zadd mytestzset a 10
655 $r zadd mytestzset b 20
656 $r zadd mytestzset c 30
657 $r save
658 } {OK}
659
660 test {SRANDMEMBER} {
661 $r del myset
662 $r sadd myset a
663 $r sadd myset b
664 $r sadd myset c
665 unset -nocomplain myset
666 array set myset {}
667 for {set i 0} {$i < 100} {incr i} {
668 set myset([$r srandmember myset]) 1
669 }
670 lsort [array names myset]
671 } {a b c}
672
673 test {Create a random list} {
674 set tosort {}
675 array set seenrand {}
676 for {set i 0} {$i < 10000} {incr i} {
677 while 1 {
678 # Make sure all the weights are different because
679 # Redis does not use a stable sort but Tcl does.
680 set rint [expr int(rand()*1000000)]
681 if {![info exists seenrand($rint)]} break
682 }
683 set seenrand($rint) x
684 $r lpush tosort $i
685 $r set weight_$i $rint
686 lappend tosort [list $i $rint]
687 }
688 set sorted [lsort -index 1 -real $tosort]
689 set res {}
690 for {set i 0} {$i < 10000} {incr i} {
691 lappend res [lindex $sorted $i 0]
692 }
693 format {}
694 } {}
695
696 test {SORT with BY against the newly created list} {
697 $r sort tosort {BY weight_*}
698 } $res
699
700 test {SORT direct, numeric, against the newly created list} {
701 $r sort tosort
702 } [lsort -integer $res]
703
704 test {SORT decreasing sort} {
705 $r sort tosort {DESC}
706 } [lsort -decreasing -integer $res]
707
708 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
709 set start [clock clicks -milliseconds]
710 for {set i 0} {$i < 100} {incr i} {
711 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
712 }
713 set elapsed [expr [clock clicks -milliseconds]-$start]
714 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
715 flush stdout
716 format {}
717 } {}
718
719 test {SORT speed, sorting 10000 elements list directly, 100 times} {
720 set start [clock clicks -milliseconds]
721 for {set i 0} {$i < 100} {incr i} {
722 set sorted [$r sort tosort {LIMIT 0 10}]
723 }
724 set elapsed [expr [clock clicks -milliseconds]-$start]
725 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
726 flush stdout
727 format {}
728 } {}
729
730 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
731 set start [clock clicks -milliseconds]
732 for {set i 0} {$i < 100} {incr i} {
733 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
734 }
735 set elapsed [expr [clock clicks -milliseconds]-$start]
736 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
737 flush stdout
738 format {}
739 } {}
740
741 test {SORT regression for issue #19, sorting floats} {
742 $r flushdb
743 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
744 $r lpush mylist $x
745 }
746 $r sort mylist
747 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
748
749 test {SORT with GET #} {
750 $r del mylist
751 $r lpush mylist 1
752 $r lpush mylist 2
753 $r lpush mylist 3
754 $r mset weight_1 10 weight_2 5 weight_3 30
755 $r sort mylist BY weight_* GET #
756 } {2 1 3}
757
758 test {LREM, remove all the occurrences} {
759 $r flushdb
760 $r rpush mylist foo
761 $r rpush mylist bar
762 $r rpush mylist foobar
763 $r rpush mylist foobared
764 $r rpush mylist zap
765 $r rpush mylist bar
766 $r rpush mylist test
767 $r rpush mylist foo
768 set res [$r lrem mylist 0 bar]
769 list [$r lrange mylist 0 -1] $res
770 } {{foo foobar foobared zap test foo} 2}
771
772 test {LREM, remove the first occurrence} {
773 set res [$r lrem mylist 1 foo]
774 list [$r lrange mylist 0 -1] $res
775 } {{foobar foobared zap test foo} 1}
776
777 test {LREM, remove non existing element} {
778 set res [$r lrem mylist 1 nosuchelement]
779 list [$r lrange mylist 0 -1] $res
780 } {{foobar foobared zap test foo} 0}
781
782 test {LREM, starting from tail with negative count} {
783 $r flushdb
784 $r rpush mylist foo
785 $r rpush mylist bar
786 $r rpush mylist foobar
787 $r rpush mylist foobared
788 $r rpush mylist zap
789 $r rpush mylist bar
790 $r rpush mylist test
791 $r rpush mylist foo
792 $r rpush mylist foo
793 set res [$r lrem mylist -1 bar]
794 list [$r lrange mylist 0 -1] $res
795 } {{foo bar foobar foobared zap test foo foo} 1}
796
797 test {LREM, starting from tail with negative count (2)} {
798 set res [$r lrem mylist -2 foo]
799 list [$r lrange mylist 0 -1] $res
800 } {{foo bar foobar foobared zap test} 2}
801
802 test {LREM, deleting objects that may be encoded as integers} {
803 $r lpush myotherlist 1
804 $r lpush myotherlist 2
805 $r lpush myotherlist 3
806 $r lrem myotherlist 1 2
807 $r llen myotherlist
808 } {2}
809
810 test {MGET} {
811 $r flushdb
812 $r set foo BAR
813 $r set bar FOO
814 $r mget foo bar
815 } {BAR FOO}
816
817 test {MGET against non existing key} {
818 $r mget foo baazz bar
819 } {BAR {} FOO}
820
821 test {MGET against non-string key} {
822 $r sadd myset ciao
823 $r sadd myset bau
824 $r mget foo baazz bar myset
825 } {BAR {} FOO {}}
826
827 test {RANDOMKEY} {
828 $r flushdb
829 $r set foo x
830 $r set bar y
831 set foo_seen 0
832 set bar_seen 0
833 for {set i 0} {$i < 100} {incr i} {
834 set rkey [$r randomkey]
835 if {$rkey eq {foo}} {
836 set foo_seen 1
837 }
838 if {$rkey eq {bar}} {
839 set bar_seen 1
840 }
841 }
842 list $foo_seen $bar_seen
843 } {1 1}
844
845 test {RANDOMKEY against empty DB} {
846 $r flushdb
847 $r randomkey
848 } {}
849
850 test {RANDOMKEY regression 1} {
851 $r flushdb
852 $r set x 10
853 $r del x
854 $r randomkey
855 } {}
856
857 test {GETSET (set new value)} {
858 list [$r getset foo xyz] [$r get foo]
859 } {{} xyz}
860
861 test {GETSET (replace old value)} {
862 $r set foo bar
863 list [$r getset foo xyz] [$r get foo]
864 } {bar xyz}
865
866 test {SMOVE basics} {
867 $r sadd myset1 a
868 $r sadd myset1 b
869 $r sadd myset1 c
870 $r sadd myset2 x
871 $r sadd myset2 y
872 $r sadd myset2 z
873 $r smove myset1 myset2 a
874 list [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
875 } {{a x y z} {b c}}
876
877 test {SMOVE non existing key} {
878 list [$r smove myset1 myset2 foo] [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
879 } {0 {a x y z} {b c}}
880
881 test {SMOVE non existing src set} {
882 list [$r smove noset myset2 foo] [lsort [$r smembers myset2]]
883 } {0 {a x y z}}
884
885 test {SMOVE non existing dst set} {
886 list [$r smove myset2 myset3 y] [lsort [$r smembers myset2]] [lsort [$r smembers myset3]]
887 } {1 {a x z} y}
888
889 test {SMOVE wrong src key type} {
890 $r set x 10
891 catch {$r smove x myset2 foo} err
892 format $err
893 } {ERR*}
894
895 test {SMOVE wrong dst key type} {
896 $r set x 10
897 catch {$r smove myset2 x foo} err
898 format $err
899 } {ERR*}
900
901 test {MSET base case} {
902 $r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n"
903 $r mget x y z
904 } [list 10 {foo bar} "x x x x x x x\n\n\r\n"]
905
906 test {MSET wrong number of args} {
907 catch {$r mset x 10 y "foo bar" z} err
908 format $err
909 } {*wrong number*}
910
911 test {MSETNX with already existent key} {
912 list [$r msetnx x1 xxx y2 yyy x 20] [$r exists x1] [$r exists y2]
913 } {0 0 0}
914
915 test {MSETNX with not existing keys} {
916 list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2]
917 } {1 xxx yyy}
918
919 test {MSETNX should remove all the volatile keys even on failure} {
920 $r mset x 1 y 2 z 3
921 $r expire y 10000
922 $r expire z 10000
923 list [$r msetnx x A y B z C] [$r mget x y z]
924 } {0 {1 {} {}}}
925
926 test {ZSET basic ZADD and score update} {
927 $r zadd ztmp 10 x
928 $r zadd ztmp 20 y
929 $r zadd ztmp 30 z
930 set aux1 [$r zrange ztmp 0 -1]
931 $r zadd ztmp 1 y
932 set aux2 [$r zrange ztmp 0 -1]
933 list $aux1 $aux2
934 } {{x y z} {y x z}}
935
936 test {ZCARD basics} {
937 $r zcard ztmp
938 } {3}
939
940 test {ZCARD non existing key} {
941 $r zcard ztmp-blabla
942 } {0}
943
944 test {ZSCORE} {
945 set aux {}
946 set err {}
947 for {set i 0} {$i < 1000} {incr i} {
948 set score [expr rand()]
949 lappend aux $score
950 $r zadd zscoretest $score $i
951 }
952 for {set i 0} {$i < 1000} {incr i} {
953 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
954 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
955 break
956 }
957 }
958 set _ $err
959 } {}
960
961 test {ZSCORE after a DEBUG RELOAD} {
962 set aux {}
963 set err {}
964 $r del zscoretest
965 for {set i 0} {$i < 1000} {incr i} {
966 set score [expr rand()]
967 lappend aux $score
968 $r zadd zscoretest $score $i
969 }
970 $r debug reload
971 for {set i 0} {$i < 1000} {incr i} {
972 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
973 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
974 break
975 }
976 }
977 set _ $err
978 } {}
979
980 test {ZRANGE and ZREVRANGE} {
981 list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1]
982 } {{y x z} {z x y}}
983
984 test {ZSETs stress tester - sorting is working well?} {
985 set delta 0
986 for {set test 0} {$test < 2} {incr test} {
987 unset -nocomplain auxarray
988 array set auxarray {}
989 set auxlist {}
990 $r del myzset
991 for {set i 0} {$i < 1000} {incr i} {
992 if {$test == 0} {
993 set score [expr rand()]
994 } else {
995 set score [expr int(rand()*10)]
996 }
997 set auxarray($i) $score
998 $r zadd myzset $score $i
999 # Random update
1000 if {[expr rand()] < .2} {
1001 set j [expr int(rand()*1000)]
1002 if {$test == 0} {
1003 set score [expr rand()]
1004 } else {
1005 set score [expr int(rand()*10)]
1006 }
1007 set auxarray($j) $score
1008 $r zadd myzset $score $j
1009 }
1010 }
1011 foreach {item score} [array get auxarray] {
1012 lappend auxlist [list $score $item]
1013 }
1014 set sorted [lsort -command zlistAlikeSort $auxlist]
1015 set auxlist {}
1016 foreach x $sorted {
1017 lappend auxlist [lindex $x 1]
1018 }
1019 set fromredis [$r zrange myzset 0 -1]
1020 set delta 0
1021 for {set i 0} {$i < [llength $fromredis]} {incr i} {
1022 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1023 incr delta
1024 }
1025 }
1026 }
1027 format $delta
1028 } {0}
1029
1030 test {ZINCRBY - can create a new sorted set} {
1031 $r del zset
1032 $r zincrby zset 1 foo
1033 list [$r zrange zset 0 -1] [$r zscore zset foo]
1034 } {foo 1}
1035
1036 test {ZINCRBY - increment and decrement} {
1037 $r zincrby zset 2 foo
1038 $r zincrby zset 1 bar
1039 set v1 [$r zrange zset 0 -1]
1040 $r zincrby zset 10 bar
1041 $r zincrby zset -5 foo
1042 $r zincrby zset -5 bar
1043 set v2 [$r zrange zset 0 -1]
1044 list $v1 $v2 [$r zscore zset foo] [$r zscore zset bar]
1045 } {{bar foo} {foo bar} -2 6}
1046
1047 test {ZRANGEBYSCORE basics} {
1048 $r del zset
1049 $r zadd zset 1 a
1050 $r zadd zset 2 b
1051 $r zadd zset 3 c
1052 $r zadd zset 4 d
1053 $r zadd zset 5 e
1054 $r zrangebyscore zset 2 4
1055 } {b c d}
1056
1057 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
1058 set err {}
1059 $r del zset
1060 for {set i 0} {$i < 1000} {incr i} {
1061 $r zadd zset [expr rand()] $i
1062 }
1063 for {set i 0} {$i < 100} {incr i} {
1064 set min [expr rand()]
1065 set max [expr rand()]
1066 if {$min > $max} {
1067 set aux $min
1068 set min $max
1069 set max $aux
1070 }
1071 set low [$r zrangebyscore zset -inf $min]
1072 set ok [$r zrangebyscore zset $min $max]
1073 set high [$r zrangebyscore zset $max +inf]
1074 foreach x $low {
1075 set score [$r zscore zset $x]
1076 if {$score > $min} {
1077 append err "Error, score for $x is $score > $min\n"
1078 }
1079 }
1080 foreach x $ok {
1081 set score [$r zscore zset $x]
1082 if {$score < $min || $score > $max} {
1083 append err "Error, score for $x is $score outside $min-$max range\n"
1084 }
1085 }
1086 foreach x $high {
1087 set score [$r zscore zset $x]
1088 if {$score < $max} {
1089 append err "Error, score for $x is $score < $max\n"
1090 }
1091 }
1092 }
1093 set _ $err
1094 } {}
1095
1096 test {ZRANGEBYSCORE with LIMIT} {
1097 $r del zset
1098 $r zadd zset 1 a
1099 $r zadd zset 2 b
1100 $r zadd zset 3 c
1101 $r zadd zset 4 d
1102 $r zadd zset 5 e
1103 list \
1104 [$r zrangebyscore zset 0 10 LIMIT 0 2] \
1105 [$r zrangebyscore zset 0 10 LIMIT 2 3] \
1106 [$r zrangebyscore zset 0 10 LIMIT 2 10] \
1107 [$r zrangebyscore zset 0 10 LIMIT 20 10]
1108 } {{a b} {c d e} {c d e} {}}
1109
1110 test {ZREMRANGE basics} {
1111 $r del zset
1112 $r zadd zset 1 a
1113 $r zadd zset 2 b
1114 $r zadd zset 3 c
1115 $r zadd zset 4 d
1116 $r zadd zset 5 e
1117 list [$r zremrangebyscore zset 2 4] [$r zrange zset 0 -1]
1118 } {3 {a e}}
1119
1120 test {ZREMRANGE from -inf to +inf} {
1121 $r del zset
1122 $r zadd zset 1 a
1123 $r zadd zset 2 b
1124 $r zadd zset 3 c
1125 $r zadd zset 4 d
1126 $r zadd zset 5 e
1127 list [$r zremrangebyscore zset -inf +inf] [$r zrange zset 0 -1]
1128 } {5 {}}
1129
1130 test {SORT against sorted sets} {
1131 $r del zset
1132 $r zadd zset 1 a
1133 $r zadd zset 5 b
1134 $r zadd zset 2 c
1135 $r zadd zset 10 d
1136 $r zadd zset 3 e
1137 $r sort zset alpha desc
1138 } {e d c b a}
1139
1140 test {Sorted sets +inf and -inf handling} {
1141 $r del zset
1142 $r zadd zset -100 a
1143 $r zadd zset 200 b
1144 $r zadd zset -300 c
1145 $r zadd zset 1000000 d
1146 $r zadd zset +inf max
1147 $r zadd zset -inf min
1148 $r zrange zset 0 -1
1149 } {min c a b d max}
1150
1151 test {EXPIRE - don't set timeouts multiple times} {
1152 $r set x foobar
1153 set v1 [$r expire x 5]
1154 set v2 [$r ttl x]
1155 set v3 [$r expire x 10]
1156 set v4 [$r ttl x]
1157 list $v1 $v2 $v3 $v4
1158 } {1 5 0 5}
1159
1160 test {EXPIRE - It should be still possible to read 'x'} {
1161 $r get x
1162 } {foobar}
1163
1164 test {EXPIRE - After 6 seconds the key should no longer be here} {
1165 after 6000
1166 list [$r get x] [$r exists x]
1167 } {{} 0}
1168
1169 test {EXPIRE - Delete on write policy} {
1170 $r del x
1171 $r lpush x foo
1172 $r expire x 1000
1173 $r lpush x bar
1174 $r lrange x 0 -1
1175 } {bar}
1176
1177 test {EXPIREAT - Check for EXPIRE alike behavior} {
1178 $r del x
1179 $r set x foo
1180 $r expireat x [expr [clock seconds]+15]
1181 $r ttl x
1182 } {1[345]}
1183
1184 test {ZSETs skiplist implementation backlink consistency test} {
1185 set diff 0
1186 set elements 10000
1187 for {set j 0} {$j < $elements} {incr j} {
1188 $r zadd myzset [expr rand()] "Element-$j"
1189 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1190 }
1191 set l1 [$r zrange myzset 0 -1]
1192 set l2 [$r zrevrange myzset 0 -1]
1193 for {set j 0} {$j < [llength $l1]} {incr j} {
1194 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1195 incr diff
1196 }
1197 }
1198 format $diff
1199 } {0}
1200
1201 foreach fuzztype {binary alpha compr} {
1202 test "FUZZ stresser with data model $fuzztype" {
1203 set err 0
1204 for {set i 0} {$i < 10000} {incr i} {
1205 set fuzz [randstring 0 512 $fuzztype]
1206 $r set foo $fuzz
1207 set got [$r get foo]
1208 if {$got ne $fuzz} {
1209 set err [list $fuzz $got]
1210 break
1211 }
1212 }
1213 set _ $err
1214 } {0}
1215 }
1216
1217 test {BGSAVE} {
1218 $r flushdb
1219 $r save
1220 $r set x 10
1221 $r bgsave
1222 waitForBgsave $r
1223 $r debug reload
1224 $r get x
1225 } {10}
1226
1227 # Leave the user with a clean DB before to exit
1228 test {FLUSHDB} {
1229 set aux {}
1230 $r select 9
1231 $r flushdb
1232 lappend aux [$r dbsize]
1233 $r select 10
1234 $r flushdb
1235 lappend aux [$r dbsize]
1236 } {0 0}
1237
1238 test {Perform a final SAVE to leave a clean DB on disk} {
1239 $r save
1240 } {OK}
1241
1242 catch {
1243 if {[string match {*Darwin*} [exec uname -a]]} {
1244 test {Check for memory leaks} {
1245 exec leaks redis-server
1246 } {*0 leaks*}
1247 }
1248 }
1249
1250 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1251 if {$::failed > 0} {
1252 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1253 }
1254 }
1255
1256 proc stress {} {
1257 set r [redis]
1258 $r select 9
1259 $r flushdb
1260 while 1 {
1261 set randkey [expr int(rand()*10000)]
1262 set randval [expr int(rand()*10000)]
1263 set randidx0 [expr int(rand()*10)]
1264 set randidx1 [expr int(rand()*10)]
1265 set cmd [expr int(rand()*20)]
1266 catch {
1267 if {$cmd == 0} {$r set $randkey $randval}
1268 if {$cmd == 1} {$r get $randkey}
1269 if {$cmd == 2} {$r incr $randkey}
1270 if {$cmd == 3} {$r lpush $randkey $randval}
1271 if {$cmd == 4} {$r rpop $randkey}
1272 if {$cmd == 5} {$r del $randkey}
1273 if {$cmd == 6} {$r llen $randkey}
1274 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
1275 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
1276 if {$cmd == 9} {$r lindex $randkey $randidx0}
1277 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
1278 if {$cmd == 11} {$r sadd $randkey $randval}
1279 if {$cmd == 12} {$r srem $randkey $randval}
1280 if {$cmd == 13} {$r smove $randkey $randval}
1281 if {$cmd == 14} {$r scard $randkey}
1282 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
1283 }
1284 flush stdout
1285 }
1286 $r flushdb
1287 $r close
1288 }
1289
1290 # Set a few configuration defaults
1291 set ::host 127.0.0.1
1292 set ::port 6379
1293 set ::stress 0
1294 set ::flush 0
1295 set ::first 0
1296 set ::last 1000000
1297
1298 # Parse arguments
1299 for {set j 0} {$j < [llength $argv]} {incr j} {
1300 set opt [lindex $argv $j]
1301 set arg [lindex $argv [expr $j+1]]
1302 set lastarg [expr {$arg eq {}}]
1303 if {$opt eq {-h} && !$lastarg} {
1304 set ::host $arg
1305 incr j
1306 } elseif {$opt eq {-p} && !$lastarg} {
1307 set ::port $arg
1308 incr j
1309 } elseif {$opt eq {-stress}} {
1310 set ::stress 1
1311 } elseif {$opt eq {--flush}} {
1312 set ::flush 1
1313 } elseif {$opt eq {--first} && !$lastarg} {
1314 set ::first $arg
1315 incr j
1316 } elseif {$opt eq {--last} && !$lastarg} {
1317 set ::last $arg
1318 incr j
1319 } else {
1320 echo "Wrong argument: $opt"
1321 exit 1
1322 }
1323 }
1324
1325 # Before to run the test check if DB 9 and DB 10 are empty
1326 set r [redis]
1327
1328 if {$::flush} {
1329 $r flushall
1330 }
1331
1332 $r select 9
1333 set db9size [$r dbsize]
1334 $r select 10
1335 set db10size [$r dbsize]
1336 if {$db9size != 0 || $db10size != 0} {
1337 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
1338 exit 1
1339 }
1340 $r close
1341 unset r
1342 unset db9size
1343 unset db10size
1344
1345 if {$::stress} {
1346 stress
1347 } else {
1348 main $::host $::port
1349 }