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