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