]> git.saurik.com Git - redis.git/blame - test-redis.tcl
library clients update scripts
[redis.git] / test-redis.tcl
CommitLineData
ed9b544e 1# TODO # test pipelining
2
43c9dc7b 3source client-libraries/tcl/redis.tcl
4
ed9b544e 5set ::passed 0
6set ::failed 0
7
8proc test {name code okpattern} {
9 puts -nonewline [format "%-70s " $name]
10 flush stdout
11 set retval [uplevel 1 $code]
12 if {$okpattern eq $retval || [string match $okpattern $retval]} {
13 puts "PASSED"
14 incr ::passed
15 } else {
16 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
17 incr ::failed
18 }
19}
20
21proc main {server port} {
43c9dc7b 22 set r [redis $server $port]
23 set err ""
ed9b544e 24
abcb223e
BH
25 # The following AUTH test should be enabled only when requirepass
26 # <PASSWORD> is set in redis.conf and redis-server was started with
27 # redis.conf as the first argument.
28
29 #test {AUTH with requirepass in redis.conf} {
30 # $r auth foobared
31 #} {OK}
32
ed9b544e 33 test {DEL all keys to start with a clean DB} {
43c9dc7b 34 foreach key [$r keys *] {$r del $key}
35 $r dbsize
ed9b544e 36 } {0}
37
38 test {SET and GET an item} {
43c9dc7b 39 $r set x foobar
40 $r get x
ed9b544e 41 } {foobar}
42
43 test {DEL against a single item} {
43c9dc7b 44 $r del x
45 $r get x
ed9b544e 46 } {}
47
48 test {KEYS with pattern} {
49 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
43c9dc7b 50 $r set $key hello
ed9b544e 51 }
43c9dc7b 52 lsort [$r keys foo*]
ed9b544e 53 } {foo_a foo_b foo_c}
54
55 test {KEYS to get all keys} {
43c9dc7b 56 lsort [$r keys *]
ed9b544e 57 } {foo_a foo_b foo_c key_x key_y key_z}
58
59 test {DBSIZE} {
43c9dc7b 60 $r dbsize
ed9b544e 61 } {6}
62
63 test {DEL all keys} {
43c9dc7b 64 foreach key [$r keys *] {$r del $key}
65 $r dbsize
ed9b544e 66 } {0}
67
68 test {Very big payload in GET/SET} {
69 set buf [string repeat "abcd" 1000000]
43c9dc7b 70 $r set foo $buf
71 $r get foo
ed9b544e 72 } [string repeat "abcd" 1000000]
73
74 test {SET 10000 numeric keys and access all them in reverse order} {
75 for {set x 0} {$x < 10000} {incr x} {
43c9dc7b 76 $r set $x $x
ed9b544e 77 }
78 set sum 0
79 for {set x 9999} {$x >= 0} {incr x -1} {
43c9dc7b 80 incr sum [$r get $x]
ed9b544e 81 }
82 format $sum
83 } {49995000}
84
85 test {DBSIZE should be 10001 now} {
43c9dc7b 86 $r dbsize
ed9b544e 87 } {10001}
88
89 test {INCR against non existing key} {
90 set res {}
43c9dc7b 91 append res [$r incr novar]
92 append res [$r get novar]
ed9b544e 93 } {11}
94
95 test {INCR against key created by incr itself} {
43c9dc7b 96 $r incr novar
ed9b544e 97 } {2}
98
99 test {INCR against key originally set with SET} {
43c9dc7b 100 $r set novar 100
101 $r incr novar
ed9b544e 102 } {101}
103
104 test {SETNX target key missing} {
43c9dc7b 105 $r setnx novar2 foobared
106 $r get novar2
ed9b544e 107 } {foobared}
108
109 test {SETNX target key exists} {
43c9dc7b 110 $r setnx novar2 blabla
111 $r get novar2
ed9b544e 112 } {foobared}
113
114 test {EXISTS} {
115 set res {}
43c9dc7b 116 $r set newkey test
117 append res [$r exists newkey]
118 $r del newkey
119 append res [$r exists newkey]
ed9b544e 120 } {10}
121
122 test {Zero length value in key. SET/GET/EXISTS} {
43c9dc7b 123 $r set emptykey {}
124 set res [$r get emptykey]
125 append res [$r exists emptykey]
126 $r del emptykey
127 append res [$r exists emptykey]
ed9b544e 128 } {10}
129
130 test {Commands pipelining} {
43c9dc7b 131 set fd [$r channel]
ed9b544e 132 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
133 flush $fd
134 set res {}
43c9dc7b 135 append res [string match OK* [::redis::redis_read_reply $fd]]
136 append res [::redis::redis_read_reply $fd]
137 append res [string match PONG* [::redis::redis_read_reply $fd]]
ed9b544e 138 format $res
139 } {1xyzk1}
140
141 test {Non existing command} {
43c9dc7b 142 catch {$r foobaredcommand} err
143 string match ERR* $err
ed9b544e 144 } {1}
145
146 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
43c9dc7b 147 $r lpush mylist a
148 $r lpush mylist b
149 $r rpush mylist c
150 set res [$r llen mylist]
151 append res [$r lindex mylist 0]
152 append res [$r lindex mylist 1]
153 append res [$r lindex mylist 2]
ed9b544e 154 } {3bac}
155
156 test {DEL a list} {
43c9dc7b 157 $r del mylist
158 $r exists mylist
ed9b544e 159 } {0}
160
161 test {Create a long list and check every single element with LINDEX} {
162 set ok 0
163 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 164 $r rpush mylist $i
ed9b544e 165 }
166 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 167 if {[$r lindex mylist $i] eq $i} {incr ok}
168 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
ed9b544e 169 incr ok
170 }
171 }
172 format $ok
173 } {2000}
174
175 test {Test elements with LINDEX in random access} {
176 set ok 0
177 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 178 set rint [expr int(rand()*1000)]
179 if {[$r lindex mylist $rint] eq $rint} {incr ok}
180 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
ed9b544e 181 incr ok
182 }
183 }
184 format $ok
185 } {2000}
186
187 test {LLEN against non-list value error} {
43c9dc7b 188 $r del mylist
189 $r set mylist foobar
190 catch {$r llen mylist} err
191 format $err
c937aa89 192 } {ERR*}
ed9b544e 193
194 test {LINDEX against non-list value error} {
43c9dc7b 195 catch {$r lindex mylist 0} err
196 format $err
c937aa89 197 } {ERR*}
ed9b544e 198
199 test {LPUSH against non-list value error} {
43c9dc7b 200 catch {$r lpush mylist 0} err
201 format $err
c937aa89 202 } {ERR*}
ed9b544e 203
204 test {RPUSH against non-list value error} {
43c9dc7b 205 catch {$r rpush mylist 0} err
206 format $err
c937aa89 207 } {ERR*}
ed9b544e 208
209 test {RENAME basic usage} {
43c9dc7b 210 $r set mykey hello
211 $r rename mykey mykey1
212 $r rename mykey1 mykey2
213 $r get mykey2
ed9b544e 214 } {hello}
215
216 test {RENAME source key should no longer exist} {
43c9dc7b 217 $r exists mykey
ed9b544e 218 } {0}
219
220 test {RENAME against already existing key} {
43c9dc7b 221 $r set mykey a
222 $r set mykey2 b
223 $r rename mykey2 mykey
224 set res [$r get mykey]
225 append res [$r exists mykey2]
ed9b544e 226 } {b0}
227
228 test {RENAMENX basic usage} {
43c9dc7b 229 $r del mykey
230 $r del mykey2
231 $r set mykey foobar
232 $r renamenx mykey mykey2
233 set res [$r get mykey2]
234 append res [$r exists mykey]
ed9b544e 235 } {foobar0}
236
237 test {RENAMENX against already existing key} {
43c9dc7b 238 $r set mykey foo
239 $r set mykey2 bar
240 $r renamenx mykey mykey2
ed9b544e 241 } {0}
242
243 test {RENAMENX against already existing key (2)} {
43c9dc7b 244 set res [$r get mykey]
245 append res [$r get mykey2]
ed9b544e 246 } {foobar}
247
248 test {RENAME against non existing source key} {
43c9dc7b 249 catch {$r rename nokey foobar} err
250 format $err
c937aa89 251 } {ERR*}
ed9b544e 252
253 test {RENAME where source and dest key is the same} {
43c9dc7b 254 catch {$r rename mykey mykey} err
255 format $err
c937aa89 256 } {ERR*}
ed9b544e 257
258 test {DEL all keys again (DB 0)} {
43c9dc7b 259 foreach key [$r keys *] {
260 $r del $key
ed9b544e 261 }
43c9dc7b 262 $r dbsize
ed9b544e 263 } {0}
264
265 test {DEL all keys again (DB 1)} {
43c9dc7b 266 $r select 1
267 foreach key [$r keys *] {
268 $r del $key
ed9b544e 269 }
43c9dc7b 270 set res [$r dbsize]
271 $r select 0
ed9b544e 272 format $res
273 } {0}
274
275 test {MOVE basic usage} {
43c9dc7b 276 $r set mykey foobar
277 $r move mykey 1
ed9b544e 278 set res {}
43c9dc7b 279 lappend res [$r exists mykey]
280 lappend res [$r dbsize]
281 $r select 1
282 lappend res [$r get mykey]
283 lappend res [$r dbsize]
284 $r select 0
ed9b544e 285 format $res
286 } [list 0 0 foobar 1]
287
288 test {MOVE against key existing in the target DB} {
43c9dc7b 289 $r set mykey hello
290 $r move mykey 1
ed9b544e 291 } {0}
292
293 test {SET/GET keys in different DBs} {
43c9dc7b 294 $r set a hello
295 $r set b world
296 $r select 1
297 $r set a foo
298 $r set b bared
299 $r select 0
ed9b544e 300 set res {}
43c9dc7b 301 lappend res [$r get a]
302 lappend res [$r get b]
303 $r select 1
304 lappend res [$r get a]
305 lappend res [$r get b]
306 $r select 0
ed9b544e 307 format $res
308 } {hello world foo bared}
309
310 test {Basic LPOP/RPOP} {
43c9dc7b 311 $r del mylist
312 $r rpush mylist 1
313 $r rpush mylist 2
314 $r lpush mylist 0
315 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
ed9b544e 316 } [list 0 2 1 0]
317
318 test {LPOP/RPOP against empty list} {
43c9dc7b 319 $r lpop mylist
ed9b544e 320 } {}
321
322 test {LPOP against non list value} {
43c9dc7b 323 $r set notalist foo
324 catch {$r lpop notalist} err
325 format $err
c937aa89 326 } {ERR*kind*}
ed9b544e 327
328 test {Mass LPUSH/LPOP} {
329 set sum 0
330 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 331 $r lpush mylist $i
ed9b544e 332 incr sum $i
333 }
334 set sum2 0
335 for {set i 0} {$i < 500} {incr i} {
43c9dc7b 336 incr sum2 [$r lpop mylist]
337 incr sum2 [$r rpop mylist]
ed9b544e 338 }
339 expr $sum == $sum2
340 } {1}
341
342 test {LRANGE basics} {
343 for {set i 0} {$i < 10} {incr i} {
43c9dc7b 344 $r rpush mylist $i
ed9b544e 345 }
43c9dc7b 346 list [$r lrange mylist 1 -2] \
347 [$r lrange mylist -3 -1] \
348 [$r lrange mylist 4 4]
ed9b544e 349 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
350
351 test {LRANGE inverted indexes} {
43c9dc7b 352 $r lrange mylist 6 2
ed9b544e 353 } {}
354
355 test {LRANGE out of range indexes including the full list} {
43c9dc7b 356 $r lrange mylist -1000 1000
ed9b544e 357 } {0 1 2 3 4 5 6 7 8 9}
358
359 test {LRANGE against non existing key} {
43c9dc7b 360 $r lrange nosuchkey 0 1
ed9b544e 361 } {}
362
363 test {LTRIM basics} {
43c9dc7b 364 $r del mylist
ed9b544e 365 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 366 $r lpush mylist $i
367 $r ltrim mylist 0 4
ed9b544e 368 }
43c9dc7b 369 $r lrange mylist 0 -1
ed9b544e 370 } {99 98 97 96 95}
371
372 test {LSET} {
43c9dc7b 373 $r lset mylist 1 foo
374 $r lset mylist -1 bar
375 $r lrange mylist 0 -1
ed9b544e 376 } {99 foo 97 96 bar}
377
378 test {LSET out of range index} {
43c9dc7b 379 catch {$r lset mylist 10 foo} err
380 format $err
c937aa89 381 } {ERR*range*}
ed9b544e 382
383 test {LSET against non existing key} {
43c9dc7b 384 catch {$r lset nosuchkey 10 foo} err
385 format $err
c937aa89 386 } {ERR*key*}
ed9b544e 387
388 test {LSET against non list value} {
43c9dc7b 389 $r set nolist foobar
390 catch {$r lset nolist 0 foo} err
391 format $err
c937aa89 392 } {ERR*value*}
ed9b544e 393
394 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
43c9dc7b 395 $r sadd myset foo
396 $r sadd myset bar
397 list [$r scard myset] [$r sismember myset foo] \
398 [$r sismember myset bar] [$r sismember myset bla] \
399 [lsort [$r smembers myset]]
ed9b544e 400 } {2 1 1 0 {bar foo}}
401
402 test {SADD adding the same element multiple times} {
43c9dc7b 403 $r sadd myset foo
404 $r sadd myset foo
405 $r sadd myset foo
406 $r scard myset
ed9b544e 407 } {2}
408
409 test {SADD against non set} {
43c9dc7b 410 catch {$r sadd mylist foo} err
411 format $err
c937aa89 412 } {ERR*kind*}
ed9b544e 413
414 test {SREM basics} {
43c9dc7b 415 $r sadd myset ciao
416 $r srem myset foo
417 lsort [$r smembers myset]
ed9b544e 418 } {bar ciao}
419
420 test {Mass SADD and SINTER with two sets} {
421 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 422 $r sadd set1 $i
423 $r sadd set2 [expr $i+995]
ed9b544e 424 }
43c9dc7b 425 lsort [$r sinter set1 set2]
ed9b544e 426 } {995 996 997 998 999}
427
428 test {SINTERSTORE with two sets} {
43c9dc7b 429 $r sinterstore setres set1 set2
430 lsort [$r smembers setres]
ed9b544e 431 } {995 996 997 998 999}
432
433 test {SINTER against three sets} {
43c9dc7b 434 $r sadd set3 999
435 $r sadd set3 995
436 $r sadd set3 1000
437 $r sadd set3 2000
438 lsort [$r sinter set1 set2 set3]
ed9b544e 439 } {995 999}
440
441 test {SINTERSTORE with three sets} {
43c9dc7b 442 $r sinterstore setres set1 set2 set3
443 lsort [$r smembers setres]
ed9b544e 444 } {995 999}
445
446 test {SAVE - make sure there are all the types as values} {
43c9dc7b 447 $r lpush mysavelist hello
448 $r lpush mysavelist world
449 $r set myemptykey {}
450 $r set mynormalkey {blablablba}
451 $r save
c937aa89 452 } {OK}
ed9b544e 453
454 test {Create a random list} {
455 set tosort {}
456 array set seenrand {}
457 for {set i 0} {$i < 10000} {incr i} {
458 while 1 {
459 # Make sure all the weights are different because
460 # Redis does not use a stable sort but Tcl does.
43c9dc7b 461 set rint [expr int(rand()*1000000)]
462 if {![info exists seenrand($rint)]} break
ed9b544e 463 }
43c9dc7b 464 set seenrand($rint) x
465 $r lpush tosort $i
466 $r set weight_$i $rint
467 lappend tosort [list $i $rint]
ed9b544e 468 }
469 set sorted [lsort -index 1 -real $tosort]
470 set res {}
471 for {set i 0} {$i < 10000} {incr i} {
472 lappend res [lindex $sorted $i 0]
473 }
474 format {}
475 } {}
476
477 test {SORT with BY against the newly created list} {
43c9dc7b 478 $r sort tosort {BY weight_*}
ed9b544e 479 } $res
480
481 test {SORT direct, numeric, against the newly created list} {
43c9dc7b 482 $r sort tosort
ed9b544e 483 } [lsort -integer $res]
484
485 test {SORT decreasing sort} {
43c9dc7b 486 $r sort tosort {DESC}
ed9b544e 487 } [lsort -decreasing -integer $res]
488
489 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
490 set start [clock clicks -milliseconds]
491 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 492 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
ed9b544e 493 }
494 set elapsed [expr [clock clicks -milliseconds]-$start]
495 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
496 flush stdout
497 format {}
498 } {}
499
500 test {SORT speed, sorting 10000 elements list directly, 100 times} {
501 set start [clock clicks -milliseconds]
502 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 503 set sorted [$r sort tosort {LIMIT 0 10}]
ed9b544e 504 }
505 set elapsed [expr [clock clicks -milliseconds]-$start]
506 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
507 flush stdout
508 format {}
509 } {}
510
511 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
512 set start [clock clicks -milliseconds]
513 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 514 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
ed9b544e 515 }
516 set elapsed [expr [clock clicks -milliseconds]-$start]
517 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
518 flush stdout
519 format {}
520 } {}
521
522 test {SORT regression for issue #19, sorting floats} {
43c9dc7b 523 $r flushdb
ed9b544e 524 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
43c9dc7b 525 $r lpush mylist $x
ed9b544e 526 }
43c9dc7b 527 $r sort mylist
ed9b544e 528 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
529
530 test {LREM, remove all the occurrences} {
43c9dc7b 531 $r flushall
532 $r rpush mylist foo
533 $r rpush mylist bar
534 $r rpush mylist foobar
535 $r rpush mylist foobared
536 $r rpush mylist zap
537 $r rpush mylist bar
538 $r rpush mylist test
539 $r rpush mylist foo
540 set res [$r lrem mylist 0 bar]
541 list [$r lrange mylist 0 -1] $res
ed9b544e 542 } {{foo foobar foobared zap test foo} 2}
543
544 test {LREM, remove the first occurrence} {
43c9dc7b 545 set res [$r lrem mylist 1 foo]
546 list [$r lrange mylist 0 -1] $res
ed9b544e 547 } {{foobar foobared zap test foo} 1}
548
549 test {LREM, remove non existing element} {
43c9dc7b 550 set res [$r lrem mylist 1 nosuchelement]
551 list [$r lrange mylist 0 -1] $res
ed9b544e 552 } {{foobar foobared zap test foo} 0}
553
554 test {LREM, starting from tail with negative count} {
43c9dc7b 555 $r flushall
556 $r rpush mylist foo
557 $r rpush mylist bar
558 $r rpush mylist foobar
559 $r rpush mylist foobared
560 $r rpush mylist zap
561 $r rpush mylist bar
562 $r rpush mylist test
563 $r rpush mylist foo
564 $r rpush mylist foo
565 set res [$r lrem mylist -1 bar]
566 list [$r lrange mylist 0 -1] $res
ed9b544e 567 } {{foo bar foobar foobared zap test foo foo} 1}
568
569 test {LREM, starting from tail with negative count (2)} {
43c9dc7b 570 set res [$r lrem mylist -2 foo]
571 list [$r lrange mylist 0 -1] $res
ed9b544e 572 } {{foo bar foobar foobared zap test} 2}
573
5b19bd72 574 test {MGET} {
43c9dc7b 575 $r flushall
576 $r set foo BAR
577 $r set bar FOO
578 $r mget foo bar
5b19bd72 579 } {BAR FOO}
580
581 test {MGET against non existing key} {
43c9dc7b 582 $r mget foo baazz bar
5b19bd72 583 } {BAR {} FOO}
584
585 test {MGET against non-string key} {
43c9dc7b 586 $r sadd myset ciao
587 $r sadd myset bau
588 $r mget foo baazz bar myset
5b19bd72 589 } {BAR {} FOO {}}
590
ce7bef07 591 test {RANDOMKEY} {
592 $r flushall
593 $r set foo x
594 $r set bar y
595 set foo_seen 0
596 set bar_seen 0
597 for {set i 0} {$i < 100} {incr i} {
598 set rkey [$r randomkey]
599 if {$rkey eq {foo}} {
600 set foo_seen 1
601 }
602 if {$rkey eq {bar}} {
603 set bar_seen 1
604 }
605 }
606 list $foo_seen $bar_seen
607 } {1 1}
608
609 test {RANDOMKEY against empty DB} {
610 $r flushall
611 $r randomkey
612 } {}
613
ed9b544e 614 # Leave the user with a clean DB before to exit
615 test {FLUSHALL} {
43c9dc7b 616 $r flushall
617 $r dbsize
ed9b544e 618 } {0}
619
620 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
621 if {$::failed > 0} {
622 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
623 }
624 close $fd
625}
626
5a6948fb 627proc stress {} {
43c9dc7b 628 set r [redis]
629 $r flushall
5a6948fb 630 while 1 {
631 set randkey [expr int(rand()*10000)]
632 set randval [expr int(rand()*10000)]
633 set randidx0 [expr int(rand()*10)]
634 set randidx1 [expr int(rand()*10)]
635 set cmd [expr int(rand()*10)]
43c9dc7b 636 catch {
637 if {$cmd == 0} {$r set $randkey $randval}
638 if {$cmd == 1} {$r get $randkey}
639 if {$cmd == 2} {$r incr $randkey}
640 if {$cmd == 3} {$r lpush $randkey $randval}
641 if {$cmd == 4} {$r rpop $randkey}
642 if {$cmd == 5} {$r del $randkey}
643 if {$cmd == 6} {$r lrange $randkey $randidx0 $randidx1}
644 if {$cmd == 7} {$r ltrim $randkey $randidx0 $randidx1}
645 if {$cmd == 8} {$r lindex $randkey $randidx0}
646 if {$cmd == 9} {$r lset $randkey $randidx0 $randval}
647 }
5a6948fb 648 flush stdout
649 }
43c9dc7b 650 $r close
5a6948fb 651}
652
ed9b544e 653if {[llength $argv] == 0} {
654 main 127.0.0.1 6379
5a6948fb 655} elseif {[llength $argv] == 1 && [lindex $argv 0] eq {stress}} {
656 stress
ed9b544e 657} else {
658 main [lindex $argv 0] [lindex $argv 1]
659}